public static List<SearchRecord> HighLightSearchFile() { List<SearchRecord> recordList = new List<SearchRecord>(); try { Query query = GetFileQuery(); IndexSearcher presearcher = new IndexSearcher(fileSet.Path); ParallelMultiSearcher searcher = new ParallelMultiSearcher(new IndexSearcher[] { presearcher }); #if DEBUG System.Console.WriteLine(query.ToString()); #endif Highlighter highlighter = new Highlighter(new QueryScorer(query)); highlighter.SetTextFragmenter(new SimpleFragmenter(SupportClass.FRAGMENT_SIZE)); TopDocs topDocs = searcher.Search(query.Weight(searcher), null, searchSet.MaxMatches); ScoreDoc[] scoreDocs = topDocs.scoreDocs; for (int i = 0; i < scoreDocs.Length; i++) { float score = scoreDocs[i].score; if (score < searchSet.MinScore) continue; Document doc = searcher.Doc(scoreDocs[i].doc); string name = doc.Get("Name"); string path = doc.Get("Path"); string content = doc.Get("Content"); TokenStream nts = analyzer.TokenStream("Name", new System.IO.StringReader(name)); TokenStream pts = analyzer.TokenStream("Path", new System.IO.StringReader(path)); TokenStream cts = analyzer.TokenStream("Content", new System.IO.StringReader(content)); string nr = "",pr="",cr=""; nr = highlighter.GetBestFragment(nts, name); pr = highlighter.GetBestFragment(pts, path); cr = highlighter.GetBestFragment(cts, content); SearchField nf; SearchField pf; SearchField cf; if (nr != null && string.IsNullOrEmpty(nr.Trim()) == false) { nf = new SearchField("文件名", "文件名", name, nr, 1.0f, true, true, 0); } else { nf = new SearchField("文件名", "文件名", name, name, 1.0f, true, true, 0); } if (pr != null && string.IsNullOrEmpty(pr.Trim()) == false) { pf = new SearchField("路径", "路径", path, pr, 1.0f, false, true, 0); } else { pf = new SearchField("路径", "路径", path, path, 1.0f, false, true, 0); } if (cr != null && string.IsNullOrEmpty(cr.Trim()) == false) { cf = new SearchField("内容", "内容", content, cr, 1.0f, false, true, 0); } else { cf = new SearchField("内容", "内容", content, content, 1.0f, false, true, 0); } recordList.Add(new SearchRecord("文件", "文件", "文件",score, nf, pf, cf)); } } catch (Exception e) { SupportClass.FileUtil.WriteToLog(SupportClass.LogPath, e.StackTrace.ToString()); } return recordList; }
public static List<SearchRecord> SearchFile() { List<SearchRecord> recordList = new List<SearchRecord>(); try { Query query = GetFileQuery(); IndexSearcher presearcher = new IndexSearcher(fileSet.Path); ParallelMultiSearcher searcher = new ParallelMultiSearcher(new IndexSearcher[] { presearcher }); #if DEBUG System.Console.WriteLine(query.ToString()); #endif TopDocs topDocs = searcher.Search(query.Weight(searcher), null, searchSet.MaxMatches); ScoreDoc[] scoreDocs = topDocs.scoreDocs; for (int i = 0; i < scoreDocs.Length; i++) { float score = scoreDocs[i].score; if (score < searchSet.MinScore) continue; Document doc = searcher.Doc(scoreDocs[i].doc); string name = doc.Get("Name"); string path = doc.Get("Path"); string content = doc.Get("Content"); SearchField nf = new SearchField("文件名", "文件名", name, name, 1.0f, true, true, 0); SearchField pf = new SearchField("路径", "路径", path, path, 1.0f, false, true, 0); SearchField cf = new SearchField("内容", "内容", content, content, 1.0f, false, true, 0); recordList.Add(new SearchRecord("文件","文件","文件",score,nf,pf,cf)); } } catch (Exception e) { SupportClass.FileUtil.WriteToLog(SupportClass.LogPath, e.StackTrace.ToString()); } return recordList; }