-2
A
回答
1
#region search hightligh
public static IEnumerable<Models.Post> SearchHighligh(string input, string fieldName = "")
{
if (string.IsNullOrEmpty(input)) return new List<Models.Post>();
var terms = input.Trim().Replace("-", " ").Split(' ')
.Where(x => !string.IsNullOrEmpty(x)).Select(x => x.Trim() + "*");
input = string.Join(" ", terms);
return _searchHighligh(input, fieldName);
}
private static string getHighlight(Highlighter highlighter, StandardAnalyzer analyzer, string fieldContent)
{
Lucene.Net.Analysis.TokenStream stream = analyzer.TokenStream("", new StringReader(fieldContent));
return highlighter.GetBestFragments(stream, fieldContent, 1, ".");
}
private static IEnumerable<Models.Post> _searchHighligh(string searchQuery, string searchField = "")
{
// validation
if (string.IsNullOrEmpty(searchQuery.Replace("*", "").Replace("?", ""))) return new List<Models.Post>();
// set up lucene searcher
using (var searcher = new IndexSearcher(_directory, false))
{
var hits_limit = 1000;
var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
IFormatter formatter = new SimpleHTMLFormatter("<span style=\"font-weight:bold; background-color:yellow;\">", "</span>");
SimpleFragmenter fragmenter = new SimpleFragmenter(1000);
QueryScorer scorer = null;
ScoreDoc[] hits;
// search by single field
if (!string.IsNullOrEmpty(searchField))
{
var parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, searchField, analyzer);
var query = parseQuery(searchQuery, parser);
scorer = new QueryScorer(query);
hits = searcher.Search(query, hits_limit).ScoreDocs;
}
// search by multiple fields (ordered by RELEVANCE)
else
{
var parser = new MultiFieldQueryParser
(Lucene.Net.Util.Version.LUCENE_30, new[] { "Title", "Body" }, analyzer);
var query = parseQuery(searchQuery, parser);
scorer = new QueryScorer(query);
hits = searcher.Search(query, null, hits_limit, Sort.INDEXORDER).ScoreDocs;
}
Highlighter highlighter = new Highlighter(formatter, scorer);
highlighter.TextFragmenter = fragmenter;
var results = _mapLuceneToDataListHighligh(hits, searcher, highlighter, analyzer);
analyzer.Close();
searcher.Dispose();
return results;
}
}
private static IEnumerable<Models.Post> _mapLuceneToDataListHighligh(IEnumerable<ScoreDoc> hits, IndexSearcher searcher, Highlighter highlighter, StandardAnalyzer analyzer)
{
// v 2.9.4: use 'hit.doc'
// v 3.0.3: use 'hit.Doc'
return hits.Select(hit => _mapLuceneDocumentToDataHighligh(searcher.Doc(hit.Doc), highlighter, analyzer)).ToList();
}
private static Models.Post _mapLuceneDocumentToDataHighligh(Document doc, Highlighter highlighter, StandardAnalyzer analyzer)
{
return new Models.Post
{
Id = Convert.ToInt32(doc.Get("Id")),
//Title = getHighlight(highlighter, analyzer,doc.Get("Title")),
Title =doc.Get("Title"),
Body = getHighlight(highlighter, analyzer, doc.Get("Body")),
SiteId = Convert.ToInt16(doc.Get("SiteId")),
PostTypeId = Convert.ToInt32(doc.Get("PostTypeId")),
OwnerUserId = Convert.ToInt32(doc.Get("OwnerUserId")),
ParentId = Convert.ToInt32(doc.Get("ParentId")),
CreationDate = UTILS.Function.getDateTime((doc.Get("CreationDate")), "g")
};
}
#endregion
相關問題
- 1. Lucene.Net突出搜索關鍵字的搜索結果
- 2. 如何突出顯示網格中搜索的關鍵字
- 3. 如何突出顯示搜索關鍵字
- 4. 使用JavaScript在結果中突出顯示搜索關鍵字
- 5. 在PHP搜索腳本中突出顯示關鍵字
- 6. 突出顯示搜索結果中的關鍵字
- 7. ASP.Net中的搜索關鍵字突出顯示
- 8. 突出顯示給定搜索文本中的關鍵字
- 9. 突出顯示搜索結果中的關鍵字php
- 10. 突出顯示搜索多個關鍵字(非英文字符)
- 11. 突出顯示關鍵字
- 12. Lucene.Net產品關鍵字搜索
- 13. Lucene.Net無法搜索關鍵字「The」
- 14. 突出搜索關鍵字懸停
- 15. 在org-agenda中突出顯示'org-search-view'的搜索關鍵字
- 16. 如何在終端中突出顯示vim中的關鍵字?
- 17. 如何禁用cts:在搜索中突出顯示:搜索API?
- 18. 如何以編程方式搜索並突出顯示pdf關鍵字
- 19. 搜索結果使用Lucene.net突出顯示
- 20. Lucene.NET查詢+突出顯示
- 21. 如何在msvc中添加突出顯示的關鍵字
- 22. 在網絡搜索中突出顯示搜索字詞
- 23. ASP.NET MVC - 在搜索結果中突出顯示搜索字詞
- 24. Netbeans熱鍵關閉突出顯示的搜索文本?
- 25. 突出顯示搜索中的關鍵字以阻止URL變量的傳遞?
- 26. 如何在perl中顯示確切的搜索關鍵字
- 27. 如何在PHP中突出顯示多個關鍵字?
- 28. 突出顯示搜索TextBlock
- 29. 如何突出使用PHP preg_replace括號的搜索關鍵字?
- 30. 在Elasticsearch中搜索突出顯示(javascript)