IndexHelper 是在LuceneHelper的基礎(chǔ)上的頂級封裝,同時,集成了數(shù)據(jù)同步方案。
- 分頁查詢
public <T extends BaseEntity> List<T> searchByPage(@NotNull QueryWrapper wrapper) {
Assert.notNull(wrapper);
BooleanQuery.Builder builder = new BooleanQuery.Builder();
wrapper.getQueryMap().forEach(builder::add);
IndexBO indexBO = wrapper.getIndexInfo();
List<Document> list = this.luceneHelper.searchByPage(indexBO.getIndexName(), builder, wrapper.getSort(), wrapper.getPageInfo(), wrapper.getFieldToLoad());
return this.documentHelper.toEntityList(list, ClassUtil.loadClass(wrapper.getClassName()));
}
- 查詢
search方法是基于luceneHelper.search的實(shí)現(xiàn),所以在結(jié)果集過大的情況下響應(yīng)可能會變慢。
public <T extends BaseEntity> List<T> search(@NotNull QueryWrapper queryWrapper) {
BooleanQuery.Builder builder = new BooleanQuery.Builder();
queryWrapper.getQueryMap().forEach(builder::add);
IndexBO indexBO = queryWrapper.getIndexInfo();
List<Document> list = this.luceneHelper.search(
indexBO.getIndexName(),
builder, queryWrapper.getSort(), queryWrapper.getFieldToLoad());
return this.documentHelper.toEntityList(list, ClassUtil.loadClass(queryWrapper.getClassName()));
}
- 主鍵查詢
該方法檢索的實(shí)體類必須繼承父類BaseEntity,并默認(rèn)主鍵字段:“_id”。IdQueryWrapperBuilder實(shí)現(xiàn)方式:
public IdQueryWrapperBuilder(Class<T> indexClass, String id) {
super(indexClass);
super.queryMap.put(new TermQuery(new Term(StrConstant.D_ID, id)), BooleanClause.Occur.MUST);
}
根據(jù)主鍵檢索:
public <T extends BaseEntity> T searchById(@NotNull IdQueryWrapper queryWrapper) {
BooleanQuery.Builder builder = new BooleanQuery.Builder();
queryWrapper.getQueryMap().forEach(builder::add);
IndexBO indexBO = queryWrapper.getIndexInfo();
List<Document> list = this.luceneHelper.search(
indexBO.getIndexName(), builder, Sort.INDEXORDER, queryWrapper.getFieldToLoad());
List<T> tList = this.documentHelper.toEntityList(list, ClassUtil.loadClass(queryWrapper.getClassName()));
return CollUtil.isEmpty(tList) ? null : tList.get(0);
}
- 主鍵集合查詢
該方法實(shí)際是在searchById的基礎(chǔ)上做should查詢,對標(biāo)關(guān)系型數(shù)據(jù)的 in 查詢。
public <T extends BaseEntity> List<T> searchByIds(IdsQueryWrapper queryWrapper) {
BooleanQuery.Builder builder = new BooleanQuery.Builder();
queryWrapper.getQueryMap().forEach(builder::add);
IndexBO indexBO = queryWrapper.getIndexInfo();
List<Document> list = this.luceneHelper.search(indexBO.getIndexName(), builder, Sort.RELEVANCE, queryWrapper.getFieldToLoad());
return this.documentHelper.toEntityList(list, ClassUtil.loadClass(queryWrapper.getClassName()));
}
- 數(shù)量統(tǒng)計(jì)
public Long count(CountQueryWrapper queryWrapper) {
try {
IndexBO indexBO = queryWrapper.getIndexInfo();
IndexReader indexReader = this.indexReaderCache.get(indexBO.getIndexName(), false);
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
BooleanQuery.Builder builder = new BooleanQuery.Builder();
queryWrapper.getQueryMap().forEach(builder::add);
return ((Integer) indexSearcher.count(builder.build())).longValue();
} catch (IndexNotFoundException e) {
return 0L;
} catch (Exception e) {
log.error("document count error", e);
}
throw new AppException(MessageEnum.SYSTEM_ERROR);
}
- 新增文檔
新增文檔有兩個關(guān)鍵點(diǎn):
- entityMetaObjectHandler.fill() 方法實(shí)現(xiàn)對實(shí)體類的自動填充;
- dataSyncRender.commit() 方法會根據(jù)指定的同步方式保存并同步數(shù)據(jù);
public <T extends BaseEntity> T addDocument(T entity) {
IndexBO indexBO = this.documentHelper.index(entity.getClass());
this.entityMetaObjectHandler.fill(FieldFillEnum.INSERT, indexBO, entity);
IndexUpdateParamBO paramBO = this.buildIndexUpdateParamBO(indexBO, CollUtil.newArrayList(entity), null, DocSyncTypeEnum.ADD);
this.dataSyncRender.commit(paramBO);
return entity;
}
- 新增文檔集合
public <T extends BaseEntity> List<T> addDocuments(List<T> entityList) {
if (CollUtil.isEmpty(entityList)) {
return CollUtil.newArrayList();
}
IndexBO indexBO = this.documentHelper.index(entityList.get(0).getClass());
entityList.forEach(f -> this.entityMetaObjectHandler.fill(FieldFillEnum.INSERT, indexBO, f));
IndexUpdateParamBO paramBO = this.buildIndexUpdateParamBO(indexBO, entityList, null, DocSyncTypeEnum.ADD);
this.dataSyncRender.commit(paramBO);
return entityList;
}
- 更新文檔
在 LuceneHelper 的介紹里面提到過,lucene的更新操作實(shí)際是“先刪除,后新增”。如果直接調(diào)用LuceneHelper更新文檔,則每次更新前都必須先查詢,替換字段數(shù)據(jù),再更新到索引。根據(jù)主鍵更新文檔的前提是主鍵明確,因此,在這里隱藏掉“先刪除,后新增”的邏輯。
- 根據(jù)主鍵檢索出文檔,如果沒有文檔,則拋出異常;
- 拷貝文檔字段信息;
- entityMetaObjectHandler.fill() 方法實(shí)現(xiàn)對實(shí)體類的自動填充;
- dataSyncRender.commit() 方法會根據(jù)指定的同步方式保存并同步數(shù)據(jù);
public <T extends BaseEntity> T updateDocumentById(T entity) {
String id = this.documentHelper.id(entity);
IdQueryWrapperBuilder<T> idQueryWrapperBuilder = new IdQueryWrapperBuilder<>(ClassUtil.getClass(entity), id);
IdQueryWrapper idQueryWrapper = idQueryWrapperBuilder.build();
T source = this.searchById(idQueryWrapper);
if(Objects.isNull(source)) {
throw new AppException(MessageEnum.PARAM_NULL);
}
IndexBO indexBO = idQueryWrapper.getIndexInfo();
BeanUtil.copyProperties(entity, source, CopyOptions.create().ignoreNullValue());
this.entityMetaObjectHandler.fill(FieldFillEnum.UPDATE, indexBO, source);
IndexUpdateParamBO paramBO = this.buildIndexUpdateParamBO(indexBO, CollUtil.newArrayList(source), null, DocSyncTypeEnum.UPDATE);
this.dataSyncRender.commit(paramBO);
return source;
}
- 刪除操作
public void deleteByIds(DeleteByIdsWrapper wrapper) {
Assert.notNull(wrapper, "DeleteByIdsWrapper can not null");
IndexBO indexBO = wrapper.getIndexInfo();
IndexUpdateParamBO paramBO = this.buildIndexUpdateParamBO(indexBO, null, wrapper.getIdList(), DocSyncTypeEnum.DEL);
paramBO.setIgnoreSync(wrapper.getIgnoreSync());
this.dataSyncRender.commit(paramBO);
}
和LuceneHelper相比,IndexHelper的使用相對更簡單,繼承了實(shí)體類和文檔的轉(zhuǎn)換方案,避免對文檔的繁瑣操作。同時,業(yè)繼承了數(shù)據(jù)的同步方式。也就是說,LuceneHelper的操作僅是本地存儲,僅是把IndexReader/IndexWriter交由組件管理而已。