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); }
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); }
/// <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); }
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>()); } }
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); }