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;
    }
相关推荐
Elasticsearch3 天前
Elastic Observability 的跨项目搜索:通过一个查询搜索所有关联项目
elasticsearch
Elasticsearch3 天前
Elasticsearch 中的 agentic 工作流:暂停 AI agent 等待人工审批,72 小时后恢复
elasticsearch
Elastic 中国社区官方博客3 天前
jina-ocr-v1:在低成本 GPU 上实现更快速的文档解析
大数据·数据库·elasticsearch·搜索引擎·全文检索·jina
vx-程序开发3 天前
【计算机毕设】django校园跑腿服务系统83141
java·数据库·vue.js·spring boot·spring·elasticsearch·django
Elasticsearch4 天前
jina-ocr-v1:在低成本 GPU 上实现更快速的文档解析
elasticsearch
深海鱼肝油ya4 天前
向量数据库Elasticsearch(四)搜索文档——各种方式
人工智能·elasticsearch·向量数据库·es搜索文档·只能体·非结构化数据库
Elasticsearch4 天前
SNAP 支付错误检测:Elastic 如何帮助美国各州应对 FY2028 的处罚
elasticsearch
Elastic 中国社区官方博客4 天前
使用 Elasticsearch 和 Jina 进行 AI 视频搜索:精准找到你需要的视频片段秒数
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
Elasticsearch4 天前
2026 年唯一获得满分 Endpoint Prevention and Response(EPR)评分的是 Elastic
elasticsearch
Elasticsearch5 天前
没有小到无法记录的日志文件:Elastic Agent 如何跟踪低于 1 KiB 阈值的文件
elasticsearch