Пример #1
0
        protected override BenchmarkHighlighter GetBenchmarkHighlighter(Query q)
        {
            m_highlighter = new FastVectorHighlighter(false, false);
            Query myq = q;

            return(new BenchmarkHighlighterAnonymousClass(this, m_highlighter, myq));
        }
Пример #2
0
        public static SearchResultsViewModel Search(SearchQuery searchQuery)
        {
            var ret = new SearchResultsViewModel
            {
                SearchResults = new List <SearchResultsViewModel.SearchResult>(PageSize), Query = searchQuery
            };

            // Parse query, possibly throwing a ParseException
            Query query;

            if (searchQuery.TitlesOnly)             // we only need to query on one field
            {
                var qp = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "Title",
                                         new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29));
                query = qp.Parse(searchQuery.Query);
            }
            else             // querying on both fields, Content and Title
            {
                query = MultiFieldQueryParser.Parse(Lucene.Net.Util.Version.LUCENE_29, searchQuery.Query,
                                                    SearchFields, SearchFlags,
                                                    new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29)
                                                    );
            }

            // Init the highlighter instance
            var fvh = new FastVectorHighlighter(FastVectorHighlighter.DEFAULT_PHRASE_HIGHLIGHT,
                                                FastVectorHighlighter.DEFAULT_FIELD_MATCH,
                                                new SimpleFragListBuilder(),
                                                new ScoreOrderFragmentsBuilder(new[] { "[b]" }, new[] { "[/b]" })
                                                );

            // Perform the actual search
            var tsdc = TopScoreDocCollector.create(PageSize * searchQuery.CurrentPage, true);

            Searcher.Search(query, tsdc);
            ret.TotalResults = tsdc.GetTotalHits();
            var hits = tsdc.TopDocs().ScoreDocs;

            int i;

            for (i = (searchQuery.CurrentPage - 1) * PageSize; i < hits.Length; ++i)
            {
                var d        = Searcher.Doc(hits[i].doc);
                var fq       = fvh.GetFieldQuery(query);
                var fragment = fvh.GetBestFragment(fq, Searcher.GetIndexReader(), hits[i].doc, "Content", 400);

                ret.SearchResults.Add(new SearchResultsViewModel.SearchResult
                {
                    Id          = d.Get("Id"),
                    Title       = d.Get("Title"),
                    Score       = hits[i].score,
                    LuceneDocId = hits[i].doc,
                    Fragment    = MvcHtmlString.Create(fragment.HtmlStripFragment()),
                });
            }
            return(ret);
        }
Пример #3
0
 public LuceneReturnEnumerator(TopDocs myDocuments, IndexSearcher myIndexSearcher, FastVectorHighlighter myHighlighter, Analyzer myAnalyzer, Query myQuery)
 {
     _IndexSearcher = myIndexSearcher;
     _docs          = myDocuments;
     _doccount      = _docs.ScoreDocs.Count();
     _highlighter   = myHighlighter;
     _analyzer      = myAnalyzer;
     _query         = myQuery;
 }
Пример #4
0
        private string GetHighlightedText(FastVectorHighlighter highlighter, FieldQuery fieldQuery, IndexSearcher searcher, ScoreDoc match, string tag, int length)
        {
            var s = highlighter.GetBestFragment(fieldQuery, searcher.IndexReader, match.Doc, tag, length);

            if (!string.IsNullOrEmpty(s))
            {
                s = HttpUtility.HtmlEncode(s).Replace(HighlightPreTag, HtmlPreTag).Replace(HighlightPostTag, HtmlPostTag);
            }
            return(s);
        }
Пример #5
0
        public LuceneReturn(TopScoreDocCollector myCollector, IndexSearcher myIndexSearcher, Query myQuery, Analyzer myAnalyzer)
        {
            _Collector     = myCollector;
            _IndexSearcher = myIndexSearcher;
            _docs          = _Collector.TopDocs();
            bOpen          = true;

            _highlighter = new FastVectorHighlighter(true, true, new SimpleFragListBuilder(), new SimpleFragmentsBuilder());
            _analyzer    = myAnalyzer;
            _query       = myQuery;
        }
Пример #6
0
        public static IEnumerable <SearchResult> Search(
            string queryString,
            out int totalResults,
            int maxResults = 100)
        {
            totalResults = 0;
            if (string.IsNullOrEmpty(queryString))
            {
                return(new List <SearchResult>());
            }

            var indexReader = DirectoryReader.Open(
                directory: Index,
                readOnly: true);
            var indexSearcher = new IndexSearcher(indexReader);
            var queryParser   = new QueryParser(
                Lucene.Net.Util.Version.LUCENE_30,
                "Body",
                Analyzer
                );

            queryParser.AllowLeadingWildcard = true;
            queryParser.DefaultOperator      = QueryParser.Operator.AND;
            var query = queryParser.Parse(queryString);

            var resultsCollector = TopScoreDocCollector.Create(
                numHits: maxResults,
                docsScoredInOrder: true
                );

            indexSearcher.Search(
                query: query,
                results: resultsCollector
                );
            totalResults = resultsCollector.TotalHits;
            var result = new List <SearchResult>();

            var fvh = new FastVectorHighlighter();
            var fq  = fvh.GetFieldQuery(query);

            foreach (var scoreDoc in resultsCollector.TopDocs().ScoreDocs)
            {
                string[] fragments    = fvh.GetBestFragments(fq, indexSearcher.IndexReader, scoreDoc.Doc, "Body", 100, 5);
                var      doc          = indexSearcher.Doc(scoreDoc.Doc);
                var      searchResult = new SearchResult(
                    type: doc.Get("Type"),
                    id: doc.Get("ID"),
                    snippets: fragments
                    );
                result.Add(searchResult);
            }
            return(result);
        }
Пример #7
0
        private void SetupHighlighter(IndexQueryServerSide query, Query luceneQuery, JsonOperationContext context)
        {
            var fragmentsBuilder = new PerFieldFragmentsBuilder(query, context);

            _highlighter = new FastVectorHighlighter(
                FastVectorHighlighter.DEFAULT_PHRASE_HIGHLIGHT,
                FastVectorHighlighter.DEFAULT_FIELD_MATCH,
                new SimpleFragListBuilder(),
                fragmentsBuilder);

            _highlighterQuery = _highlighter.GetFieldQuery(luceneQuery);
        }
Пример #8
0
        /// <summary>
        /// Runs Lucene Highlighter module over search results and original Lucene Query.
        /// </summary>
        /// <param name="query"></param>
        /// <returns>List of results transformed into LuceneSearchResult DTOs.</returns>
        protected IList <LuceneSearchResult> TransformTopDocs(Query query)
        {
            if (this.topDocs != null)
            {
                IList <LuceneSearchResult> results = new List <LuceneSearchResult>();

                FastVectorHighlighter highlighter = new FastVectorHighlighter(true, true,
                                                                              new SimpleFragListBuilder(), new ScoreOrderFragmentsBuilder(
                                                                                  BaseFragmentsBuilder.COLORED_PRE_TAGS,
                                                                                  BaseFragmentsBuilder.COLORED_POST_TAGS));
                FieldQuery fieldQuery = highlighter.GetFieldQuery(query);

                foreach (ScoreDoc scoreDoc in this.topDocs.ScoreDocs)
                {
                    string snippet = highlighter.GetBestFragment(fieldQuery, this.searcher.IndexReader, scoreDoc.Doc, "FileData", 200);
                    results.Add(new LuceneSearchResult(this.searcher.Doc(scoreDoc.Doc), scoreDoc.Score, this.topDocs.TotalHits, snippet));
                }

                return(results);
            }
            return(null);
        }
Пример #9
0
 public BenchmarkHighlighterAnonymousClass(SearchTravRetVectorHighlightTask outerInstance, FastVectorHighlighter highlighter, Query myq)
 {
     this.outerInstance = outerInstance;
     this.highlighter   = highlighter;
     this.myq           = myq;
 }
Пример #10
0
        private static List <SearchDoc> lucene_search(Guid applicationId, int lowerBoundary, int count, ref Query query,
                                                      ref IndexSearcher searcher, bool additionalId, bool title, bool description, bool content, bool tags, bool fileContent)
        {
            try
            {
                List <SearchDoc> listDocs = new List <SearchDoc>();

                TopDocs hits = searcher.Search(query, lowerBoundary + count + (count / 2));
                FastVectorHighlighter fvHighlighter = new FastVectorHighlighter(true, true);

                for (int i = lowerBoundary, lnt = hits.ScoreDocs.Length; i < lnt; ++i)
                {
                    ScoreDoc sd = hits.ScoreDocs[i];

                    string addIdFr = !additionalId ? string.Empty :
                                     fvHighlighter.GetBestFragment(fvHighlighter.GetFieldQuery(query),
                                                                   searcher.IndexReader, docId: sd.Doc, fieldName: "AdditionalID", fragCharSize: 200);
                    string titleFr = !title ? string.Empty :
                                     fvHighlighter.GetBestFragment(fvHighlighter.GetFieldQuery(query),
                                                                   searcher.IndexReader, docId: sd.Doc, fieldName: "Title", fragCharSize: 200);
                    string descFr = !description ? string.Empty :
                                    fvHighlighter.GetBestFragment(fvHighlighter.GetFieldQuery(query),
                                                                  searcher.IndexReader, docId: sd.Doc, fieldName: "Description", fragCharSize: 200);
                    string contentFr = !content ? string.Empty :
                                       fvHighlighter.GetBestFragment(fvHighlighter.GetFieldQuery(query),
                                                                     searcher.IndexReader, docId: sd.Doc, fieldName: "Content", fragCharSize: 200);
                    string tagsFr = !tags ? string.Empty :
                                    fvHighlighter.GetBestFragment(fvHighlighter.GetFieldQuery(query),
                                                                  searcher.IndexReader, docId: sd.Doc, fieldName: "Tags", fragCharSize: 200);
                    string fileFr = !fileContent ? string.Empty :
                                    fvHighlighter.GetBestFragment(fvHighlighter.GetFieldQuery(query),
                                                                  searcher.IndexReader, docId: sd.Doc, fieldName: "FileContent", fragCharSize: 200);

                    if (!string.IsNullOrEmpty(titleFr))
                    {
                        titleFr = titleFr.Trim();
                    }
                    if (!string.IsNullOrEmpty(addIdFr))
                    {
                        addIdFr = addIdFr.Trim();
                    }

                    string highlightedText = ((string.IsNullOrEmpty(descFr) ? string.Empty : descFr + " ") +
                                              (string.IsNullOrEmpty(contentFr) ? string.Empty : contentFr + " ") +
                                              (string.IsNullOrEmpty(tagsFr) ? string.Empty : tagsFr + " ") +
                                              (string.IsNullOrEmpty(fileFr) ? string.Empty : fileFr)).Trim();

                    if (string.IsNullOrEmpty(addIdFr) && string.IsNullOrEmpty(titleFr) && string.IsNullOrEmpty(highlightedText))
                    {
                        break;
                    }

                    Document  doc  = searcher.Doc(sd.Doc);
                    SearchDoc item = SearchDoc.ToSearchDoc(doc);
                    item.Description = highlightedText;
                    listDocs.Add(item);
                }

                return(listDocs);
            }
            catch (Exception ex)
            {
                LogController.save_error_log(applicationId, null, "SearchIndexDocuments", ex, ModuleIdentifier.SRCH);
                return(new List <SearchDoc>());
            }
        }
Пример #11
0
        public static SearchResultsViewModel SearchWithSuggestions(SearchQuery searchQuery, bool suggestOnlyWhenNoResults = false)
        {
            var ret = new SearchResultsViewModel
            {
                SearchResults = new List <SearchResultsViewModel.SearchResult>(PageSize), Query = searchQuery
            };

            // Parse query, possibly throwing a ParseException
            Query query;

            if (searchQuery.TitlesOnly)
            {
                var qp = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "Title",
                                         new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29)
                                         );
                query = qp.Parse(searchQuery.Query);
            }
            else
            {
                query = MultiFieldQueryParser.Parse(Lucene.Net.Util.Version.LUCENE_29, searchQuery.Query,
                                                    SearchFields, SearchFlags,
                                                    new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29)
                                                    );
            }

            // Perform the actual search
            var tsdc = TopScoreDocCollector.create(PageSize * searchQuery.CurrentPage, true);

            Searcher.Search(query, tsdc);
            ret.TotalResults = tsdc.GetTotalHits();
            var hits = tsdc.TopDocs().ScoreDocs;

            // Do the suggestion magic
            if (suggestOnlyWhenNoResults && ret.TotalResults == 0 || (!suggestOnlyWhenNoResults))
            {
                ret.Suggestions = new List <string>();
                var spellChecker = new SpellChecker.Net.Search.Spell.SpellChecker(Searcher.GetIndexReader().Directory());

                // This is kind of a hack to get things working quickly
                // for real-world usage we probably want to get the analyzed terms from the Query object
                var individualTerms = searchQuery.Query.Split(new[] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

                foreach (var term in individualTerms)
                {
                    // we only specify field name if we actually got results,
                    // to improve suggestions relevancy
                    ret.Suggestions.AddRange(spellChecker.SuggestSimilar(term,
                                                                         searchQuery.MaxSuggestions,
                                                                         null,
                                                                         ret.TotalResults == 0 ? null : "Title",
                                                                         true));
                }
            }

            // Init the highlighter instance
            var fvh = new FastVectorHighlighter(FastVectorHighlighter.DEFAULT_PHRASE_HIGHLIGHT,
                                                FastVectorHighlighter.DEFAULT_FIELD_MATCH,
                                                new SimpleFragListBuilder(),
                                                new ScoreOrderFragmentsBuilder(new[] { "[b]" }, new[] { "[/b]" }));


            int i;

            for (i = (searchQuery.CurrentPage - 1) * PageSize; i < hits.Length; ++i)
            {
                var d        = Searcher.Doc(hits[i].doc);
                var fq       = fvh.GetFieldQuery(query);
                var fragment = fvh.GetBestFragment(fq, Searcher.GetIndexReader(),
                                                   hits[i].doc, "Content", 400);

                ret.SearchResults.Add(new SearchResultsViewModel.SearchResult
                {
                    Id          = d.Get("Id"),
                    Title       = d.Get("Title"),
                    Score       = hits[i].score,
                    LuceneDocId = hits[i].doc,
                    Fragment    = MvcHtmlString.Create(fragment.HtmlStripFragment()),
                });
            }
            return(ret);
        }
Пример #12
0
        public IEnumerable <SearchResult> Search(
            string queryString,
            string context,
            string subContext,
            out int totalResults,
            int maxResults)
        {
            totalResults = 0;
            queryString  = (queryString ?? "").Replace(":", " ");
            if (string.IsNullOrWhiteSpace(queryString))
            {
                return(new List <SearchResult>());
            }

            queryString = AddContextCriteria(queryString, context, subContext);

            IndexSearcher indexSearcher = SearcherProvider.GetIndexSearcher();
            var           analyser      = AnalyzerProvider.GetAnalyzer();
            var           queryParser   = new QueryParser(
                Lucene.Net.Util.Version.LUCENE_30,
                Consts.FullTextFieldName,
                analyser);

            queryParser.AllowLeadingWildcard = true;
            queryParser.DefaultOperator      = QueryParser.Operator.AND;

            var query = queryParser.Parse(queryString);

            var resultsCollector = TopScoreDocCollector.Create(
                numHits: 9999,
                docsScoredInOrder: true
                );

            indexSearcher.Search(
                query: query,
                results: resultsCollector
                );
            totalResults = resultsCollector.TotalHits;
            var result = new List <SearchResult>();

            var highlighter = new FastVectorHighlighter();
            var fieldQuery  = highlighter.GetFieldQuery(query);

            foreach (var scoreDoc in resultsCollector.TopDocs().ScoreDocs.Take(maxResults))
            {
                string[] fragments = highlighter.GetBestFragments(
                    fieldQuery: fieldQuery,
                    reader: indexSearcher.IndexReader,
                    docId: scoreDoc.Doc,
                    fieldName: Consts.FullTextFieldName,
                    fragCharSize: 100,
                    maxNumFragments: 5);
                var doc          = indexSearcher.Doc(scoreDoc.Doc);
                var searchResult = new SearchResult(
                    type: doc.Get(Consts.SerializedObjectTypeFieldName),
                    document: doc,
                    snippets: fragments
                    );
                result.Add(searchResult);
            }
            return(result);
        }