/// <summary>
        /// 获取
        /// </summary>
        /// <param name="bQuery"></param>
        /// <param name="maxNum">查询条数</param>
        private List <LiteNewsModel> GetSearchResult(BooleanQuery bQuery, Dictionary <string, string> dicKeywords, int maxNum = 100)
        {
            var           list   = new List <LiteNewsModel>();
            IndexSearcher search = new IndexSearcher(Direcotry, true);
            // Stopwatch stopwatch = Stopwatch.StartNew();
            //SortField构造函数第三个字段true为降序,false为升序
            Sort sort = new Sort(new SortField[] {
                SortField.FIELD_SCORE, new SortField("title", SortField.SCORE, true),
                SortField.FIELD_SCORE, new SortField("abstract", SortField.SCORE, true),
                SortField.FIELD_DOC, new SortField("content", SortField.DOC, true),
            });
            TopDocs docs = search.Search(bQuery, (Filter)null, maxNum, sort);

            if (docs != null)
            {
                var count = docs.TotalHits > maxNum ? maxNum : docs.TotalHits;
                for (int i = 0; i < count; i++)
                {
                    Document      doc   = search.Doc(docs.ScoreDocs[i].Doc);
                    LiteNewsModel model = new LiteNewsModel()
                    {
                        Title    = doc.Get("title").ToString(),
                        Abstract = doc.Get("abstract").ToString(),
                        BillCode = doc.Get("billCode").ToString(),
                        Url      = doc.Get("url").ToString(),
                        Content  = doc.Get("content").ToString(),
                        Time     = Convert.ToDateTime(doc.Get("addtime")),
                    };
                    list.Add(SetHighlighter(dicKeywords, model));
                }
            }
            return(list);
        }
        public void AddSingleIndex(LiteNewsModel model)
        {
            IndexWriter writer = new IndexWriter(Direcotry, PanGuAnalyzer, false, IndexWriter.MaxFieldLength.LIMITED);

            AddIndex(writer, model);
            //释放资源
            writer.Optimize();
            writer.Dispose();
        }
Пример #3
0
        public LiteNewsModel  GetModel(string billCode)
        {
            var model = new LiteNewsModel()
            {
                BillCode = "T20181102221",
                Abstract = "描述测试测试",
                Content  = "测试测试测试测试信息111内容",
                Time     = DateTime.Now,
                Title    = "测试数据第一条20122228",
                Url      = "https://www.test.com/test.html"
            };

            return(model);
        }
 /// <summary>
 /// 创建索引
 /// </summary>
 /// <param name="analyzer"></param>
 /// <param name="title"></param>
 /// <param name="content"></param>
 internal static void AddIndex(IndexWriter writer, LiteNewsModel model)
 {
     try
     {
         Document doc = new Document();
         //只有对需要全文检索的字段才ANALYZED
         doc.Add(new Field("billCode", model.BillCode, Field.Store.YES, Field.Index.NOT_ANALYZED));
         doc.Add(new Field("title", model.Title, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
         doc.Add(new Field("abstract", model.Abstract, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
         doc.Add(new Field("content", model.Content, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
         doc.Add(new Field("url", model.Url, Field.Store.YES, Field.Index.NOT_ANALYZED));
         doc.Add(new Field("time", model.Time.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
         writer.AddDocument(doc);
     }
     catch (FileNotFoundException fnfe)
     {
         throw fnfe;
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
        /// <summary>
        /// 设置关键字高亮
        /// </summary>
        /// <param name="dicKeywords">关键字列表</param>
        /// <param name="model">返回的数据模型</param>
        /// <returns></returns>
        private LiteNewsModel SetHighlighter(Dictionary <string, string> dicKeywords, LiteNewsModel model)
        {
            //return model;
            SimpleHTMLFormatter simpleHTMLFormatter = new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>");
            Highlighter         highlighter         = new PanGu.HighLight.Highlighter(simpleHTMLFormatter, new Segment())
            {
                FragmentSize = 50
            };
            string strTitle    = string.Empty;
            string strAbstract = string.Empty;

            dicKeywords.TryGetValue("title", out strTitle);
            dicKeywords.TryGetValue("abstract", out strAbstract);
            try
            {
                if (!string.IsNullOrEmpty(strTitle))
                {
                    var transStr = highlighter.GetBestFragment(strTitle, model.Title);
                    model.Title = string.IsNullOrEmpty(transStr) ? model.Title : transStr;
                }
                if (!string.IsNullOrEmpty(strAbstract))
                {
                    var transStr = highlighter.GetBestFragment(strAbstract, model.Abstract);
                    model.Abstract = string.IsNullOrEmpty(transStr) ? model.Abstract : transStr;
                }
            }
            catch
            {
                return(model);
            }
            return(model);
        }