예제 #1
0
        /// <summary>
        /// 排序分词查询
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="reverse">倒序</param>
        /// <param name="start"></param>
        /// <param name="count"></param>
        public static SearchRet SearchOrder(Func <Query> query, SortField[] orders, int start = 0, int count = 100)
        {
            SearchRet searchRet = new SearchRet();
            Stopwatch sw        = new Stopwatch();

            sw.Restart();


            //IndexSearcher需要传递一个IndexReader对象

            var           reader   = LuceneFactory.GetIndexReader(Path);
            IndexSearcher searcher = LuceneFactory.getIndexSearcher(reader);

            //PhraseQuery用来进行多个关键词的检索,调用Add方法添加关键词
            var sort = new Sort();

            sort.SetSort(orders);

            var topdocs = searcher.Search(query(), null, MaxHits, sort);

            searchRet.Total = topdocs.totalHits;

            ScoreDoc[] docs = topdocs.scoreDocs;
            sw.Stop();
            searchRet.Cost        = sw.Elapsed.TotalMilliseconds;
            searchRet.SearchDatas = new List <SearchData>();
            for (int i = start; i < start + count; i++)
            {
                //取得文档的编号(主键)是Lucene.Net提供的
                if (i > searchRet.Total - 1)
                {
                    break;
                }
                int      docId  = docs[i].doc;
                Document doc    = searcher.Doc(docId);
                var      fileds = doc.GetFields();
                var      ret    = new SearchData();
                foreach (Field filed in fileds)
                {
                    var name = filed.Name();
                    var val  = filed.StringValue();

                    ret.Add(name, val);
                }
                searchRet.SearchDatas.Add(ret);
            }
            return(searchRet);
        }
예제 #2
0
        /// <summary>
        /// 条件查询, 可参考 http://blog.csdn.net/weizengxun/article/details/8101097
        /// </summary>
        /// <param name="fun"></param>
        /// <param name="start"></param>
        /// <param name="count"></param>
        public static SearchRet SearchCondition(Func <Query> fun, int start = 0, int count = 100)
        {
            SearchRet searchRet = new SearchRet();
            Stopwatch sw        = new Stopwatch();

            sw.Restart();
            //IndexSearcher需要传递一个IndexReader对象
            var           reader   = LuceneFactory.GetIndexReader(Path);
            IndexSearcher searcher = LuceneFactory.getIndexSearcher(reader); // new IndexSearcher(SearchEngineWapper._reader);

            //PhraseQuery用来进行多个关键词的检索,调用Add方法添加关键词
            var query = fun();

            // query.Add(query8, BooleanClause.Occur.SHOULD);
            //PhraseQuery. SetSlop(int slop)用来设置关键词之间的最大距离,默认是0,设置了Slop以后哪怕文档中两个关键词之间没有紧挨着也能找到。
            TopScoreDocCollector collector = TopScoreDocCollector.create(MaxHits, true);

            searcher.Search(query, null, collector);
            searchRet.Total       = collector.GetTotalHits();
            searchRet.SearchDatas = new List <SearchData>();
            ScoreDoc[] docs = collector.TopDocs(start, count).scoreDocs;
            sw.Stop();
            searchRet.Cost = sw.Elapsed.TotalMilliseconds;
            for (int i = 0; i < docs.Length; i++)
            {
                //取得文档的编号(主键)是Lucene.Net提供的
                int      docId  = docs[i].doc;
                Document doc    = searcher.Doc(docId);
                var      fileds = doc.GetFields();
                var      ret    = new SearchData();
                foreach (Field filed in fileds)
                {
                    var name = filed.Name();
                    var val  = filed.StringValue();

                    ret.Add(name, val);
                }
                searchRet.SearchDatas.Add(ret);
            }
            return(searchRet);
        }