1、Lucene的核心jar包
lucene-core-5.2.1.jar
lucene-analyzers-common-5.2.1.jar
lucene-queryparser-5.2.1.jar
2、主要开发包说明
org.apache.lucene.analysis:语言分析器,主要用于分词
org.apache.lucene.document:索引文档的管理
org.apache.lucene.index:索引管理,如增、删、改
org.apache.lucene.queryparser:查询分析
org.apache.lucene.search:检索管理
org.apache.lucene.store:数据存储管理
org.apache.lucene.util:工具包
3、写入索引操作的核心类
Directory:代表索引文档的存储位置,这是一个抽象类有FSDirectory和RAMDirectory两个主要子类。前者将索引写入文件系统,后者将索引文档写入内存。
Analyzer:建立索引时使用的分析器
IndexWriterConfig:操作索引库的配置信息
IndexWriter:建立索引的核心类,用来操作索引(增、删、改)
Document:代表一个索引文档
Field:代表索引文档中存储的数据,新版本的Lucene进行了细化给出了多个子类:IntField、LongField、FloatField、DoubleField、TextField、StringField等。
4、写入索引
package cn.harmel.lucene;
import java.io.IOException;
import java.nio.file.Paths;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
public class First {
public static void main(String[] args) throws IOException {
Analyzer a = new StandardAnalyzer();
Directory dir = FSDirectory.open(Paths.get("./index"));
IndexWriterConfig iwc = new IndexWriterConfig(a);
IndexWriter iw = new IndexWriter(dir, iwc);
Document doc = new Document();
doc.add(new TextField("info", "this is my first lucene test", Field.Store.YES));
iw.addDocument(doc);
iw.close();
}
}
原文:http://my.oschina.net/harmel/blog/490647