導入org . Apache . Lucene . analysis . analyzer;
導入org . Apache . Lucene . document . document;
導入org . Apache . Lucene . document . field;
導入org . Apache . Lucene . document . string field;
導入org . Apache . Lucene . document . textfield;
import org . Apache . Lucene . index . corruptindexexception;
導入org . Apache . Lucene . index . directory reader;
導入org . Apache . Lucene . index . index reader;
導入org . Apache . Lucene . index . index writer;
導入org . Apache . Lucene . index . indexwriterconfig;
導入org . Apache . Lucene . index . indexwriterconfig . open mode;
導入org . Apache . Lucene . query parser . classic . parse exception;
導入org . Apache . Lucene . query parser . classic . query parser;
導入org . Apache . Lucene . search . index searcher;
導入org . Apache . Lucene . search . query;
導入org . Apache . Lucene . search . score doc;
導入org . Apache . Lucene . search . top docs;
導入org . Apache . Lucene . store . directory;
import org . Apache . Lucene . store . lockonpoverfailedexception;
導入org . Apache . Lucene . store . ram directory;
導入org . Apache . Lucene . util . version;
導入org . wl tea . analyzer . Lucene . ik analyzer;
/**
*使用IKAnalyzer演示Lucene索引和查詢。
* 2012-3-2
*
*以下是Lucene4.0 API的組合。
*
*/
公共類LuceneIndexAndSearchDemo {
/**
*模擬:
*創建單個記錄的索引並進行搜索。
* @param args
*/
公共靜態void main(String[] args){
lucenedocument的域名
String fieldName = " text
//檢索內容
String text = "IK Analyzer是壹款開源的中文分詞工具包,結合了詞典分詞和語法分詞。它采用了全新的正向叠代細粒度分割算法。”;
//實例化IKAnalyzer單詞拆分器
Analyzer analyzer = new IKAnalyzer(真);
目錄directory = null
IndexWriter iwriter = null
IndexReader ireader = null
IndexSearcher isearcher = null
嘗試{
//創建內存索引對象
directory = new ram directory();
//配置IndexWriterConfig
IndexWriterConfig iwConfig =新的IndexWriterConfig(版本。LUCENE_40,分析器);
iwConfig.setOpenMode(OpenMode。創建_或_追加);
iwriter = new IndexWriter(目錄,iwConfig);
//寫入索引
文檔doc =新文檔();
doc.add(新字符串字段(" ID "," 10000 ",字段。store . YES));
doc.add(新的文本字段(字段名,文本,字段。store . YES));
I writer . add document(doc);
I writer . close();
//搜索過程* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
//實例化搜索器
ireader = DirectoryReader.open(目錄);
ise archer = new index searcher(ireader);
String keyword = "中文分詞工具包";
//使用QueryParser查詢分析器構造查詢對象。
QueryParser qp =新的QueryParser(版本。LUCENE_40,fieldName,analyzer);
qp . setdefaultoperator(query parser。AND _ OPERATOR);
Query query = qp.parse(關鍵字);
system . out . println(" Query = "+Query ");
//搜索相似度最高的五條記錄。
top docs top docs = ise archer . search(query,5);
system . out . println(" hit:"+top docs . total hits);
//輸出結果
score doc[]score docs = top docs . score docs;
for(int I = 0;我& lttop docs . total hits;i++){
文檔target doc = ise archer . doc(score docs[I])。doc);
system . out . println(" content:"+target doc . tostring());
}
} catch(CorruptIndexException e){
e . printstacktrace();
} catch(lockonpoverfailedexception e){
e . printstacktrace();
} catch (IOException e) {
e . printstacktrace();
} catch (ParseException e) {
e . printstacktrace();
}最後{
如果(ireader!= null){
嘗試{
ireader . close();
} catch (IOException e) {
e . printstacktrace();
}
}
如果(目錄!= null){
嘗試{
directory . close();
} catch (IOException e) {
e . printstacktrace();
}
}
}
}
}