lucene-candy系列:IndexHelper

IndexHelper 是在LuceneHelper的基础上的顶级封装,同时,集成了数据同步方案。

  1. 分页查询
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()));
    }
  1. 查询
    search方法是基于luceneHelper.search的实现,所以在结果集过大的情况下响应可能会变慢。
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()));
    }
  1. 主键查询
    该方法检索的实体类必须继承父类BaseEntity,并默认主键字段:“_id”。IdQueryWrapperBuilder实现方式:
public IdQueryWrapperBuilder(Class<T> indexClass, String id) {
        super(indexClass);
        super.queryMap.put(new TermQuery(new Term(StrConstant.D_ID, id)), BooleanClause.Occur.MUST);
    }

根据主键检索:

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);
    }
  1. 主键集合查询
    该方法实际是在searchById的基础上做should查询,对标关系型数据的 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()));
    }
  1. 数量统计
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);
    }
  1. 新增文档
    新增文档有两个关键点:
  • entityMetaObjectHandler.fill() 方法实现对实体类的自动填充;
  • dataSyncRender.commit() 方法会根据指定的同步方式保存并同步数据;
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;
    }
  1. 新增文档集合
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;
    }
  1. 更新文档
    在 LuceneHelper 的介绍里面提到过,lucene的更新操作实际是“先删除,后新增”。如果直接调用LuceneHelper更新文档,则每次更新前都必须先查询,替换字段数据,再更新到索引。根据主键更新文档的前提是主键明确,因此,在这里隐藏掉“先删除,后新增”的逻辑。
  • 根据主键检索出文档,如果没有文档,则抛出异常;
  • 拷贝文档字段信息;
  • entityMetaObjectHandler.fill() 方法实现对实体类的自动填充;
  • dataSyncRender.commit() 方法会根据指定的同步方式保存并同步数据;
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;
    }
  1. 删除操作
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的使用相对更简单,继承了实体类和文档的转换方案,避免对文档的繁琐操作。同时,业继承了数据的同步方式。也就是说,LuceneHelper的操作仅是本地存储,仅是把IndexReader/IndexWriter交由组件管理而已。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容