示例#1
0
        private void SearchFromIndexSelf()
        {
            int    max  = new SUC_NEWS().MaxID();
            int    rand = new Random().Next(max);
            string name = new SUC_NEWS().FindByCondition(new SUC_NEWS()
            {
                ID = rand
            })[0].TITLE;

            string[] titles = SplitContent.SplitWords(name);
            rand = new Random().Next(titles.Length);
            SearchFromIndexData(titles[rand]);
        }
示例#2
0
        private void SearchFromIndexData(string searchkey)
        {
            string        indexPath = Context.Server.MapPath("~/IndexData");
            FSDirectory   directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NoLockFactory());
            IndexReader   reader    = IndexReader.Open(directory, true);
            IndexSearcher searcher  = new IndexSearcher(reader);
            //搜索条件
            PhraseQuery query = new PhraseQuery();

            //把用户输入的关键字进行分词
            foreach (string word in SplitContent.SplitWords(searchkey))
            {
                query.Add(new Term("TITLE", word));
            }
            //query.Add(new Term("content", "C#"));//多个查询条件时 为且的关系
            query.SetSlop(100);  //指定关键词相隔最大距离

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

            searcher.Search(query, null, collector);                                    //根据query查询条件进行查询,查询结果放入collector容器
            //TopDocs 指定0到GetTotalHits() 即所有查询结果中的文档 如果TopDocs(20,10)则意味着获取第20-30之间文档内容 达到分页的效果
            ScoreDoc[] docs = collector.TopDocs(0, collector.GetTotalHits()).scoreDocs; //collector.GetTotalHits()

            //展示数据实体对象集合
            for (int i = 0; i < docs.Length; i++)
            {
                int      docID = docs[i].doc;         //得到查询结果文档的ID(Lucene内部分配的ID)
                Document doc   = searcher.Doc(docID); //根据文档ID来获得文档对象Document
                SUC_NEWS mod   = new SUC_NEWS();
                mod.TITLE = SplitContent.HightLight(searchkey, doc.Get("TITLE"));
                mod.TITLE = string.IsNullOrEmpty(mod.TITLE) ? doc.Get("TITLE") : mod.TITLE;
                //book.ContentDESCRPTION = doc.Get("content");//未使用高亮
                //搜索关键字高亮显示 使用盘古提供高亮插件
                mod.CONTENT     = SplitContent.HightLight(searchkey, doc.Get("CONTENT"));
                mod.CONTENT     = string.IsNullOrEmpty(mod.CONTENT) ? doc.Get("CONTENT") : mod.CONTENT;
                mod.CONTENT     = mod.CONTENT.Replace("<b>", "");
                mod.ID          = Convert.ToInt32(doc.Get("ID"));
                mod.pandaWebUrl = doc.Get("URL");
                modResult.Add(mod);
            }
        }