public IEnumerable<IHit> Query(int pageIndex, int pageSize, out int totalCount, out IEnumerable<FacetGroup> facetedResults) { totalCount = 0; facetedResults = null; if (searchPaths == null || searchPaths.Count <= 0) searchPaths.AddRange(indexPaths.Values.Select(o => o.Path)); List<LuceneHit> results = new List<LuceneHit>(); List<IndexSearcher> subSearchs = new List<IndexSearcher>(); searchPaths.ForEach(o => subSearchs.Add(new IndexSearcher(FSDirectory.Open(o)))); if (facetFields != null && facetFields.Count > 0) { var facetGroups = new List<FacetGroup>(); var mainQueryFilter = new CachingWrapperFilter(new QueryWrapperFilter(query)); MultiReader readers = new MultiReader(subSearchs.Select(o => o.IndexReader).ToArray()); foreach (var facetField in facetFields) { FacetGroup fg = new FacetGroup(); fg.FieldName = facetFieldNameProvider.GetMapName(TypeName, facetField); var items = new List<FacetItem>(); var allDistinctField = FieldCache_Fields.DEFAULT.GetStrings(readers, facetField).Distinct().ToArray(); int totalHits = 0; Parallel.ForEach(allDistinctField, fieldValue => { //foreach (var fieldValue in allDistinctField) //{ var facetQuery = new TermQuery(new Term(facetField, fieldValue)); var facetQueryFilter = new CachingWrapperFilter(new QueryWrapperFilter(facetQuery)); var bs = new OpenBitSetDISI(facetQueryFilter.GetDocIdSet(readers).Iterator(), readers.MaxDoc); bs.InPlaceAnd(mainQueryFilter.GetDocIdSet(readers).Iterator()); int count = (Int32)bs.Cardinality(); FacetItem item = new FacetItem(); item.GroupValue = fieldValue; item.Count = count; items.Add(item); totalHits += count; } ); fg.FacetItems = items.OrderByDescending(o => o.Count); fg.TotalHits = totalHits; facetGroups.Add(fg); } facetedResults = facetGroups.OrderBy(o => o.FieldName); } ParallelMultiSearcher searcher = new ParallelMultiSearcher(subSearchs.ToArray()); Sort sort = null; if (sortFields != null && sortFields.Count > 0) { sort = new Sort(sortFields.ToArray()); } int maxDoc = searcher.MaxDoc; int startIndex = 0; if (pageIndex >= 0 && pageSize > 0) { startIndex = pageIndex * pageSize; maxDoc = pageSize * (pageIndex + 1); } var docs = sort == null ? searcher.Search(query, null, maxDoc) : searcher.Search(query, null, maxDoc, sort); totalCount = docs.TotalHits; int endIndex = docs.TotalHits - startIndex; for (int i = startIndex; i < endIndex; i++) { LuceneHit h = new LuceneHit(TypeName, DocumentBuilder, searcher.Doc(docs.ScoreDocs[i].Doc)); results.Add(h); } return results; }
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> 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; }
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; }
public static List<SearchRecord> SearchEx(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 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++) { Document doc = searcher.Doc(scoreDocs[i].doc); float score = scoreDocs[i].score; if (score < searchSet.MinScore) continue; Field[] fields = new Field[doc.GetFields().Count]; doc.GetFields().CopyTo(fields, 0); List<SearchField> sfList = new List<SearchField>(); foreach (Field field in fields) { if (fpDict.ContainsKey(field.Name())) sfList.Add(new SearchField(field, fpDict[field.Name()])); //else // sfList.Add(new SearchField(field,false)); } 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 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++) { Document doc = searcher.Doc(scoreDocs[i].doc); float score = scoreDocs[i].score; if (score < searchSet.MinScore) continue; Field[] fields = new Field[doc.GetFields().Count]; doc.GetFields().CopyTo(fields, 0); List<SearchField> sfList = new List<SearchField>(); foreach (Field field in fields) { if (fpDict.ContainsKey(field.Name())) sfList.Add(new SearchField(field, fpDict[field.Name()])); //else // sfList.Add(new SearchField(field, false)); } 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; }