lucene3 学习日志
程序员文章站
2022-03-22 08:07:23
...
近来项目需要使用Lucene,工作之余上网学习了下相关内容,做个笔记
1.创建索引
步骤:创建IndexWriter
IndexWriter writer = new IndexWriter(
new NIOFSDirectory(new File(path)), new StandardAnalyzer(
Version.LUCENE_30), MaxFieldLength.LIMITED);
创建Document
创建Field 包含 field名和field值
将Field通过Document的add方法添加到Document中
Document doc = new Document();
doc.add(new Field("text", "It is a text area", Store.YES,
Index.ANALYZED_NO_NORMS));
doc.add(new Field("info", "It is a Infomation area", Store.YES,
Index.ANALYZED_NO_NORMS));
将Document通过IndexWriter的addDocument方法添加到IndexWriter中
关闭IndexWriter
writer.addDocument(doc);writer.close();
2从索引中根据关键字查询
创建IndexSearcher
IndexSearcher searcher = new IndexSearcher(new NIOFSDirectory(new File(
path)));
创建Query
Query query = new QueryParser(Version.LUCENE_30, field,
new StandardAnalyzer(Version.LUCENE_30)).parse(keyword);
通过IndexSearcher的search方法查找关键字,使用TopDocs封装结果集
TopDocs docs = searcher.search(query, 10);
全部代码:(包换了合并内存索引到硬盘索引中)
import java.io.File;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.NIOFSDirectory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
public class TestLucen {
public static final String path = "E:\\workspaces\\lucene\\index";
public static void main(String[] args) throws Exception {
writeIndex();
readIndex("text", "area");
}
public static void writeIndex() throws Exception {
// 硬盘索引
IndexWriter writer = new IndexWriter(
new NIOFSDirectory(new File(path)), new StandardAnalyzer(
Version.LUCENE_30), MaxFieldLength.LIMITED);
// Ram索引
RAMDirectory ram = new RAMDirectory();
IndexWriter ramwriter = new IndexWriter(ram, new StandardAnalyzer(
Version.LUCENE_30), MaxFieldLength.LIMITED);
Document doc = new Document();
Document doc1 = new Document();
doc.add(new Field("text", "It is a text area", Store.YES,
Index.ANALYZED_NO_NORMS));
doc.add(new Field("info", "It is a Infomation area", Store.YES,
Index.ANALYZED_NO_NORMS));
writer.addDocument(doc);
doc1.add(new Field("text", "it is another area", Store.YES,
Index.ANALYZED));
ramwriter.addDocument(doc1);
ramwriter.optimize();
ramwriter.close();
// 将Ram索引合并到硬盘索引上,必须先调用ram的close方法
writer.addIndexes(IndexReader.open(ram));
writer.optimize();
writer.close();
}
public static void readIndex(String field, String keyword) throws Exception {
IndexSearcher searcher = new IndexSearcher(new NIOFSDirectory(new File(
path)));
Query query = new QueryParser(Version.LUCENE_30, field,
new StandardAnalyzer(Version.LUCENE_30)).parse(keyword);
TopDocs docs = searcher.search(query, 10);
System.out.println("查找到" + docs.totalHits + "个\n对应的text为:");
ScoreDoc[] doc = docs.scoreDocs;
for (ScoreDoc d : doc) {
Document docu = searcher.doc(d.doc);
System.out.println(docu.get(field));
}
}
}
执行结果:
查找到2个
对应的text为:
It is a text area
it is another area