一个简单的Lucene工具类,经过注释的方式来配置构建索引的字段。提供新建索引、查找、删除、更新方法,支持分页。代码地址:https://gitee.com/shaojiepeng...git
之前在作某个feature的时候,鉴于存储在DB中的数据量过大,故使用Lucene来优化查找性能。
相信你们在某些场景下会把DB中的数据读出来,建索引来优化查找。那么这个工具类就比较适合这些场景了。工具
从附件中下载jar包直接导入到项目中,或者下载此Maven项目的源码,使用项目依赖的方式导入你的项目。性能
**@IndexClass** :注释,说明此model类须要构建索引 **indexDirPath** :索引所存放的物理位置,如:"D:/Index" **@IndexField** :注释,说明此字段须要构建索引 **fieldStore** :Lucene中的Field.Store同义,不懂请自行查询资料 **fieldIndex** :Lucene中的Field.Index同义,不懂请自行查询资料
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和分页查找的方法,请自行查阅代码。ui
以为不错,请点个赞吧。spa