예제 #1
0
 public void Edit(LuceneModel model)
 {
     model.Action = LuceneType.Modify;
     _luceneModels.Enqueue(model);
 }
예제 #2
0
 public void Add(LuceneModel model)
 {
     model.Action = LuceneType.Insert;
     _luceneModels.Enqueue(model);
 }
예제 #3
0
 public void Delete(int id,int type)
 {
     var model = new LuceneModel {ID = id, Type = type};
      model.Action = LuceneType.Delete;
     _luceneModels.Enqueue(model);
 }
예제 #4
0
        public List<LuceneModel> SearchFromIndexData(string q)
        {
            string keyword = q;
            var directory = FSDirectory.Open(new DirectoryInfo(LucenePath), new NoLockFactory());
            var reader = IndexReader.Open(directory, true);
            var searcher = new IndexSearcher(reader);

            ////--------------------------------------这里配置搜索条件
            ////PhraseQuery query = new PhraseQuery();
            ////foreach(string word in Common.SplitContent.SplitWords(Request.QueryString["SearchKey"])) {
            ////    query.Add(new Term("content", word));//这里是 and关系
            ////}
            ////query.SetSlop(100);

            ////关键词Or关系设置
            //var queryOr = new BooleanQuery();
            //TermQuery query;
            //foreach (var word in SplitContent.SplitWords(keyword))
            //{
            //    query = new TermQuery(new Term("title", word));
            //    queryOr.Add(query, BooleanClause.Occur.SHOULD);//这里设置 条件为Or关系
            //}
            ////--------------------------------------
            //var collector = TopScoreDocCollector.create(1000, true);
            ////searcher.Search(query, null, collector);
            //searcher.Search(queryOr, null, collector);

            q = SplitContent.SplitWords(q, new PanGuTokenizer());
            var queryParser = new QueryParser("content", new PanGuAnalyzer(true));
            var query = queryParser.Parse(q);
            var titleQueryParser = new QueryParser("title", new PanGuAnalyzer(true));
            var titleQuery = titleQueryParser.Parse(q);
            var bq = new BooleanQuery();
            bq.Add(query, BooleanClause.Occur.SHOULD);
            bq.Add(titleQuery, BooleanClause.Occur.SHOULD);
            var collector = TopScoreDocCollector.create(1000, true);
            searcher.Search(bq, null, collector);
            var totalCount = collector.GetTotalHits();//返回总条数
            var docs = collector.TopDocs(0, 10).scoreDocs;//取前十条数据  可以通过它实现LuceneNet搜索结果分页
            var result = new List<LuceneModel>();
            for (int i = 0; i < docs.Length; i++)
            {
                var docId = docs[i].doc;
                var doc = searcher.Doc(docId);
                var model = new LuceneModel();
                model.Content = doc.Get("content");
                model.HightLightContent = SplitContent.HightLight(keyword, doc.Get("content"));
                model.Title = doc.Get("title");
                model.HightLightTitle = SplitContent.HightLight(keyword, doc.Get("title"));
                model.ID = Convert.ToInt32(doc.Get("id"));
                result.Add(model);
            }
            return result;
        }