Пример #1
0
        // inherit javadoc
        public override TopDocs Search(Weight weight, Filter filter, int nDocs)
        {
            if (nDocs <= 0)
            {
                // null might be returned from hq.top() below.
                throw new System.ArgumentException("nDocs must be > 0");
            }

            TopDocCollector collector = new TopDocCollector(nDocs);

            Search(weight, filter, collector);
            return(collector.TopDocs());
        }
Пример #2
0
        // inherit javadoc
        public override TopDocs Search(Weight weight, Filter filter, int nDocs)
        {
            if (nDocs <= 0)
                // null might be returned from hq.top() below.
                throw new System.ArgumentException("nDocs must be > 0");

            TopDocCollector collector = new TopDocCollector(nDocs);
            Search(weight, filter, collector);
            return collector.TopDocs();
        }
Пример #3
0
        public IEnumerable<Content> Search(string keyword, int page, int pageSize, out int totals)
        {
            lock (locker)
            {
                List<Content> result = new List<Content>();
                IndexReader reader = IndexReader.Open(DBNLConfigurationManager.LuceneElement.IndexingFolder);

                IndexSearcher searcher = new IndexSearcher(reader);

                TopDocCollector collector = new TopDocCollector((page + 1) * pageSize);

                PhraseQuery pquery = new PhraseQuery();
                BooleanQuery myquery = new BooleanQuery();
                PhraseQuery q2 = new PhraseQuery();
                //grab the search terms from the query string
                string[] str = keyword.Split(' ');
                //build the query
                foreach (string word in str)
                {
                    //brand is the field I'm searching in
                    q2.Add(new Term("content", word.ToLower()));
                }

                //finally, add it to the BooleanQuery object
                myquery.Add(q2, BooleanClause.Occur.MUST);

                //foreach (string srt in keyword.Split(new char[] {' '}))
                //{
                //    pquery.Add(new Term("content", srt.ToLower()));
                //}
                //pquery.Add(q2, BooleanClause.Occur.MUST);

                TermQuery query = new TermQuery(new Term("content", keyword));
                //            TopDocs topDocs = searcher.Search(query, collector);
                //searcher.Search(query, collector);

                QueryParser qp = new QueryParser("content", new StandardAnalyzer());

                //Contains a phrase such as "this is a phrase"
                Query q = qp.Parse(keyword);
                //Hits hits = searcher.Search(q);
                //Hits hits = searcher.Search(query);
                Hits hits = searcher.Search(myquery);

                //ScoreDoc[] hits = collector.TopDocs().scoreDocs;
                totals = hits.Length();
                Lucene.Net.Highlight.Formatter formatter = new Lucene.Net.Highlight.SimpleHTMLFormatter(
            "<span class=\"Highlight\">",
            "</span>");

                Lucene.Net.Highlight.SimpleFragmenter fragmenter = new Lucene.Net.Highlight.SimpleFragmenter(400);
                Lucene.Net.Highlight.QueryScorer scorer = new Lucene.Net.Highlight.QueryScorer(myquery);
                Lucene.Net.Highlight.Highlighter highlighter = new Lucene.Net.Highlight.Highlighter(formatter, scorer);
                highlighter.SetTextFragmenter(fragmenter);

                for (int i = (page - 1) * pageSize; i < Math.Min(page * pageSize, hits.Length()); i++)
                {

                    Document doc = hits.Doc(i);
                    string raw_text = doc.Get("content");

                    Lucene.Net.Analysis.TokenStream stream = analyzer.TokenStream("", new System.IO.StringReader(raw_text));
                    string highlighted_text = highlighter.GetBestFragments(stream, raw_text, 1, "...").Replace("'", "''");

                    if (highlighted_text == "") // someties the highlighter fails to emit text...
                    {
                        highlighted_text = raw_text.Replace("'", "''");
                    }
                    if (highlighted_text.Length > 500)
                    {
                        highlighted_text = highlighted_text.Substring(0, 500);
                    }

                    Content content = new ContentService().GetItem(int.Parse(doc.Get("id")));
                    content.HighlightText = highlighted_text;
                    result.Add(content);
                }
                reader.Close();

                searcher.Close();
                return result.AsEnumerable();
            }
        }
Пример #4
0
    ////string[] querys = { "dominos pizza" };
    //string[] fields = { "title" };
    //Lucene.Net.Search.BooleanClause.Occur[] clauses = { Lucene.Net.Search.BooleanClause.Occur.MUST };
    //string[] sortby = { "id" };
    //XmlDocument xDoc = GetSearchResult(@"C:\luceneindexpath\", querys, fields, clauses, sortby);
    //return xDoc;
    public static XmlDocument GetSearchResult(string Indexpath, string[] querys, string[] fields, BooleanClause.Occur[] clauses, string[] sortingfields)
    {
        //XMLDocument is created to return output records
        XmlDocument xResDoc = new XmlDocument();
        xResDoc.LoadXml("<SEARCHRESULTS></SEARCHRESULTS>");
        try
        {
            // This block will parse the given queries
            // (i.e) would remove characters other than a-z and 0-9
            for (int i = 0; i < querys.Length; i++)
            {
                querys[i] = querys[i].ToLower();
                querys[i] = System.Text.RegularExpressions.Regex.Replace(querys[i], "[^a-z0-9]", " ");
                while (querys[i].Contains("  "))
                    querys[i] = querys[i].Replace("  ", " ");
            }
            //

            if (System.IO.Directory.Exists(Indexpath))
            {
                // Open's Already created Lucene Index for searching
                Lucene.Net.Index.IndexReader idxReader = Lucene.Net.Index.IndexReader.Open(Indexpath);
                Searcher searcher = new IndexSearcher(idxReader);

                //Creates a Lucene Query with given search inputs
                Query qry = MultiFieldQueryParser.Parse(querys, fields, clauses, new StandardAnalyzer());
                ScoreDoc[] hits;

                //Assigns No. of Records to be fetched
                //here idxReader max records is used
                //can be changed to get specific top records
                int top = idxReader.MaxDoc();

                //To get output with specific sort
                if (sortingfields.Length > 0)
                {
                    TopFieldDocCollector collector = new TopFieldDocCollector(idxReader, new Sort(sortingfields), top);
                    searcher.Search(qry, collector);
                    hits = collector.TopDocs().scoreDocs;
                }
                else //To get output without specific sort
                {
                    TopDocCollector collector = new TopDocCollector(top);
                    searcher.Search(qry, collector);
                    hits = collector.TopDocs().scoreDocs;
                }

                //Loops through the output records and creates an XMLElement for XML Output
                for (int i = 0; i < hits.Length; i++)
                {
                    int docId = hits[i].doc;
                    Document doc = searcher.Doc(docId);
                    XmlElement xEle = xResDoc.CreateElement("SEARCHRESULT");
                    foreach (Field fl in doc.Fields())
                    {
                        xEle.SetAttribute(fl.Name(), doc.Get(fl.Name()));
                    }
                    xResDoc.DocumentElement.AppendChild(xEle);
                }
                searcher.Close();
                idxReader.Close();
            }
        }
        catch (Exception ex)
        {
            //Print Error Message
        }
        return xResDoc;
    }
Пример #5
0
        protected override IEnumerable<LucObject> DoExecute(Query query, bool allVersions, IndexReader idxReader, out int totalCount)
        {
            Searcher searcher = null;

            BeginFullExecutingTime();
            try
            {
                IEnumerable<LucObject> result;
                searcher = new IndexSearcher(idxReader);
                TopDocs topDocs;
                ScoreDoc[] hits;
                var top = 100000; // 501; //idxReader.MaxDoc();
                if (this.LucQuery.SortFields.Length > 0)
                {
                    BeginKernelTime();
                    TopFieldDocCollector collector = new TopFieldDocCollector(idxReader, new Sort(this.LucQuery.SortFields), top);
                    searcher.Search(query, collector);
                    FinishKernelTime();
                    BeginCollectingTime();
                    topDocs = collector.TopDocs();
                    totalCount = topDocs.TotalHits;
                    hits = topDocs.ScoreDocs;
                    FinishCollectingTime();
                }
                else
                {
                    BeginKernelTime();
                    TopDocCollector collector = new TopDocCollector(top);
                    searcher.Search(query, collector);
                    FinishKernelTime();
                    BeginCollectingTime();
                    topDocs = collector.TopDocs();
                    totalCount = topDocs.TotalHits;
                    hits = topDocs.ScoreDocs;
                    FinishCollectingTime();
                }
                BeginPagingTime();
                result = GetResultPage(hits, searcher, allVersions);
                FinishPagingTime();


                return result;
            }
            catch
            {
                FinishKernelTime();
                FinishCollectingTime();
                FinishPagingTime();
                throw;
            }
            finally
            {
                FinishFullExecutingTime();
                if (searcher != null)
                {
                    searcher.Close();
                    searcher = null;
                }
            }
        }