Springboot 快速集成 ES

 1、Springboot 官网给出的版本选择标准

2、选择版本依赖

我的 elasticsearch 服务版本为 7.17.13,所以 springboot 版本我选用 2.7.10

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.7.10</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

客户端版本选择 springboot 默认版本 

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

添加配置文件

spring:
  elasticsearch:
    uris: http://localhost:9200
    username: elastic
    password: admin123

对应的配置类:org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchProperties

ps: spring.elasticsearch.rest 的配置方式从 springboot 2.6.0 起已弃用,将在3.0.0中移除,不推荐使用

3、实现简单的增删改查

a、继承 ElasticsearchRepository<T, ID> 接口,使用模版方法或自定义方法
@Repository
public interface SystemConfigRepository extends ElasticsearchRepository<SystemConfig, String> {

    SystemConfig findByBizAndCodeAndKey(String biz, String appCode, String ruleKey);

    @Query("{\"bool\": {\"must\": [{\"match\": {\"appCode\": \"?0\"}}," +
            "{\"match\": {\"ruleKey\": \"?1\"}}]}}")
    SystemConfig findByCondition(String appCode, String ruleKey);
}
基于命名约定

查询方法根据方法的命名自动生成,方法名中的关键字和字段名称将被解析为查询条件。 常用的命名约定如下:

  • findBygetByqueryBy:表明查询方法的开始。
  • 属性名称:指定要匹配的字段名。
  • AndOr:用于连接多个条件。
  • InNotIn:用于查询字段值在给定集合中或不在给定集合中的记录。
  • IsEquals:用于查询字段是否等于给定值的记录。
  • Like:用于查询字段模糊匹配的记录。
  • GreaterThanLessThan:用于查询字段大于或小于给定值的记录。
  • OrderBy:用于指定排序字段
基于 @Query 注解

        在查询方法上直接指定 Elasticsearch 查询语句,可以使用 Elasticsearch 查询 DSL 编写复杂的查询逻辑,并使用 SpEL 表达式引用方法参数或其他变量。

  • 字符串查询
@Query("{\"match\": {\"field\": \"value\"}}")
List<YourEntity> findByCustomQuery();
  • 参数绑定
@Query("{\"match\": {\"field\": \"?0\"}}")
List<YourEntity> findByValue(String value);
  • 布尔查询
// 使用布尔查询可以组合多个查询条件,例如 must、should、must_not 等
@Query("{\"bool\": {\"must\": [{\"match\": {\"field1\": \"value1\"}}," +
       "{\"match\": {\"field2\": \"value2\"}}]}}")
List<YourEntity> findByCustomQuery();
  • 范围查询
@Query("{\"range\": {\"age\": {\"gte\": 18, \"lte\": 30}}}")
List<YourEntity> findByAgeRange();
  •  排序查询
@Query("{\"match_all\": {}}")
List<YourEntity> findAllByOrderByFieldAsc();
b、注入 ElasticsearchOperations 操作类
    @Resource
    private ElasticsearchOperations esOperations;

    public List<AppInfoBO> getMonitorAppList() {
        SystemConfig systemConfig = getAppInfo();
        Criteria criteria = new Criteria(SystemConfig.BIZ).is(systemConfig.getBiz())
                .and(SystemConfig.CODE).is(systemConfig.getCode())
                .and(SystemConfig.STATUS).is(systemConfig.getStatus());
        // 时间倒序
        CriteriaQuery criteriaQuery = new CriteriaQuery(criteria);
        criteriaQuery.addSort(Sort.by(Sort.Direction.DESC,SystemConfig.CREATED_TIME));
        SearchHits<SystemConfig> searchHits = esOperations.search(criteriaQuery, SystemConfig.class);
        // 转换成 map
        return searchHits.stream().map(searchHit -> {
            SystemConfig systemConfigES = searchHit.getContent();
                return new AppInfoBO(systemConfigES.getKey(), (String) systemConfigES.getValue());
        }).collect(Collectors.toList());
    }

至此执行单元测试即可完成 

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值