Exemple #1
0
 public void Delete(int id, int type)
 {
     var model = new LuceneModel
     {
         ID = id,
         Type = type,
         IndexType = LuceneType.Delete
     };
     LuceneModels.Enqueue(model);
 }
Exemple #2
0
        public List<LuceneModel> GetList(string keyword,int? type, int pageIndex, int pageSize, out int dataCount)
        {
            dataCount = 0;
            var result = new List<LuceneModel>();
            try
            {
                var ky = keyword;
                IndexSearcher searcher;
                searcher = new IndexSearcher(FSDirectory.Open(new DirectoryInfo(LucenePath)), true);
                var bq = new BooleanQuery();
                if (!string.IsNullOrEmpty(keyword))
                {
                    keyword = SplitContent.SplitWords(keyword, new PanGuTokenizer());
                    var queryParser = new QueryParser("content", new PanGuAnalyzer(true));
                    var query = queryParser.Parse(keyword);
                    var titleQueryParser = new QueryParser("title", new PanGuAnalyzer(true));
                    var titleQuery = titleQueryParser.Parse(keyword);
                    bq.Add(query, BooleanClause.Occur.SHOULD);
                    //表示条件关系为“or”,BooleanClause.Occur.MUST表示“and”,BooleanClause.Occur.MUST_NOT表示“not”
                    bq.Add(titleQuery, BooleanClause.Occur.SHOULD);
                }
                else
                {
                    Query query = new WildcardQuery(new Term("title", "*"));
                    bq.Add(query, BooleanClause.Occur.SHOULD);
                }
                if (type.HasValue)
                {
                    Query query = new WildcardQuery(new Term("type", type.Value.ToString()));
                    bq.Add(query, BooleanClause.Occur.SHOULD);
                }

                var sort = new Sort(new SortField("createtime", SortField.INT, true));
                var docs = searcher.Search(bq, null, pageSize * 1000, sort);

                //var collector = TopScoreDocCollector.create(1000, true);
                //searcher.Search(bq, null, collector);
                //dataCount = collector.GetTotalHits();//返回总条数
                //var docs = collector.TopDocs((pageIndex - 1) * pageSize, pageSize).scoreDocs;//取前十条数据  可以通过它实现搜索结果分页

                ////创建一个结果收集器(收集结果最大数为1000页)
                //TopScoreDocCollector collector = TopScoreDocCollector.create(pageSize * 1000, true);
                //searcher.Search(bq, null, collector);
                //TopDocs topDoc = collector.TopDocs(0, collector.GetTotalHits());
                ////搜索结果总数超出指定收集器大小,则摈弃
                //if (topDoc.totalHits > pageSize * 1000)
                //    recCount = pageSize * 1000;
                //else
                //    recCount = topDoc.totalHits;

                //搜索结果总数超出指定收集器大小,则摈弃
                if (docs.totalHits > pageSize * 1000)
                    dataCount = pageSize * 1000;
                else
                    dataCount = docs.totalHits;
                int currentCount = (pageIndex - 1) * pageSize;

                while (currentCount < dataCount && result.Count < pageSize)
                {
                    var model = new LuceneModel();
                    Document doc = searcher.Doc(docs.scoreDocs[currentCount].doc);

                    model.Content = doc.Get("content");
                    model.HightLightContent = SplitContent.HightLight(ky, doc.Get("content"));
                    model.Title = doc.Get("title");
                    model.HightLightTitle = SplitContent.HightLight(ky, doc.Get("title"));
                    model.ID = Convert.ToInt32(doc.Get("id"));
                    model.ClickCount = Convert.ToInt32(doc.Get("clickcount"));
                    model.Images = doc.Get("images");
                    model.Tags = doc.Get("tags");
                    model.Type = Convert.ToInt32(doc.Get("type"));
                    model.CreateTime = DateTimeExtension.UnixToDateTime(Convert.ToInt32(doc.Get("createtime")));
                    result.Add(model);

                    #region 循环中有try catch 非常消耗性能 解决方案 报错不处理
                    //try
                    //{
                    //    model.Content = doc.Get("content");
                    //    model.HightLightContent = SplitContent.HightLight(ky, doc.Get("content"));
                    //    model.Title = doc.Get("title");
                    //    model.HightLightTitle = SplitContent.HightLight(ky, doc.Get("title"));
                    //    model.ID = Convert.ToInt32(doc.Get("id"));
                    //    model.ClickCount = Convert.ToInt32(doc.Get("clickcount"));
                    //    model.Images = doc.Get("images");
                    //    model.Tags = doc.Get("tags");
                    //    model.Type = Convert.ToInt32(doc.Get("type"));
                    //    model.CreateTime = DateTimeExtension.UnixToDateTime(Convert.ToInt32(doc.Get("createtime")));
                    //    result.Add(model);
                    //}
                    //catch (Exception e)
                    //{
                    //    LoggerHelper.Logger(
                    //        string.Format("Lucene搜索错误:ID:{0},Type:{1},Title:{2},CreateTime:{3}", doc.Get("id"),
                    //            doc.Get("type"), doc.Get("title"), doc.Get("createtime")), e);
                    //}
                    //finally
                    //{
                    //    i++;
                    //}
                    #endregion

                    currentCount++;

                }
                return result;
            }
            catch (Exception ex)
            {
                throw new LuceneException.LuceneException("Lucene获取列表错误",ex);
            }
        }
Exemple #3
0
 public void Edit(LuceneModel model)
 {
     model.IndexType = LuceneType.Modify;
     LuceneModels.Enqueue(model);
 }
Exemple #4
0
 public void Add(LuceneModel model)
 {
     model.IndexType = LuceneType.Insert;
     LuceneModels.Enqueue(model);
 }
Exemple #5
0
 private void ModifyData(LuceneModel model, IndexWriter writer)
 {
     DeleteData(model, writer);
     InsertData(model,writer);
 }
Exemple #6
0
        private void InsertData(LuceneModel model, IndexWriter writer)
        {
            var document = new Document();//new一篇文档对象 --一条记录对应索引库中的一个文档

            //向文档中添加字段  Add(字段,值,是否保存字段原始值,是否针对该列创建索引)
            //--所有字段的值都将以字符串类型保存 因为索引库只存储字符串类型数据
            document.Add(new Field("id", model.ID.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            //Field.Store:表示是否保存字段原值。指定Field.Store.YES的字段在检索时才能用document.Get取出原值
            //Field.Index.NOT_ANALYZED:指定不按照分词后的结果保存--是否按分词后结果保存取决于是否对该列内容进行模糊查询
            document.Add(new NumericField("type", Field.Store.YES, true).SetIntValue(model.Type));
            document.Add(new Field("title", model.Title, Field.Store.YES, Field.Index.ANALYZED,
                Field.TermVector.WITH_POSITIONS_OFFSETS));
            //Field.Index.ANALYZED:指定文章内容按照分词后结果保存 否则无法实现后续的模糊查询
            //WITH_POSITIONS_OFFSETS:指示不仅保存分割后的词 还保存词之间的距离
            if (!string.IsNullOrEmpty(model.Content))
                document.Add(new Field("content", model.Content, Field.Store.YES, Field.Index.ANALYZED,
                    Field.TermVector.WITH_POSITIONS_OFFSETS));
            document.Add(
                new NumericField("createtime", Field.Store.YES, true).SetIntValue(
                    DateTimeExtension.DateTimeToUnix(model.CreateTime)));
            if (!string.IsNullOrEmpty(model.Images))
                document.Add(new Field("images", model.Images, Field.Store.YES, Field.Index.NO));
            if (!string.IsNullOrEmpty(model.Tags))
                document.Add(new Field("tags", model.Tags, Field.Store.YES, Field.Index.ANALYZED,
                    Field.TermVector.WITH_POSITIONS_OFFSETS));
            document.Add(new NumericField("clickcount", Field.Store.YES, true).SetIntValue(model.ClickCount));
            writer.AddDocument(document);
        }
Exemple #7
0
 private void DeleteData(LuceneModel model, IndexWriter writer)
 {
     writer.DeleteDocuments(new[] {new Term("id", model.ID.ToString()), new Term("type", model.Type.ToString())});
 }