예제 #1
0
 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;
 }
예제 #2
0
        public static List<SearchRecord> FuzzyFastFieldSearch(out Query mquery)
        {
            List<SearchRecord> docList = new List<SearchRecord>();
            mquery = null;
            try
            {
                if (searchIndexList.Count > 0)
                {
                    foreach (IndexSet indexSet in searchIndexList)
                    {
                        if (indexSet.Type == IndexTypeEnum.Increment)
                            continue;
                        IndexSearcher searcher = new IndexSearcher(indexSet.Path);
                        Query query = GetFuzzyQuery(indexSet);
#if DEBUG
                        System.Console.WriteLine(query.ToString());
#endif
                        SupportClass.FileUtil.WriteToLog(SupportClass.LogPath, query.ToString());
                        TopDocs topDocs = searcher.Search(query.Weight(searcher), null, searchSet.MaxMatches);
                        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
                        SpecialFieldSelector sfs = new SpecialFieldSelector(indexDict[indexSet].PrimaryKey);
                        for (int i = 0; i < scoreDocs.Length; i++)
                        {
                            Document doc = searcher.Doc(scoreDocs[i].doc, sfs);
                            float score = scoreDocs[i].score;
                            if (score < searchSet.MinScore)
                                continue;
                            docList.Add(doc);
                        }
                    }
                }
                else
                {
                    foreach (IndexSet indexSet in indexFieldsDict.Keys)
                    {
                        if (indexSet.Type == IndexTypeEnum.Increment)
                            continue;
                        IndexSearcher searcher = new IndexSearcher(indexSet.Path);
                        Query query = GetFuzzyQuery(indexSet);
#if DEBUG
                        System.Console.WriteLine(query.ToString());
#endif
                        SupportClass.FileUtil.WriteToLog(SupportClass.LogPath, query.ToString());
                        TopDocs topDocs = searcher.Search(query.Weight(searcher), null, searchSet.MaxMatches);
                        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
                        SpecialFieldSelector sfs = new SpecialFieldSelector(indexDict[indexSet].PrimaryKey);
                        for (int i = 0; i < scoreDocs.Length; i++)
                        {
                            Document doc = searcher.Doc(scoreDocs[i].doc, sfs);
                            float score = scoreDocs[i].score;
                            if (score < searchSet.MinScore)
                                continue;
                            docList.Add(doc);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                SupportClass.FileUtil.WriteToLog(SupportClass.LogPath, e.StackTrace.ToString());
            }
            mquery = GetQuery();
            return docList;
        }