Example #1
0
        /// <summary>
        /// 显示搜索结果
        /// </summary>
        /// <param name="queryResult"></param>
        private void ShowMultFileSearchResult(TopDocs queryResult)
        {
            if (queryResult == null || queryResult.totalHits == 0)
            {
                SetOutput("Sorry,没有搜索到你要的结果。");
                return;
            }
            Searchable[] abs = new Searchable[2];
            abs[0] = new IndexSearcher(INDEX_STORE_PATH);
            abs[1] = new IndexSearcher(INDEX_STORE_PATH1);
            MultiSearcher searcher = new MultiSearcher(abs);//构造MultiSearcher
            int           counter  = 1;

            foreach (ScoreDoc sd in queryResult.scoreDocs)
            {
                try
                {
                    Document doc      = searcher.Doc(sd.doc);
                    string   id       = doc.Get("id");       //获取id
                    string   fileName = doc.Get("filename"); //获取文件名
                    string   contents = doc.Get("contents"); //获取文件内容
                    string   result   = string.Format("这是第{0}个搜索结果,Id为{1},文件名为:{2},文件内容为:{3}{4}", counter, id, fileName, Environment.NewLine, contents);
                    SetOutput(result);
                }
                catch (Exception ex)
                {
                    SetOutput(ex.Message);
                }
                counter++;
            }
        }
Example #2
0
        public SearchResponse Search(SearchRequest request)
        {
            var result = new SearchResponse();

            if (string.IsNullOrEmpty(request.Query))
            {
                return(result);
            }
            result.Results = new List <Entry>();

            var payers    = payerManager.All();
            var searchers = new List <IndexSearcher>();

            foreach (var directoryPath in System.IO.Directory.GetDirectories(luceneDir))
            {
                var directoryName = Path.GetFileName(directoryPath);
                if (payers.Any(p => p.PayerId == directoryName))
                {
                    searchers.Add(new IndexSearcher(GetDirectory(directoryPath)));
                }
            }

            using (var searcher = new MultiSearcher(searchers.ToArray()))
            {
                var hitsLimit = short.MaxValue;
                using (var analyzer = new StandardAnalyzer(version))
                {
                    var query          = CreateQuery(request, analyzer);
                    var hits           = searcher.Search(query, hitsLimit).ScoreDocs;
                    var searchResponse = CreateSearchResponse(searcher, hits, request);
                    result.Results.AddRange(searchResponse);
                }
            }
            return(result);
        }
Example #3
0
        public void TestSpanRegexBug()
        {
            CreateRamDirectories();

            SpanRegexQuery srq   = new SpanRegexQuery(new Term("field", "a.*"));
            SpanRegexQuery stq   = new SpanRegexQuery(new Term("field", "b.*"));
            SpanNearQuery  query = new SpanNearQuery(new SpanQuery[] { srq, stq }, 6,
                                                     true);

            // 1. Search the same store which works
            IndexSearcher[] arrSearcher = new IndexSearcher[2];
            arrSearcher[0] = new IndexSearcher(indexStoreA, true);
            arrSearcher[1] = new IndexSearcher(indexStoreB, true);
            MultiSearcher searcher = new MultiSearcher(arrSearcher);
            int           numHits  = searcher.Search(query, null, 1000).TotalHits;

            arrSearcher[0].Close();
            arrSearcher[1].Close();

            // Will fail here
            // We expect 2 but only one matched
            // The rewriter function only write it once on the first IndexSearcher
            // So it's using term: a1 b1 to search on the second IndexSearcher
            // As a result, it won't match the document in the second IndexSearcher
            Assert.AreEqual(2, numHits);
            indexStoreA.Close();
            indexStoreB.Close();
        }
Example #4
0
        /// <summary>
        /// 根据多个索引文件夹搜索
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        private TopDocs MultiSearch(string keyword, string field)
        {
            TopDocs docs = null;
            int     n    = 20;//最多返回多少个结果

            SetOutput(string.Format("正在检索关键字:{0}", keyword));
            Searchable[] abs = new Searchable[2];
            abs[0] = new IndexSearcher(INDEX_STORE_PATH);
            abs[1] = new IndexSearcher(INDEX_STORE_PATH1);
            MultiSearcher searcher = new MultiSearcher(abs);//构造MultiSearcher

            try
            {
                QueryParser parser = new QueryParser(field, new StandardAnalyzer());
                Query       query  = parser.Parse(keyword);
                Stopwatch   watch  = new Stopwatch();
                watch.Start();
                docs = searcher.Search(query, (Filter)null, n); //排序获取搜索结果
                watch.Stop();
                StringBuffer sb = "搜索完成,共用时:" + watch.Elapsed.Hours + "时 " + watch.Elapsed.Minutes + "分 " + watch.Elapsed.Seconds + "秒 " + watch.Elapsed.Milliseconds + "毫秒";
                SetOutput(sb);
            }
            catch (Exception ex)
            {
                SetOutput(ex.Message);
                docs = null;
            }
            return(docs);
        }
Example #5
0
        public static int GetCount(string type, string channelid, string classid, string year, string keywords, string groupname, out Dictionary <string, int> groupAggregate)
        {
            if (keywords.Length == 0)
            {
                keywords = "jUmBoT";
            }
            DateTime arg_15_0 = DateTime.Now;

            string[] array = type.Split(new char[]
            {
                ','
            });
            int num = array.Length;

            IndexSearcher[] array2 = new IndexSearcher[num];
            for (int i = 0; i < num; i++)
            {
                array2[i] = new IndexSearcher(HttpContext.Current.Server.MapPath("~/data/index/" + array[i] + "/"));
            }
            MultiSearcher multiSearcher = new MultiSearcher(array2);
            BooleanQuery  booleanQuery  = new BooleanQuery();

            if (channelid != "0")
            {
                Term      t     = new Term("channelid", channelid);
                TermQuery query = new TermQuery(t);
                booleanQuery.Add(query, BooleanClause.Occur.MUST);
            }
            if (Validator.StrToInt(year, 0) > 1900)
            {
                Term      t2     = new Term("year", year);
                TermQuery query2 = new TermQuery(t2);
                booleanQuery.Add(query2, BooleanClause.Occur.MUST);
            }
            string[] fields = new string[]
            {
                "title",
                "tags",
                "summary",
                "content",
                "fornull"
            };
            MultiFieldQueryParser multiFieldQueryParser = new MultiFieldQueryParser(fields, new StandardAnalyzer());
            Query query3 = multiFieldQueryParser.Parse(keywords);

            booleanQuery.Add(query3, BooleanClause.Occur.MUST);
            Hits hits = multiSearcher.Search(booleanQuery);

            if (num == 1)
            {
                groupAggregate = SimpleFacets.Facet(booleanQuery, array2[0], groupname);
            }
            else
            {
                groupAggregate = null;
            }
            return(hits.Length());
        }
Example #6
0
 private void PrintScores(TopDocs docs, int startIndex, int endIndex, MultiSearcher searcher)
 {
     ScoreDoc[] scoreDocs = docs.ScoreDocs;
     for (int i = startIndex; i < endIndex && i < scoreDocs.Count(); i++)
     {
         int      docId = scoreDocs[i].Doc;
         Document doc   = searcher.Doc(docId);
         logger.Info(string.Format("{0}的分值为{1}", doc.Get("productid"), scoreDocs[i].Score));
     }
 }
Example #7
0
        /// <summary>
        /// Perform search
        /// </summary>
        /// <param name="query"></param>
        /// <param name="startIndex"></param>
        /// <param name="blockSize"></param>
        /// <param name="indexDirEs"></param>
        /// <param name="indexDirEn"></param>
        /// <param name="sortBy"></param>
        /// <returns></returns>
        public List <IssueDocument> MedesSearch(Query query, int startIndex, int blockSize, Directory indexDirEs, Directory indexDirEn, Directory indexDirHe, string sortBy)
        {
#if DEBUG
            T.TraceMessage(string.Format("Begin search , query: '{0}'", query.ToString()));
#endif

            List <IssueDocument> result = new List <IssueDocument>();
            try
            {
                // build a multi searcher across the 2 indexes
                MultiSearcher mSearcher = CombineSearchers(indexDirEs, indexDirEn, indexDirHe);

                TopDocs tDocs       = null;
                int     iterateLast = startIndex + blockSize;


                string           customScoreField = "article_id";
                FieldScoreQuery  dateBooster      = new FieldScoreQuery(customScoreField, FieldScoreQuery.Type.FLOAT);
                CustomScoreQuery customQuery      = new CustomScoreQuery(query, dateBooster);

                tDocs = mSearcher.Search(customQuery, 1000);
                //ScoreDoc[] hits = tpDcs.scoreDocs;
                if (startIndex + blockSize > tDocs.TotalHits)
                {
                    iterateLast = tDocs.TotalHits;
                }

                for (int i = startIndex; i < iterateLast; i++)
                {
                    // Document hitDoc = mSearcher.Doc(hits[i].doc);

                    Document hitDoc = mSearcher.Doc(i);

                    result.Add(new IssueDocument()
                    {
                        Id = Int32.Parse(hitDoc.Get("issue_id").ToString())
                    });
                }

                // close the searcher and indexes
                mSearcher.Dispose();
                indexDirEs.Dispose();
                indexDirEn.Dispose();
                indexDirHe.Dispose();
            }
            catch (Exception ex)
            {
                T.TraceError("Error MedesSearch, query '{0}'", query.ToString());
                T.TraceError(ex);
                throw ex;
            }
            return(result);
        }
        public void MultiSearcher_Sort_By_Int_Test()
        {
            var multiSearcher = new MultiSearcher(
                new Searchable[] { _searcher1, _searcher2 });

            var result = Search(multiSearcher, _query, new Sort(new SortField(IdFieldName, SortField.INT)));

            var expected = new[] { 10, 20, 30, 40, 50 };

            CollectionAssert.AreEqual(expected, result);

            multiSearcher.Dispose();
        }
        public void MultiSearcher_Sort_By_String_Test()
        {
            var multiSearcher = new MultiSearcher(
                new Searchable[] { _searcher1, _searcher2 });

            var result = Search(multiSearcher, _query, new Sort(new SortField(NameFieldName, SortField.STRING)));

            // Anders, Andreas, Anja, Anne
            var expected = new[] { 50, 10, 40, 20, 30 };

            CollectionAssert.AreEqual(expected, result);

            multiSearcher.Dispose();
        }
Example #10
0
        private MultiSearcher CombineSearchers(Directory indexDirEs, Directory indexDirEn, Directory indexDirHe)
        {
            IndexSearcher iSearcherEs = new IndexSearcher(indexDirEs, true); // read-only=true
            IndexSearcher iSearcherEn = new IndexSearcher(indexDirEn, true); // read-only=true
            IndexSearcher iSearcherHe = new IndexSearcher(indexDirHe, true); // read-only=true


            Searcher[] listSearchers = new Searcher[3];
            listSearchers[0] = iSearcherEs;
            listSearchers[1] = iSearcherEn;
            listSearchers[2] = iSearcherHe;
            MultiSearcher mSearcher = new MultiSearcher(listSearchers);

            return(mSearcher);
        }
        /// <summary>
        /// 搜索文档内容且精确匹配
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public List <Doc> SearchDocumentContentAndNotSplitWord(string text)
        {
            List <Doc> result = new List <Doc>();

            try
            {
                bool isExistIndex = IndexReader.IndexExists(this._fsDir);

                if (isExistIndex)
                {
                    this._indexReader = IndexReader.Open(this._fsDir, false);
                    IndexSearcher searcher = new IndexSearcher(this._indexReader);

                    //搜索条件
                    BooleanQuery queryOr1 = new BooleanQuery();
                    PhraseQuery  query1   = new PhraseQuery();

                    foreach (string word in SplitContent.SplitWords(text))
                    {
                        query1.Add(new Term(DocStruct.CONTENT, word));
                        queryOr1.Add(query1, BooleanClause.Occur.MUST);
                    }

                    MultiSearcher multiSearch = new MultiSearcher(new[] { searcher });

                    //TopScoreDocCollector盛放查询结果的容器
                    TopScoreDocCollector collector = TopScoreDocCollector.create(300, true);
                    multiSearch.Search(queryOr1, collector);

                    ScoreDoc[] docs = collector.TopDocs(0, collector.GetTotalHits()).scoreDocs.OrderByDescending(t => t.score).ToArray();
                    for (int i = 0; i < docs.Length; i++)
                    {
                        int      docId = docs[i].doc;         //得到查询结果文档的id(Lucene内部分配的id)
                        Document doc   = searcher.Doc(docId); //根据文档id来获得文档对象Document
                        var      d     = new Doc();
                        d.Id = doc.Get(DocStruct.ID);
                        result.Add(d);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteError(this.GetType(), ex);
            }

            return(result);
        }
Example #12
0
        public static int GetCount(string type, string channelid, string classid, string year, string keywords, string groupname, out Dictionary <string, int> groupAggregate)
        {
            if (keywords.Length == 0)
            {
                keywords = "jUmBoT";
            }
            DateTime now = DateTime.Now;

            string[] strArray = type.Split(',');
            int      length   = strArray.Length;

            IndexSearcher[] indexSearcherArray = new IndexSearcher[length];
            for (int index = 0; index < length; ++index)
            {
                indexSearcherArray[index] = new IndexSearcher(HttpContext.Current.Server.MapPath("~/data/index/" + strArray[index] + "/"));
            }
            MultiSearcher multiSearcher = new MultiSearcher((Searchable[])indexSearcherArray);
            BooleanQuery  booleanQuery  = new BooleanQuery();

            if (channelid != "0")
            {
                TermQuery termQuery = new TermQuery(new Term(nameof(channelid), channelid));
                booleanQuery.Add((Query)termQuery, BooleanClause.Occur.MUST);
            }
            if (Validator.StrToInt(year, 0) > 1900)
            {
                TermQuery termQuery = new TermQuery(new Term(nameof(year), year));
                booleanQuery.Add((Query)termQuery, BooleanClause.Occur.MUST);
            }
            Query query = new MultiFieldQueryParser(new string[5]
            {
                "title",
                "tags",
                "summary",
                "content",
                "fornull"
            }, (Analyzer) new StandardAnalyzer()).Parse(keywords);

            booleanQuery.Add(query, BooleanClause.Occur.MUST);
            Hits hits = multiSearcher.Search((Query)booleanQuery);

            groupAggregate = length != 1 ? (Dictionary <string, int>)null : SimpleFacets.Facet((Query)booleanQuery, indexSearcherArray[0], groupname);
            return(hits.Length());
        }
        public AutoCompletionResult Autocomplete(string text, bool includeExplanation = false)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return(AutoCompletionResult.NoResult(text));
            }

            var searchers = _directoryFactory.GetAllDirectories().Select(d =>
            {
                try
                {
                    return(new IndexSearcher(d, true));
                }
                catch (Exception e)
                {
                    _log.Error(e, "While searching directory {0}", d);
                    return(null);
                }
            })
                            .Where(s => s != null)
                            .ToArray();

            using (var searcher = new MultiSearcher(searchers))
            {
                try
                {
                    BooleanQuery query = GetQueryForText(text);

                    var results  = searcher.Search(query, 10);
                    var commands = results.ScoreDocs
                                   .Select(d =>
                    {
                        var document = searcher.Doc(d.Doc);
                        try
                        {
                            Explanation explanation = null;
                            if (includeExplanation)
                            {
                                explanation = searcher.Explain(query, d.Doc);
                            }
                            var coreDoc = CoreDocument.Rehydrate(document);
                            var command = _converterRepository.FromDocumentToItem(coreDoc);

                            return(new AutoCompletionResult.CommandResult(command, coreDoc.GetDocumentId(), explanation));
                        }
                        catch (Exception e)
                        {
                            _log.Error(e, "Error getting command result for document {0}:{1}",
                                       document.GetField(SpecialFields.ConverterId).StringValue,
                                       document.GetField(SpecialFields.Id).StringValue);
                            return(null);
                        }
                    })
                                   .Where(r => r != null);
                    return(AutoCompletionResult.OrderedResult(text, commands));
                }
                catch (ParseException e)
                {
                    _log.Error(e, "Error parsing '{0}'", text);
                    return(AutoCompletionResult.NoResult(text));
                }
            }
        }
        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public List <Doc> Search(string text)
        {
            List <Doc> result = new List <Doc>();

            try
            {
                bool isExistIndex = IndexReader.IndexExists(this._fsDir);

                if (isExistIndex)
                {
                    this._indexReader = IndexReader.Open(this._fsDir, false);
                    IndexSearcher searcher = new IndexSearcher(this._indexReader);

                    //搜索条件
                    BooleanQuery shouldQuery   = new BooleanQuery();
                    BooleanQuery titleQuery    = new BooleanQuery();
                    BooleanQuery contentQuery  = new BooleanQuery();
                    BooleanQuery categoryQuery = new BooleanQuery();

                    //把用户输入的关键字进行分词
                    foreach (string word in SplitContent.SplitWords(text))
                    {
                        PhraseQuery query1 = new PhraseQuery();
                        PhraseQuery query2 = new PhraseQuery();
                        PhraseQuery query3 = new PhraseQuery();
                        query1.Add(new Term(DocStruct.TITLE, word));
                        query2.Add(new Term(DocStruct.CONTENT, word));
                        query3.Add(new Term(DocStruct.CATEGORY, word));

                        // 引号括起来的词语,必须出现
                        if (Regex.IsMatch(text, "^\".*\"$"))
                        {
                            titleQuery.Add(query1, BooleanClause.Occur.MUST);
                            contentQuery.Add(query2, BooleanClause.Occur.MUST);
                            categoryQuery.Add(query3, BooleanClause.Occur.MUST);
                        }
                        else
                        {
                            titleQuery.Add(query1, BooleanClause.Occur.SHOULD);
                            contentQuery.Add(query2, BooleanClause.Occur.SHOULD);
                            categoryQuery.Add(query3, BooleanClause.Occur.SHOULD);
                        }
                    }

                    shouldQuery.Add(titleQuery, BooleanClause.Occur.SHOULD);
                    shouldQuery.Add(contentQuery, BooleanClause.Occur.SHOULD);
                    shouldQuery.Add(categoryQuery, BooleanClause.Occur.SHOULD);

                    MultiSearcher multiSearch = new MultiSearcher(new[] { searcher });

                    //TopScoreDocCollector盛放查询结果的容器
                    TopScoreDocCollector collector = TopScoreDocCollector.create(300, true);

                    multiSearch.Search(shouldQuery, collector);

                    //TopDocs 指定0到GetTotalHits() 即所有查询结果中的文档 如果TopDocs(20,10)则意味着获取第20-30之间文档内容 达到分页的效果
                    ScoreDoc[] docs = collector.TopDocs(0, collector.GetTotalHits()).scoreDocs.OrderByDescending(t => t.score).ToArray();
                    for (int i = 0; i < docs.Length; i++)
                    {
                        int      docId = docs[i].doc;         //得到查询结果文档的id(Lucene内部分配的id)
                        Document doc   = searcher.Doc(docId); //根据文档id来获得文档对象Document
                        var      d     = new Doc();
                        d.Id = doc.Get(DocStruct.ID);
                        result.Add(d);
                    }
                    //LogHelper.WriteInfo(this.GetType(), string.Format("Searched results count:{0}", docs.Length));
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteError(this.GetType(), ex);
            }

            return(result);
        }
Example #15
0
        /// <summary>
        /// 分布检索
        /// </summary>
        /// <param name="type">多个用,隔开</param>
        /// <param name="ccid"></param>
        /// <param name="keywords">已经使用分词工具处理过</param>
        /// <param name="pageLen"></param>
        /// <param name="pageNo"></param>
        /// <param name="recCount"></param>
        /// <param name="eventTime"></param>
        /// <returns></returns>
        public static List <SearchItem> Search(string type, string ccid, String keywords, int pageLen, int pageNo, out int recCount, out double eventTime)
        {
            DateTime start = DateTime.Now;

            string[] types     = type.Split(',');
            int      _type_num = types.Length;

            IndexSearcher[] searchers = new IndexSearcher[_type_num];
            for (int i = 0; i < _type_num; i++)
            {
                searchers[i] = new IndexSearcher(HttpContext.Current.Server.MapPath("~/_data/index/" + types[i] + "/"));
            }
            MultiSearcher search = new MultiSearcher(searchers);

            BooleanQuery bq = new BooleanQuery();

            if (ccid != "0")
            {
                Term Term_channel = new Term("channelid", ccid);
                var  termQuery    = new TermQuery(Term_channel);
                bq.Add(termQuery, BooleanClause.Occur.MUST);
            }

            string[] fields = { "title", "tags", "summary" };
            MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, new StandardAnalyzer());//要查询的字段
            Query query = parser.Parse(keywords);

            bq.Add(query, BooleanClause.Occur.MUST);//添加到条件
            Hits              hits     = search.Search(bq);
            TimeSpan          duration = DateTime.Now - start;
            List <SearchItem> result   = new List <SearchItem>();

            recCount  = hits.Length();
            eventTime = duration.TotalMilliseconds;
            if (recCount > 0)
            {
                int i = (pageNo - 1) * pageLen;
                while (i < recCount && result.Count < pageLen)
                {
                    SearchItem news = null;
                    try
                    {
                        news           = new SearchItem();
                        news.Id        = hits.Doc(i).Get("id");
                        news.ChannelId = hits.Doc(i).Get("channelid");
                        news.TableName = hits.Doc(i).Get("tablename");
                        news.Title     = hits.Doc(i).Get("title");
                        news.Summary   = hits.Doc(i).Get("summary");
                        news.Tags      = hits.Doc(i).Get("tags");
                        news.Url       = hits.Doc(i).Get("url");
                        news.AddDate   = hits.Doc(i).Get("adddate");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                    finally
                    {
                        result.Add(news);
                        i++;
                    }
                }
                search.Close();
                return(result);
            }
            else
            {
                return(null);
            }
        }
Example #16
0
        public List <CodeIndex> Search(string text, string language, int size = 300)
        {
            List <CodeIndex> result = new List <CodeIndex>();

            try
            {
                bool isExistIndex = this._codeIndexLucene.IndexExists();

                if (isExistIndex && !text.IsNullOrEmpty())
                {
                    IndexSearcher searcher = new IndexSearcher(this._codeIndexLucene.OpenReader());

                    BooleanQuery allQuery = new BooleanQuery();
                    allQuery.Add(new TermQuery(new Term(IndexField.Language, language)), BooleanClause.Occur.MUST);

                    //搜索条件
                    BooleanQuery shouldQuery = new BooleanQuery();

                    //把用户输入的关键字进行分词
                    foreach (string word in SplitContent.SplitWords(text))
                    {
                        PhraseQuery query1 = new PhraseQuery();
                        query1.Add(new Term(IndexField.SearchText, word));
                        shouldQuery.Add(query1, BooleanClause.Occur.SHOULD);
                    }

                    allQuery.Add(shouldQuery, BooleanClause.Occur.MUST);

                    MultiSearcher        multiSearch = new MultiSearcher(new[] { searcher });
                    TopScoreDocCollector collector   = TopScoreDocCollector.create(size, true);
                    multiSearch.Search(allQuery, collector);

                    ScoreDoc[] docs = collector.TopDocs(0, collector.GetTotalHits()).scoreDocs.OrderByDescending(t => t.score).ToArray();
                    for (int i = 0; i < docs.Length; i++)
                    {
                        int      docId = docs[i].doc;         //得到查询结果文档的id(Lucene内部分配的id)
                        Document doc   = searcher.Doc(docId); //根据文档id来获得文档对象Document
                        var      m     = new CodeIndex
                        {
                            Id         = doc.Get(IndexField.Id),
                            SearchText = doc.Get(IndexField.SearchText),
                            CodeBody   = doc.Get(IndexField.CodeBody),
                            UserId     = doc.Get(IndexField.UserId),
                            Language   = doc.Get(IndexField.Language)
                        };

                        var codeDoc = this._codeFileLucene.GetDoc(m.FileId);
                        if (codeDoc != null)
                        {
                            m.FileContent = codeDoc.Get(nameof(CodeFile.FileContent));
                        }

                        result.Add(m);

                        // 当完全匹配时,只返回此条
                        if (text != null && m.SearchText != null && m.SearchText.Trim().ToLower() == text.Trim().ToLower())
                        {
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteError(this.GetType(), ex);
            }

            return(result);
        }
        /// <summary>
        /// 多索引文件联合查询搜索(带分页)
        /// </summary>
        /// <typeparam name="T">返回结果数据类型</typeparam>
        /// <param name="indexPaths">索引文件路径集合</param>
        /// <param name="query">Query</param>
        /// <param name="sort">排序</param>
        /// <param name="pageIndex">当前搜索页</param>
        /// <param name="pageSize">每页显示数</param>
        /// <param name="count">总搜索结果数</param>
        /// <returns></returns>
        public static List <T> Search <T>(string[] indexPaths, Query query, Sort sort, int pageIndex, int pageSize, out int count) where T : BaseIndexModel
        {
            count = 0;

            if (null == query)
            {
                return(null);
            }

            if (pageIndex < 1)
            {
                pageIndex = 1;
            }

            if (pageSize < 1)
            {
                pageSize = 1;
            }

            //起始搜索位置
            int start = (pageIndex - 1) * pageSize;

            if (null == indexPaths || indexPaths.Length < 1)
            {
                return(null);
            }

            List <IndexSearcher> searchers = new List <IndexSearcher>();

            foreach (var indexPath in indexPaths)
            {
                if (string.IsNullOrWhiteSpace(indexPath))
                {
                    continue;
                }

                //打开索引文件
                FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NoLockFactory());

                if (null == directory)
                {
                    continue;
                }

                //检测索引文件是否存在
                bool isExist = IndexReader.IndexExists(directory);

                if (!isExist)
                {
                    continue;
                }

                //创建一个只读的索引文件读取实例
                IndexReader reader = IndexReader.Open(directory, true);

                //实例化IndexSearcher搜索器
                IndexSearcher searcher = new IndexSearcher(reader);

                searchers.Add(searcher);
            }

            if (searchers.Count < 1)
            {
                return(null);
            }

            MultiSearcher multiSearcher = new MultiSearcher(searchers.ToArray());

            //Collector
            TopFieldCollector results = TopFieldCollector.Create(sort, start + pageSize, false, false, false, false);

            //搜索
            multiSearcher.Search(query, results);

            //总命中率(精算)
            count = results.TotalHits;

            //获取当前页文档
            var docs = results.TopDocs(start, pageSize).ScoreDocs;

            //定义一个结果返回变量
            List <T> list = new List <T>();

            //遍历当前页文档,并转换为需要的结果类型
            foreach (var scoreDoc in docs)
            {
                Document doc = multiSearcher.Doc(scoreDoc.Doc);

                var data = new IndexFactory(doc).Result as T;

                if (null != data)
                {
                    list.Add(data);
                }
            }

            return(list);
        }
Example #18
0
        public static List <SearchItem> Search(string type, string channelid, string classid, string year, string keywords, int pageLen, int pageNo, out int recCount, out double eventTime)
        {
            if (keywords.Length == 0)
            {
                keywords = "jUmBoT";
            }
            DateTime now = DateTime.Now;

            string[] strArray = type.Split(',');
            int      length   = strArray.Length;

            IndexSearcher[] indexSearcherArray = new IndexSearcher[length];
            for (int index = 0; index < length; ++index)
            {
                indexSearcherArray[index] = new IndexSearcher(HttpContext.Current.Server.MapPath("~/data/index/" + strArray[index] + "/"));
            }
            MultiSearcher multiSearcher = new MultiSearcher((Searchable[])indexSearcherArray);
            BooleanQuery  booleanQuery  = new BooleanQuery();

            if (channelid != "0")
            {
                TermQuery termQuery = new TermQuery(new Term(nameof(channelid), channelid));
                booleanQuery.Add((Query)termQuery, BooleanClause.Occur.MUST);
            }
            if (Validator.StrToInt(year, 0) > 1900)
            {
                TermQuery termQuery = new TermQuery(new Term(nameof(year), year));
                booleanQuery.Add((Query)termQuery, BooleanClause.Occur.MUST);
            }
            Query query = new MultiFieldQueryParser(new string[5]
            {
                "title",
                "tags",
                "summary",
                "content",
                "fornull"
            }, (Analyzer) new StandardAnalyzer()).Parse(keywords);

            booleanQuery.Add(query, BooleanClause.Occur.MUST);
            Sort sort = new Sort(new SortField((string)null, 1, true));
            Hits hits = multiSearcher.Search((Query)booleanQuery, sort);
            List <SearchItem> searchItemList = new List <SearchItem>();

            recCount = hits.Length();
            if (recCount > 0)
            {
                int n = (pageNo - 1) * pageLen;
                while (n < recCount && searchItemList.Count < pageLen)
                {
                    SearchItem searchItem = (SearchItem)null;
                    try
                    {
                        searchItem           = new SearchItem();
                        searchItem.Id        = hits.Doc(n).Get("id");
                        searchItem.ChannelId = hits.Doc(n).Get(nameof(channelid));
                        searchItem.ClassId   = hits.Doc(n).Get(nameof(classid));
                        searchItem.TableName = hits.Doc(n).Get("tablename");
                        searchItem.Img       = hits.Doc(n).Get("img");
                        searchItem.Title     = hits.Doc(n).Get("title");
                        searchItem.Summary   = hits.Doc(n).Get("summary");
                        searchItem.Tags      = hits.Doc(n).Get("tags");
                        searchItem.Url       = hits.Doc(n).Get("url");
                        searchItem.AddDate   = hits.Doc(n).Get("adddate");
                        searchItem.Year      = hits.Doc(n).Get(nameof(year));
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    finally
                    {
                        searchItemList.Add(searchItem);
                        ++n;
                    }
                }
                multiSearcher.Close();
                TimeSpan timeSpan = DateTime.Now - now;
                eventTime = (double)Convert.ToInt16(timeSpan.TotalMilliseconds);
                return(searchItemList);
            }
            TimeSpan timeSpan1 = DateTime.Now - now;

            eventTime = (double)Convert.ToInt16(timeSpan1.TotalMilliseconds);
            return((List <SearchItem>)null);
        }
Example #19
0
        public void TestMultiSearcher()
        {
            // setup index 1
            RAMDirectory ramDir1 = new RAMDirectory();
            IndexWriter writer1 = new IndexWriter(ramDir1, new StandardAnalyzer(TEST_VERSION), true,
                                                  IndexWriter.MaxFieldLength.UNLIMITED);
            Document d = new Document();
            Field f = new Field(FIELD_NAME, "multiOne", Field.Store.YES, Field.Index.ANALYZED);
            d.Add(f);
            writer1.AddDocument(d);
            writer1.Optimize();
            writer1.Close();
            IndexReader reader1 = IndexReader.Open(ramDir1, true);

            // setup index 2
            RAMDirectory ramDir2 = new RAMDirectory();
            IndexWriter writer2 = new IndexWriter(ramDir2, new StandardAnalyzer(TEST_VERSION), true,
                                                  IndexWriter.MaxFieldLength.UNLIMITED);
            d = new Document();
            f = new Field(FIELD_NAME, "multiTwo", Field.Store.YES, Field.Index.ANALYZED);
            d.Add(f);
            writer2.AddDocument(d);
            writer2.Optimize();
            writer2.Close();
            IndexReader reader2 = IndexReader.Open(ramDir2, true);

            var searchers = new IndexSearcher[2];
            searchers[0] = new IndexSearcher(ramDir1, true);
            searchers[1] = new IndexSearcher(ramDir2, true);
            MultiSearcher multiSearcher = new MultiSearcher(searchers);
            QueryParser parser = new QueryParser(TEST_VERSION, FIELD_NAME, new StandardAnalyzer(TEST_VERSION));
            parser.MultiTermRewriteMethod = MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE;
            query = parser.Parse("multi*");
            Console.WriteLine("Searching for: " + query.ToString(FIELD_NAME));
            // at this point the multisearcher calls combine(query[])
            hits = multiSearcher.Search(query, null, 1000);

            // query = QueryParser.Parse("multi*", FIELD_NAME, new StandardAnalyzer(TEST_VERSION));
            Query[] expandedQueries = new Query[2];
            expandedQueries[0] = query.Rewrite(reader1);
            expandedQueries[1] = query.Rewrite(reader2);
            query = query.Combine(expandedQueries);

            // create an instance of the highlighter with the tags used to surround
            // highlighted text
            Highlighter highlighter = new Highlighter(this, new QueryTermScorer(query));

            for (int i = 0; i < hits.TotalHits; i++)
            {
                String text = multiSearcher.Doc(hits.ScoreDocs[i].Doc).Get(FIELD_NAME);
                TokenStream tokenStream = analyzer.TokenStream(FIELD_NAME, new StringReader(text));
                String highlightedText = highlighter.GetBestFragment(tokenStream, text);
                Console.WriteLine(highlightedText);
            }
            Assert.IsTrue(numHighlights == 2, "Failed to find correct number of highlights " + numHighlights + " found");

        }
Example #20
0
        public static List <SearchItem> Search(string type, string channelid, string classid, string year, string keywords, int pageLen, int pageNo, out int recCount, out double eventTime)
        {
            if (keywords.Length == 0)
            {
                keywords = "jUmBoT";
            }
            DateTime now = DateTime.Now;

            string[] array = type.Split(new char[]
            {
                ','
            });
            int num = array.Length;

            IndexSearcher[] array2 = new IndexSearcher[num];
            for (int i = 0; i < num; i++)
            {
                array2[i] = new IndexSearcher(HttpContext.Current.Server.MapPath("~/data/index/" + array[i] + "/"));
            }
            MultiSearcher multiSearcher = new MultiSearcher(array2);
            BooleanQuery  booleanQuery  = new BooleanQuery();

            if (channelid != "0")
            {
                Term      t     = new Term("channelid", channelid);
                TermQuery query = new TermQuery(t);
                booleanQuery.Add(query, BooleanClause.Occur.MUST);
            }
            if (Validator.StrToInt(year, 0) > 1900)
            {
                Term      t2     = new Term("year", year);
                TermQuery query2 = new TermQuery(t2);
                booleanQuery.Add(query2, BooleanClause.Occur.MUST);
            }
            string[] fields = new string[]
            {
                "title",
                "tags",
                "summary",
                "content",
                "fornull"
            };
            MultiFieldQueryParser multiFieldQueryParser = new MultiFieldQueryParser(fields, new StandardAnalyzer());
            Query query3 = multiFieldQueryParser.Parse(keywords);

            booleanQuery.Add(query3, BooleanClause.Occur.MUST);
            Sort sort = new Sort(new SortField(null, 1, true));
            Hits hits = multiSearcher.Search(booleanQuery, sort);
            List <SearchItem> list = new List <SearchItem>();

            recCount = hits.Length();
            if (recCount > 0)
            {
                int num2 = (pageNo - 1) * pageLen;
                while (num2 < recCount && list.Count < pageLen)
                {
                    SearchItem searchItem = null;
                    try
                    {
                        searchItem           = new SearchItem();
                        searchItem.Id        = hits.Doc(num2).Get("id");
                        searchItem.ChannelId = hits.Doc(num2).Get("channelid");
                        searchItem.ClassId   = hits.Doc(num2).Get("classid");
                        searchItem.TableName = hits.Doc(num2).Get("tablename");
                        searchItem.Img       = hits.Doc(num2).Get("img");
                        searchItem.Title     = hits.Doc(num2).Get("title");
                        searchItem.Summary   = hits.Doc(num2).Get("summary");
                        searchItem.Tags      = hits.Doc(num2).Get("tags");
                        searchItem.Url       = hits.Doc(num2).Get("url");
                        searchItem.AddDate   = hits.Doc(num2).Get("adddate");
                        searchItem.Year      = hits.Doc(num2).Get("year");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    finally
                    {
                        list.Add(searchItem);
                        num2++;
                    }
                }
                multiSearcher.Close();
                eventTime = (double)((float)Convert.ToInt16((DateTime.Now - now).TotalMilliseconds));
                return(list);
            }
            eventTime = (double)((float)Convert.ToInt16((DateTime.Now - now).TotalMilliseconds));
            return(null);
        }
Example #21
0
        public CardDescription[] Search2(string queryString, SortField[] sortFields)
        {
            try
            {
                if (queryString != null)
                {
                    queryString = queryString.Trim();
                }


                Query query = null;
                if (queryString == null || queryString == "")
                {
                    query = new MatchAllDocsQuery();
                }
                else
                {
                    BooleanQuery bquery = new BooleanQuery();
                    QueryParser  parser = new QueryParser("name", AnalyzerFactory.GetAnalyzer());
                    Query        q      = parser.Parse(queryString);
                    bquery.Add(q, BooleanClause.Occur.SHOULD);

                    parser = new QueryParser("oldName", AnalyzerFactory.GetAnalyzer());
                    q      = parser.Parse(queryString);
                    bquery.Add(q, BooleanClause.Occur.SHOULD);

                    parser = new QueryParser("shortName", AnalyzerFactory.GetAnalyzer());
                    q      = parser.Parse(queryString);
                    bquery.Add(q, BooleanClause.Occur.SHOULD);

                    parser = new QueryParser("japName", AnalyzerFactory.GetAnalyzer());
                    q      = parser.Parse(queryString);
                    bquery.Add(q, BooleanClause.Occur.SHOULD);

                    parser = new QueryParser("enName", AnalyzerFactory.GetAnalyzer());
                    q      = parser.Parse(queryString);
                    bquery.Add(q, BooleanClause.Occur.SHOULD);

                    query = bquery;
                }

                Searcher searcher = null;
                if (KeepInMemory)
                {
                    if (allowDIY && ramdiydir != null)
                    {
                        IndexSearcher[] indexsearchers = new IndexSearcher[2];
                        indexsearchers[0] = new IndexSearcher(ramdir);
                        indexsearchers[1] = new IndexSearcher(ramdiydir);
                        searcher          = new MultiSearcher(indexsearchers);
                    }
                    else
                    {
                        searcher = new IndexSearcher(ramdir);
                    }
                }
                else
                {
                    if (allowDIY && Directory.Exists(Path + "DIYCardIndex"))
                    {
                        IndexSearcher[] indexsearchers = new IndexSearcher[2];
                        indexsearchers[0] = new IndexSearcher(Path + "CardIndex");
                        indexsearchers[1] = new IndexSearcher(Path + "DIYCardIndex");
                        searcher          = new MultiSearcher(indexsearchers);
                    }
                    else
                    {
                        searcher = new IndexSearcher(Path + "CardIndex");
                    }
                }

                Hits hits = null;

                if (sortFields == null)
                {
                    hits = searcher.Search(query);
                }
                else
                {
                    hits = searcher.Search(query, new Sort(sortFields));
                }

                int length = hits.Length();
                CardDescription[] cards = new CardDescription[length];
                for (int i = 0; i < length; i++)
                {
                    Document doc = hits.Doc(i);
                    cards[i] = GetCardByID(int.Parse(doc.GetField("ID").StringValue()));
                }

                return(cards);
            }
            catch
            {
                return(new CardDescription[0]);
            }
        }