Skip to content

LeYunone/spring-es-leyunone

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ElasticSearch快速集成组件

1、配置文件中添加配置:

spring:
    elasticsearch:
      port: 9200
      host: 192.168.151.234
      thread:
        corePoolSize: 2
        maxPoolSize: 4
        keepAliveSeconds: 30
        queueCapacity: 50 

2、在需要创建\更新\删除目标文档上实现注释 @EsIndexMaintenance

例如:

    @EsIndexMaintenance(type = OperationTypeEnum.ADD,
            indexName = "test-index",
            indexId = "#tests.![id]",
            doc = {
                    "#tests.![id]",
                    "#tests.![name]",
                    "#tests.![age]"
            },
            docField = {
                    "id",
                    "name",
                    "age"
            })
    public boolean saveBatch(List<TestDO> tests) {
        return true;
    }

    @EsIndexMaintenance(type = OperationTypeEnum.DELETE,
            indexName = "test-index",
            indexId = "#ids")
    public boolean deleteByIds(List<String> ids) {
        return true;
    }

    @EsIndexMaintenance(type = OperationTypeEnum.DELETE,
            indexName = "test-index",
            indexId = "#id")
    public boolean deleteById(String id) {
        return true;
    }

规则:

  1. indexName为es目标文档索引名
  2. indexIddoc为Spel表达式
  3. docField为目标文档的关键字需与doc数组一一对应

实现该步骤即集成完成了es文档自动维护

3、继承搜索关键字对象,集成搜索方法

搜索查询对象:

public class EsSearchQuery {
    /**
     * 关键字
     */
    private String keyWord;

    /**
     * 当前页
     */
    private Integer pageIndex = 1;

    /**
     * 每页数据量
     */
    private Integer pageSize = 10;
}

搜索方法

public interface EsSearchService {

    /**
     * 搜索方法
     * @param query 查询条件
     * @param keyWordObject 文档关键字对象
     * @param response  查询出来的结果文档
     * @param indexName 索引值
     * @param <T>
     * @return
     */
    <T extends SearchResultData> EsSearchResultData<T> search(EsSearchQuery query, Class<?> keyWordObject, Class<T> response, String... indexName);
}

规则:

  • keyWordObject为需要查询的关键字对象,例如我有一个文档里面的关键字为name、age、sex,我想要本次查询name和age关键字,则定义类:

    public class TestKeyData{
        private String name;
        private Integer age;
    }
  • response为本次查询的响应结果类

  • indexName为本次查询的目标文档名,支持多个文档复合查询

About

springboot集成es自动维护索引

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages