项目源码路径:https://github.com/tangxing1993/lucene/tree/master
项目使用Maven构建,采用lucene6.0,lucene前后版本有差异,pom文件如下:
<dependencies>
<!-- lucene的核心jar包 -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>6.0.0</version>
</dependency>
<!-- lucene的查询解析包 -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>6.0.0</version>
</dependency>
<!-- lucene的各种分词器包 -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
<version>6.0.0</version>
</dependency>
<!-- lucene的查询包 -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queries</artifactId>
<version>6.0.0</version>
</dependency>
<!-- lucene的显示高亮包 -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-highlighter</artifactId>
<version>6.0.0</version>
</dependency>
</dependencies>
<!-- 项目使用jdk1.8 -->
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
创建入门程序HelloLucene.java
/**
* 索引的核心API
* IndexWriter 索引的写对象
* IndexWriterConfig 索引写对象的配置对象(可以设置写操作是create还是append)
* Analyzer 通用的分词器接口
* Filed 域对象
* Document 文档对象
* IndexReader 索引的读对象
* IndexSearcher 搜索索引对象
*/
public class HelloLucene {
/**
* 被索引文件的存储路径
*/
private String index_store = "D:\\develope\\store\\hello";
/**
* 索引的存储目录
*/
private Directory directory;
/**
* 创建模拟数据 实际被索引的数据可以为内存中的数据,文件,数据库数据等等
*/
private String[] names = {"java","lucene"};
private String[] contents = {"hello java !","hello lucene !"};
/**
* 创建索引
*/
public void createIndex(){
IndexWriter writer = null;
Document document = null;
try{
Long start = System.currentTimeMillis()/1000;
System.out.println("开始创建索引...");
File file = new File(index_store);
if(!file.exists())
file.mkdirs();
//创建目录对象
directory = FSDirectory.open(file.toPath());
//创建标准分词器
Analyzer analyzer = new StandardAnalyzer();
//创建写索引对象的配置对象
IndexWriterConfig config = new IndexWriterConfig(analyzer);
//创建索引的写入对象
writer = new IndexWriter(directory, config);
//清除所有的索引内容
writer.deleteAll();
//向文档对象中创建域对象,然后由写索引对象创建索引
for(int i=0;i<names.length;i++){
document = new Document();
document.add(new StringField("name", names[i], Store.YES));
document.add(new TextField("content", contents[i], Store.YES));
writer.addDocument(document);
}
Long end = System.currentTimeMillis()/1000;
System.out.println("索引创建成功!耗时"+(end-start)+"s");
}catch(Exception e){
e.printStackTrace();
}finally{
if(null!=writer){
try {
writer.close(); //关闭资源
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 搜索操作
*/
public void doSearch(){
IndexReader reader =null;
try{
directory = FSDirectory.open(new File(index_store).toPath());
//创建索引文件读对象
reader = DirectoryReader.open(directory);
//创建索引搜索对象
IndexSearcher searcher = new IndexSearcher(reader);
//创建标准分词器
Analyzer analyzer = new StandardAnalyzer();
//创建查询解析器
QueryParser parser = new QueryParser("content", analyzer);
//搜索内容中包含java的文件
Query query = parser.parse("java");
//搜索内容
TopDocs search = searcher.search(query, 10); //10表示要现实的调试
//获取搜索到的结果
ScoreDoc[] scoreDocs = search.scoreDocs;
for(ScoreDoc sd : scoreDocs){
//获取搜索到的文档
Document doc = searcher.doc(sd.doc);
System.out.println("文档id:"+sd.doc+" | name:"+doc.get("name")+" | content:"+doc.get("content"));
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(null!=reader){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
//创建索引
HelloLucene lucene = new HelloLucene();
lucene.createIndex();
lucene.doSearch();
}
}
最终执行结果:
luke工具的使用:
索引创建完之后可以使用索引查看工具进行查看和维护
Luke(https://code.google.com/archive/p/luke/)这个好像只能下载到支持lucene4.0版本以下的,打开页面
,
本片用到的工具在http://www.oschina.net/news/72599/luke-6-0-0 里面下载。