10. AnalyzerAnalyzer analyzer = new StandardAnalyzer(); // or any other analyzer
TokenStream ts = analyzer.tokenStream("myfield",new StringReader("some text goes here"));
while (ts.incrementToken()) {
System.out.println("token: "+ts));
}
11. 创建索引//创建新的索引或者对已有的索引增加文档
index = new IndexWriter(indexDirectory,
new StandardAnalyzer(Version.LUCENE_CURRENT),
!incremental,
IndexWriter.MaxFieldLength.UNLIMITED);
File dir = new File(sSourceDir);
indexDir(dir); //索引路径
index.optimize();//索引优化
index.close();//关闭索引库
12. 向索引增加文档Document doc = new Document();
//创建网址列
Field f = new Field("url", news.URL ,
Field.Store.YES, Field.Index. NOT_ANALYZED,//不分词
Field.TermVector.NO);
doc.add(f);
//创建标题列
f = new Field("title", news.title ,
Field.Store.YES, Field.Index.ANALYZED,//分词
Field.TermVector.WITH_POSITIONS_OFFSETS);//存Token位置信息
doc.add(f);
//创建内容列
f = new Field("body", news.body ,
Field.Store.YES, Field.Index. ANALYZED, //分词
Field.TermVector.WITH_POSITIONS_OFFSETS); //存Token位置信息
doc.add(f);
index.addDocument(doc);
13. 搜索// read-only=true
IndexSearcher isearcher = new IndexSearcher(directory, true);
// Parse a simple query that searches for "text":
QueryParser parser = new QueryParser(Version.LUCENE_CURRENT,"fieldname", analyzer);
Query query = parser.parse("text");
//返回前1000条搜索结果
ScoreDoc[] hits = isearcher.search(query, 1000).scoreDocs;
//遍历结果
for (int i = 0; i < hits.length; i++) {
Document hitDoc = isearcher.doc(hits[i].doc);
System.out.println(hitDoc.get("fieldname"));
}
isearcher.close();
directory.close();