public IEnumerable<Hit> Search(string query, int maxResults) { var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29); QueryParser qp = new QueryParser( Lucene.Net.Util.Version.LUCENE_29, "contents", analyzer ); Query q = qp.Parse(query); TopDocs top = searcher.Search(q, maxResults); List<Hit> result = new List<Hit>(top.totalHits); for (int index = 0; index < top.totalHits; index++) { var doc = searcher.Doc(top.scoreDocs[index].doc); string contents = doc.Get("contents"); var scorer = new QueryScorer(q, searcher.GetIndexReader(), "contents"); var highlighter = new Highlighter(scorer); result.Add(new Hit() { Relevance = top.scoreDocs[index].score, Title = doc.Get("title"), Url = doc.Get("path"), Excerpt = highlighter.GetBestFragment(analyzer, "contents", contents) }); } return result; }
public string HighlightTitle(string text) { QueryScorer scorer = new QueryScorer(GetQuery()); Formatter formatter = new SimpleHTMLFormatter("<span style='color:maroon; font-weight:bold;'>", "</span>"); Highlighter highlighter = new Highlighter(formatter, scorer); highlighter.SetTextFragmenter(new NullFragmenter()); TokenStream stream = new StandardAnalyzer().TokenStream("Title", new StringReader(text)); var title = highlighter.GetBestFragment(stream, text); return title ?? text; }
public IEnumerable<SearchResult> Search(string query) { Analyzer analyzer = new SnowballAnalyzer("English"); QueryParser parser = new QueryParser("text", analyzer); Query luceneQuery = parser.Parse(query); Directory directory = FSDirectory.GetDirectory(indexPath); IndexSearcher searcher = new IndexSearcher(directory); QueryScorer queryScorer = new QueryScorer(luceneQuery); Highlighter highlighter = new Highlighter(queryScorer); TopDocs topDocs = searcher.Search(luceneQuery, 100); var searchResults = new List<SearchResult>(); foreach (ScoreDoc scoreDoc in topDocs.scoreDocs) { Document doc = searcher.Doc(scoreDoc.doc); searchResults.Add(new SearchResult { Path = doc.Get("path"), Score = scoreDoc.score, Title = doc.Get("title"), Preview = highlighter.GetBestFragment(analyzer, "text", doc.Get("text")) }); } return searchResults; }
public static List<SearchRecord> SearchPage(out Query query, out Dictionary<string, int> statistics,List<string> filterList,int pageSize, int pageNum,bool fileInclude,bool highLight) { List<SearchRecord> recordList = new List<SearchRecord>(); query = GetQuery(fileInclude); statistics = new Dictionary<string, int>(); try { #region Add Index Dir //SupportClass.FileUtil.WriteToLog(@"D:\Indexer\log\search.log", "begin to init searcher."); List<IndexSearcher> searcherList = new List<IndexSearcher>(); if (searchIndexList.Count > 0) { foreach (IndexSet indexSet in searchIndexList) { if (indexSet.Type == IndexTypeEnum.Increment) continue; searcherList.Add(new IndexSearcher(indexSet.Path)); } } else { foreach (IndexSet indexSet in indexFieldsDict.Keys) { if (indexSet.Type == IndexTypeEnum.Increment) continue; searcherList.Add(new IndexSearcher(indexSet.Path)); } } if (fileInclude) { searcherList.Add(new IndexSearcher(fileSet.Path)); } #endregion //SupportClass.FileUtil.WriteToLog(@"D:\Indexer\log\search.log", "begin to Search."); ParallelMultiSearcher searcher = new ParallelMultiSearcher(searcherList.ToArray()); TopDocs topDocs = searcher.Search(query.Weight(searcher), null, searchSet.MaxMatches); ScoreDoc[] scoreDocs = topDocs.scoreDocs; Highlighter highlighter = new Highlighter(new QueryScorer(query)); highlighter.SetTextFragmenter(new SimpleFragmenter(SupportClass.FRAGMENT_SIZE)); #region Order by Score //SupportClass.FileUtil.WriteToLog(@"D:\Indexer\log\search.log", "Add to list."); List<ScoreDoc> scoreDocList = new List<ScoreDoc>(); for (int i = 0; i < scoreDocs.Length; i++) { float score = scoreDocs[i].score; if (score < searchSet.MinScore) continue; scoreDocList.Add(scoreDocs[i]); } //SupportClass.FileUtil.WriteToLog(@"D:\Indexer\log\search.log", "Begin to sort."); scoreDocList.Sort(delegate(ScoreDoc x, ScoreDoc y) { if (x.score > y.score) return -1; else if (x.score == y.score) return 0; else return 1; }); //SupportClass.FileUtil.WriteToLog(@"D:\Indexer\log\search.log", "End sort."); #endregion #region Doc Statistic int start = 0, end = scoreDocList.Count; if (pageSize > 0 && pageNum >= 1) { start = pageSize * (pageNum - 1)+1; end = pageNum * pageSize; } int current = 1; SpecialFieldSelector sfSelector = new SpecialFieldSelector(SupportClass.TableFileNameField); for (int recNum = 0; recNum < scoreDocList.Count; recNum++) { float score = scoreDocList[recNum].score; if (score < searchSet.MinScore) continue; Document fDoc = searcher.Doc(scoreDocList[recNum].doc,sfSelector); string caption = fDoc.Get(SupportClass.TableFileNameField); if ((caption.Equals(SupportClass.TFNFieldValue) == false)) { if (sfpDict.ContainsKey(caption) == false || nameIndexDict.ContainsKey(caption) == false) { continue; } } if (statistics.ContainsKey(caption)) { statistics[caption] = statistics[caption] + 1; } else { statistics.Add(caption, 1); } if (filterList != null && filterList.Count>0) { if (!filterList.Contains(caption)) continue; } #region Add Page if (current >= start && current <= end) { Document doc = searcher.Doc(scoreDocList[recNum].doc); doc.RemoveField(SupportClass.TableFileNameField); Dictionary<string, IndexField> fpDict = sfpDict[caption]; Field[] fields = new Field[doc.GetFields().Count]; doc.GetFields().CopyTo(fields, 0); #region SearchField List<SearchField> sfList = new List<SearchField>(); foreach (Field field in fields) { string key = field.Name(); string value = field.StringValue(); string result = ""; if (highLight) { string output = SupportClass.String.DropHTML(value); TokenStream tokenStream = analyzer.TokenStream(key, new System.IO.StringReader(output)); result = highlighter.GetBestFragment(tokenStream, output); if (result != null && string.IsNullOrEmpty(result.Trim()) == false) { if (fpDict.ContainsKey(key)) sfList.Add(new SearchField(key, fpDict[key].Caption, value, result, field.GetBoost(), fpDict[key].IsTitle, true, fpDict[key].Order)); else sfList.Add(new SearchField(key, key, value, result, field.GetBoost(), false, false, 0)); } else { if (fpDict.ContainsKey(key)) sfList.Add(new SearchField(key, fpDict[key].Caption, value, value, field.GetBoost(), fpDict[key].IsTitle, true, fpDict[key].Order)); else sfList.Add(new SearchField(key, key, value, result, field.GetBoost(), false, false, 0)); } } else { if (fpDict.ContainsKey(key)) sfList.Add(new SearchField(key, fpDict[key].Caption, value, value, field.GetBoost(), fpDict[key].IsTitle, true, fpDict[key].Order)); else sfList.Add(new SearchField(key, key, value, result, field.GetBoost(), false, false, 0)); } } #endregion if (caption.Equals(SupportClass.TFNFieldValue) == false) { IndexSet indexSet = nameIndexDict[caption]; recordList.Add(new SearchRecord(indexSet, sfList, indexDict[indexSet].PrimaryKey, score)); } else { recordList.Add(new SearchRecord("文件", "文件", "文件", score, sfList)); } } #endregion current++; } //SupportClass.FileUtil.WriteToLog(@"D:\Indexer\log\search.log", "End of Search."); #endregion } catch (Exception) { //SupportClass.FileUtil.WriteToLog(@"D:\Indexer\log\search_log.txt", e.StackTrace.ToString()); } return recordList; }
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> HighLightSearch(out Dictionary<string,List<int>> statistics) { List<SearchRecord> recordList = new List<SearchRecord>(); statistics = new Dictionary<string,List<int>>(); try { if (searchIndexList.Count > 0) { foreach (IndexSet indexSet in searchIndexList) { if (indexSet.Type == IndexTypeEnum.Increment) continue; Query query = GetQuery(indexSet); Source source = indexDict[indexSet]; Dictionary<string, IndexField> fpDict = source.FieldDict; //IndexSearcher searcher = new IndexSearcher(indexSet.Path); IndexSearcher presearcher = new IndexSearcher(indexSet.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; List<int> posList=new List<int>(); 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); Field[] fields = new Field[doc.GetFields().Count]; doc.GetFields().CopyTo(fields, 0); List<SearchField> sfList = new List<SearchField>(); foreach (Field field in fields) { string key = field.Name(); string value = field.StringValue(); string output = SupportClass.String.DropHTML(value); TokenStream tokenStream = analyzer.TokenStream(key, new System.IO.StringReader(output)); string result = ""; result = highlighter.GetBestFragment(tokenStream, output); if (result != null && string.IsNullOrEmpty(result.Trim()) == false) { if (fpDict.ContainsKey(key)) sfList.Add(new SearchField(key, fpDict[key].Caption, value, result, field.GetBoost(), fpDict[key].IsTitle, true, fpDict[key].Order)); else sfList.Add(new SearchField(key, key, value, result, field.GetBoost(), false, false, 0)); } else { if (fpDict.ContainsKey(key)) sfList.Add(new SearchField(key, fpDict[key].Caption, value, value, field.GetBoost(), fpDict[key].IsTitle, true, fpDict[key].Order)); else sfList.Add(new SearchField(key, key, value, result, field.GetBoost(), false, false, 0)); } } recordList.Add(new SearchRecord(indexSet, sfList, indexDict[indexSet].PrimaryKey, score)); posList.Add(recordList.Count - 1); } try { statistics.Add(indexSet.Caption, posList); } catch (Exception) { int i = 2; while (statistics.ContainsKey(indexSet.Caption + i.ToString())) i++; statistics.Add(indexSet.Caption + i.ToString(), posList); } } } else { foreach (IndexSet indexSet in indexFieldsDict.Keys) { if (indexSet.Type == IndexTypeEnum.Increment) continue; Query query = GetQuery(indexSet); Source source = indexDict[indexSet]; Dictionary<string, IndexField> fpDict = source.FieldDict; //IndexSearcher searcher = new IndexSearcher(indexSet.Path); IndexSearcher presearcher = new IndexSearcher(indexSet.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; List<int> posList=new List<int>(); 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); Field[] fields = new Field[doc.GetFields().Count]; doc.GetFields().CopyTo(fields, 0); List<SearchField> sfList = new List<SearchField>(); foreach (Field field in fields) { string key = field.Name(); string value = field.StringValue(); string output = SupportClass.String.DropHTML(value); TokenStream tokenStream = analyzer.TokenStream(key, new System.IO.StringReader(output)); string result = ""; result = highlighter.GetBestFragment(tokenStream, output); if (result != null && string.IsNullOrEmpty(result.Trim()) == false) { if (fpDict.ContainsKey(key)) sfList.Add(new SearchField(key, fpDict[key].Caption, value, result, field.GetBoost(), fpDict[key].IsTitle, true, fpDict[key].Order)); else sfList.Add(new SearchField(key, key, value, result, field.GetBoost(), false, false, 0)); } else { if (fpDict.ContainsKey(key)) sfList.Add(new SearchField(key, fpDict[key].Caption, value, value, field.GetBoost(), fpDict[key].IsTitle, true, fpDict[key].Order)); else sfList.Add(new SearchField(key, key, value, result, field.GetBoost(), false, false, 0)); } } recordList.Add(new SearchRecord(indexSet, sfList, indexDict[indexSet].PrimaryKey, score)); posList.Add(recordList.Count - 1); } try { statistics.Add(indexSet.Caption, posList); } catch (Exception) { int i = 2; while (statistics.ContainsKey(indexSet.Caption + i.ToString())) i++; statistics.Add(indexSet.Caption + i.ToString(), posList); } } } } catch (Exception e) { SupportClass.FileUtil.WriteToLog(SupportClass.LogPath, e.StackTrace.ToString()); } return recordList; }