代码地址:
https://gitee.com/shaojiepeng/wsm-lucene
 ### wsm-lucene
一个简单的Lucene工具类,通过注释的方式来配置构建索引的字段。提供新建索引、查找、删除、更新方法,支持分页。
### 所需jar包
1. lucene-core:2.4.0
2. lucene-analyzers:2.4.1
3. commons-logging:1.2
### 背景
以前在做某个feature的时候,鉴于存储在DB中的数据量过大,故使用Lucene来优化查找性能。
相信大家在某些场景下会把DB中的数据读出来,建索引来优化查找。那么这个工具类就比较适合这些场景了。
### 如何使用
 **从附件中下载jar包直接导入到项目中,或者下载此Maven项目的源码,使用项目依赖的方式导入你的项目。** 
1. 通过注释的方式配置需要构建索引的model类
```
 **@IndexClass** :注释,说明此model类需要构建索引
 **indexDirPath** :索引所存放的物理位置,如:"D:/Index"
 **@IndexField** :注释,说明此字段需要构建索引
 **fieldStore** :Lucene中的Field.Store同义,不懂请自行查询资料
 **fieldIndex** :Lucene中的Field.Index同义,不懂请自行查询资料
```
2. 创建索引
```
IndexService indexService = new IndexServiceImpl();
/** 构建索引的接口
 * List:model的集合
 * Class: model的class
 *
 * return boolean
**/
indexService.buildIndex(List, Class)
```
3.查找
```
ArrayList<SearchParamModel> searchParams = new ArrayList<>();
/**添加查询的条件,如果有多个查询条件,则添加SearchParamModel
 * fieldName:需要查找的字段,即model中的成员变量
 * fieldValue:需要查找字段的值,这个不解释
 * BooleanType:Lucene中BooleanClause.Occur值,不懂请自行查询资料
**/
searchParams.add(new SearchParamModel(fieldName, fieldValue, BooleanType));
IndexService indexService = new IndexServiceImpl();
/** 查询的接口
 * searchParams:不解释
 * Class: model的class
 *
 * return model的集合
**/
List objs = indexService.search(searchParams, Class);
```
IndexService中还支持update, delete和分页查找的方法,请自行查阅代码。
觉得不错,请点个赞吧。