SpringBoot3.x下如何使用es进行数据查询

文章目录

概要

SpringBoot3中的elasticsearch版本已经来到8.x版本,相比较于SpringBoot2中的7.x版本相关写法已经发生改变。在SpringBoot2中会使用ElasticsearchRestTemplate和es进行交互,但这个类在SpringBoot3中已经被移除,需要使用ElasticsearchTemplate或者ElasticsearchClient和es进行交互。下面介绍这两个类的基本使用方式。

ElasticsearchTemplate

java 复制代码
    @Test
    void test_1() {
        NativeQuery query = new NativeQueryBuilder()
                .withPageable(Pageable.ofSize(100))
                //使用lambda表达式使其更符合es语句的写法
                .withQuery(q -> q
                        .bool(b -> b
                                .filter(f -> f
                                        .term(t -> t
                                                .field("Name")
                                                .value("test")))))
                .withSort(s -> s
                        .field(f -> f
                                .field("Name")
                                .order(SortOrder.Desc)))
                .build();
        SearchHits<User> search = elasticsearchTemplate.search(query, User.class, IndexCoordinates.of("test-topic"));
        List<SearchHit<User>> searchHits = search.getSearchHits();
        for (SearchHit<User> searchHit : searchHits) {
            System.out.println(searchHit.getContent().getName());
        }
    }

以上的es查询语句等价于

json 复制代码
{
    "query": {
        "bool": {
            "filter": [
                {
                    "term": {
                        "Name": {
                            "value": "test"
                        }
                    }
                }
            ]
        }
    },
    "from": 0,
    "size": 100,
    "sort": [
        {
            "Name": {
                "order": "desc"
            }
        }
    ]
}

ElasticsearchClient

java 复制代码
    @Test
    void test_2() throws IOException {
        Function<SearchRequest.Builder, ObjectBuilder<SearchRequest>> query = fn-> fn
                .index("test-topic")
                .size(100)
                //使用lambda表达式使其更符合es语句的写法
                .query(q -> q
                                .bool(b -> b
                                        .filter(f -> f
                                                .term(t -> t
                                                        .field("Name")
                                                        .value("test")))))
                .sort(s -> s
                        .field(f -> f
                                .field("Name")
                                .order(SortOrder.Desc)));
        SearchResponse<User> search = elasticsearchClient.search(query, User.class);
        List<Hit<User>> hits = search.hits().hits();
        for (Hit<User> hit : hits) {
            System.out.println(hit.source().getName());
        }
    }

小结

1.依赖引入

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

2.ElasticsearchTemplate和ElasticsearchClient已经自动装配,业务类中直接注入即可。

3.User类

java 复制代码
    /**
     * test_1:使用第一种测试时,实体属性和es字段属性不对应时需要使用@Field(org.springframework.data.elasticsearch.annotations.Field)注解标识
     * test_2:使用第二种测试时,实体属性和es字段属性不对应时需要使用@JsonProperty(com.fasterxml.jackson.annotation.JsonProperty)注解标识
     */
    @Data
    public class User {

        @Id
        private String id;

        @Field("Name")
        @JsonProperty("Name")
        private String name;
    }
相关推荐
Elasticsearch12 小时前
Elasticsearch:构建一个 AI 驱动的电子邮件钓鱼检测
elasticsearch
alan072113 小时前
【Java + Elasticsearch全量 & 增量同步实战】
java·elasticsearch·jenkins
Elastic 中国社区官方博客14 小时前
Elasticsearch:你是说,用于混合搜索(hybrid search)
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
Elastic 中国社区官方博客18 小时前
在 Kibana 中可视化你的 Bosch Smart Home 数据
大数据·运维·elasticsearch·搜索引擎·信息可视化·全文检索·kibana
Dxy123931021619 小时前
Elasticsearch 聚合入门:像 Excel 透视表一样分析数据
elasticsearch·excel
Elastic 中国社区官方博客19 小时前
通过 Elasticsearch 中的 function score query 按利润和受欢迎程度提升电商搜索效果
大数据·数据库·elasticsearch·搜索引擎·全文检索
管理大亨20 小时前
智慧农业ELK落地方案:数据驱动精准农业
大数据·redis·python·elk·elasticsearch
神的泪水20 小时前
深度解析:基于 DeepSeek V3.2 与 Claude Code 构建终端智能体开发环境
大数据·elasticsearch·搜索引擎
Dxy123931021620 小时前
Elasticsearch 查询入门:像查字典一样简单
大数据·elasticsearch