很早以前写的lucenet.net搜索

时间:2022-09-04 12:08:08

//创建索引库

string indexPath = Server.MapPath("~/temp");//索引库(索引→Index)
            //开始创建索引;
            //开始创建索引,目录:" + indexPath;
            FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NativeFSLockFactory()); //FSDirectory存储文件的索引

            bool isUpdate = IndexReader.IndexExists(directory);//判断一个索引库是否存在
            if (isUpdate)
            {
                //索引库已经存在;
                if (IndexWriter.IsLocked(directory))
                {
                    //索引库被锁定,进行解锁;
                    IndexWriter.Unlock(directory);
                }
            }

            IndexWriter writer = new IndexWriter(directory,
                new PanGuAnalyzer(), !isUpdate, Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIMITED);
            //directiory 根据一个字符串描述的路径,为建立的索引文件指定存放目录
            //PanGuAnalyzer 一个分析器
            //第三个参数 如果为true表示重写索引文件,false表示追加索引文件信息
            IList<MaiDong_Deal> list = GetAllMaiDong_DealApi();

            for (int i = 0; i < list.Count; i++)
            {
                //如果搜不出来,看看是不是编码的问题
                Document document = new Document(); //创建索引文档,例如一张白纸
                document.Add(new Field("number", i.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
                document.Add(new Field("title", list[i].DealName, Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));
                document.Add(new Field("body", list[i].Intro, Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));
                writer.DeleteDocuments(new Term("number", i.ToString()));  //删除索引库
                writer.AddDocument(document);  //讲白纸放进索引文件
            }
           writer.Optimize();  //对索引进行优化
            writer.Close();
            directory.Close();
            //索引完毕;
            Response.Write("创建索引完毕");

  

 /// <summary>
        /// 1,制定索引库位置,
        /// 2,利用indexreader可以操作索引库
        /// 3,indexsearcher得到Indexreader进行搜索对索引库
        /// 4,对客户关键词进行分词(Query代表用户的查询语句)
        /// 5,使用IndexSearcher进行搜索,把搜索结果放进TopScoreDocCollector集合中
        /// 6,从TopScoreDocCollector中获取docId集合放在ScoreDoc中
        /// 7,然后Indexsearcher根据docId 获得数据文档Document
        void Result_PreRender(object sender, EventArgs e)
        {
            string kw = Request["wd"];
            string indexPath = Server.MapPath("~/temp");
            FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NoLockFactory());
            IndexReader reader = IndexReader.Open(directory, true);
            IndexSearcher searcher = new IndexSearcher(reader);//创建索引搜索器
            PhraseQuery query = new PhraseQuery();
            string[] words = CommonHelper.SplitWords(kw);
            foreach (string word in words)
            {

                query.Add(new Term("title", word));

            }
            
            query.SetSlop(100); //相隔可以查询中间相隔几个个字把(我们的这分成我们,的两个词) 如 1,我们 2,的 不加他查询是(我们的),加上坡度就可以是(我们与的),(我们和的)就是把词的单个都查出来
            //创建结果文档收集器
            TopScoreDocCollector collector = TopScoreDocCollector.create(1000, true);//搜索1000条数据,true代表是否排序(按照积分)(相当于多个Hits的集合)
            searcher.Search(query, null, collector); //将所搜寻出来的结果以特定的形式放在collector中
            StringBuilder sb = new StringBuilder();
            this.anpUsers.RecordCount = collector.GetTotalHits();
            
            if (string.IsNullOrEmpty(Request.QueryString["page"]))
            {
                sb.Append("/Result.aspx?page={0}");
                this.anpUsers.CurrentPageIndex = 1;
            }
            else if (!string.IsNullOrEmpty(Request.QueryString["page"]))
            {
                sb.Append("/Result.aspx?page={0}");
               this.anpUsers.CurrentPageIndex = Convert.ToInt32(Request.QueryString["page"]);
            }
            if (!string.IsNullOrEmpty(kw))
            {
                sb.Append("&wd=" + kw);
            }
            this.anpUsers.UrlRewritePattern = sb.ToString();
            int startPos = Math.Max((this.anpUsers.CurrentPageIndex - 1) * this.anpUsers.PageSize, 0);
            ScoreDoc[] docs = collector.TopDocs(startPos, this.anpUsers.PageSize).scoreDocs;//scoreDocs 配文档的集合包含了DocId和Score 一般通过DocId取文档

            List<SearchResult> list = new List<SearchResult>();
            for (int i = 0; i < docs.Length; i++)
            {
                int docId = docs[i].doc;  // 根据命中的文档的内部编号获取该文档
                Document doc = searcher.Doc(docId);
                //string number = doc.Get("number");
                string body = doc.Get("body");
                string title = doc.Get("title");
                SearchResult result = new SearchResult();
               
            result.Body = body;

             result.Title = CommonHelper.HighLight(kw, title);
                list.Add(result);
            }

            Repeater1.DataSource = list;
            Repeater1.DataBind();

        }