lucenc.net 全文檢索索引 建立索引、 查詢、分頁

來源:互聯網
上載者:User

標籤:lucenc.net   全文檢索索引   建立索引   查詢   分頁   

 #region 建立、跟新詞庫        /// <summary>        /// 建立、跟新詞庫        /// </summary>        private void CreateIndexData()        {            //索引庫檔案夾            FSDirectory dir = FSDirectory.Open(new DirectoryInfo(path), new NativeFSLockFactory());            //是否存在索引庫            bool has = IndexReader.IndexExists(dir);            if (has)            {                //解鎖被鎖定的索引庫                if (IndexWriter.IsLocked(dir))                {                    IndexWriter.Unlock(dir);                }            }            //索引庫寫對象            IndexWriter iw = new IndexWriter(dir, new PanGuAnalyzer(), !has, IndexWriter.MaxFieldLength.UNLIMITED);            List<LuceneModel> list = LuceneBll.Instance.Get();            foreach (var o in list)            {                Document d = new Document();                //Field.Store.YES:                         儲存欄位值(未分詞前的欄位值)                 //Field.Store.NO:                          不儲存,儲存與索引沒有關係                 //Field.Store.COMPRESS:                    壓縮儲存,用於長文本或二進位,但效能受損                 //Field.Index ANALYZED;                    分詞建索引                //Field.Index ANALYZED_NO_NORMS;           分詞壓縮建索引                //Field.Index NO;                //Field.Index NOT_ANALYZED;                不分詞建索引                //Field.Index NOT_ANALYZED_NO_NORMS;       不分詞壓縮建索引                //Field.TermVector NO;                                      //Field.TermVector WITH_OFFSETS;           儲存位移量                //Field.TermVector WITH_POSITIONS;         儲存位置                //Field.TermVector WITH_POSITIONS_OFFSETS; 儲存位置和位移量                //Field.TermVector YES;                    為每個文檔(Document)儲存該欄位的TermVector                d.Add(new Field("Id", o.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));                d.Add(new Field("Title", o.Title, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));                d.Add(new Field("Message", o.Message, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));                iw.AddDocument(d);            }            iw.Optimize();            //釋放檔案            iw.Close();            dir.Close();        }        #endregion
#region 查詢        /// <summary>        /// 查詢        /// </summary>        /// <param name="str">要查詢的關鍵字</param>        /// <param name="index">頁索引(從1開始)</param>        /// <param name="count">每頁顯示資料條數</param>        /// <returns></returns>        private List<LuceneModel> Search(string str, int index = 1, int count = 3)        {            List<LuceneModel> list = null;            //索引庫檔案夾            FSDirectory dir = FSDirectory.Open(new DirectoryInfo(path), new NativeFSLockFactory());            //是否存在索引庫            bool has = IndexReader.IndexExists(dir);            //不存在則建立            if (!has)            {                //建立索引庫                CreateIndexData();            }            //索引庫讀對象            IndexReader ir = IndexReader.Open(dir, true);            //索引庫查詢對象            IndexSearcher searcher = new IndexSearcher(ir);            //搜尋條件            PhraseQuery query = new PhraseQuery();            //分詞            {                Segment segment = new Segment();                ICollection<WordInfo> words = segment.DoSegment(str);                foreach (var o in words)                {                    query.Add(new Term("Message", o.ToString()));                }            }            //指定關鍵詞相隔最大距離            query.SetSlop(100);            //盛放查詢結果的容器            TopScoreDocCollector collector = TopScoreDocCollector.create(index * count, false);            //根據query查詢條件進行查詢            searcher.Search(query, null, collector);            ScoreDoc[] docs = collector.TopDocs(count * (index - 1), count * (index - 1) + count).scoreDocs;            if (docs.Length > 0)            {                list = new List<LuceneModel>();                foreach (var o in docs)                {                    Document document = searcher.Doc(o.doc);                    LuceneModel m = new LuceneModel();                    m.Id = Convert.ToInt32(document.Get("Id"));                    m.Title = document.Get("Title");                    m.Message = document.Get("Message");                    list.Add(m);                }            }            //釋放檔案            ir.Close();            searcher.Close();            dir.Close();            return list;        }        #endregion


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.