日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

用lucene实现在一个(或者多个)字段中查找多个关键字

發布時間:2024/9/20 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用lucene实现在一个(或者多个)字段中查找多个关键字 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  最近跟著師兄們做個項目,我的任務就是負責做個“全文檢索”的小模塊。用到了Lucene的索引,下面的是其中的用Lucene實現在索引的一個字段(比如文章內容字段)進行查找多個關鍵字的實例代碼。

  1.Lucene說明

  Lucene是非常優秀的成熟的開源的免費的純java語言的全文索引檢索工具包。

  Lucene的的強項在“建立索引”和”搜索“,而不是實現具體的”分詞“。Lucene支持對生成索引的進行”增,刪,改,查“操作,這比自己建立的索引有了很大的進步。

  可以使用專門的分詞程序進行分詞,在分詞的結果上用Lucene建立索引。

  2.用Lucene實現在一個或者多個字段中的檢索

  主要是函數:MultiFieldQueryParser.parse(String[] query,String[] field,Occur[] occ,Analyzer analyzer);

      1)query:要查找的字符串數組

      2)field:要查找的字符串數組對應的字段(當然有可以相同的)

      3)occ:表示對應字段的限制。有三種:Occur.MUST(結果“與”), Occur.MUST_NOT(結果“差”),Occur.SHOULD(結果“或”)

      4)analyzer:對查詢數據的分析器,最好與建立索引時用的分析器一致

  3.代碼示例

  下面這個程序可以實現在一個字段“contents”中查找多個關鍵字。稍加修改也可以在多個字段查找多個關鍵字。

import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.queryParser.MultiFieldQueryParser; import org.apache.lucene.search.BooleanClause.Occur; import org.apache.lucene.search.Hits; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query;public class MultiPhraseQuerySearcher {private static String indexPath = "E:\\Lucene\\index"; // 索引保存目錄public static void createIndex(){ // 建立索引 IndexWriter writer;try {writer = new IndexWriter(indexPath,new StandardAnalyzer(),true);Field fieldB1 = new Field("contents","今晚的辯題很道地:在我們這些人當中?",Field.Store.YES,Field.Index.TOKENIZED);Field fieldB2 = new Field("contents","我們為電影《今朝》是一部不錯的影片。",Field.Store.YES,Field.Index.TOKENIZED);Field fieldB3 = new Field("contents","我們到底是啥意思呢?",Field.Store.YES,Field.Index.TOKENIZED);Document doc1 = new Document();Document doc2 = new Document();Document doc3 = new Document();doc1.add(fieldB1);doc2.add(fieldB2);doc3.add(fieldB3);writer.addDocument(doc1);writer.addDocument(doc2);writer.addDocument(doc3);writer.close();} catch (Exception e) {e.printStackTrace();} }public static void main(String[] args) { //contests字段上查找含有"我們","今晚"這兩個字段的Doument Query query;IndexSearcher searcher;try {//生成索引 createIndex();searcher = new IndexSearcher(indexPath);//要查找的字符串數組String [] stringQuery={"我們","今晚"};//待查找字符串對應的字段String[] fields={"contents","contents"};//Occur.MUST表示對應字段必須有查詢值, Occur.MUST_NOT 表示對應字段必須沒有查詢值Occur[] occ={Occur.MUST,Occur.MUST};query=MultiFieldQueryParser.parse(stringQuery,fields,occ,new StandardAnalyzer()); Hits hits = searcher.search(query);for(int i=0;i<hits.length();i++)System.out.println("Document內容為 : "+hits.doc(i));System.out.println("共檢索出符合條件的Document "+hits.length()+" 個。");} catch (Exception e) {} } }

?

    

  

轉載于:https://www.cnblogs.com/xudong-bupt/archive/2013/05/08/3065297.html

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的用lucene实现在一个(或者多个)字段中查找多个关键字的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。