• java直接使用dsl语句查询ES


    pom

    <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-data-elasticsearchartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.datagroupId>
                <artifactId>spring-data-elasticsearchartifactId>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    客户端配置

    import lombok.Data;
    import org.apache.http.HttpHost;
    import org.elasticsearch.client.RestClient;
    import org.elasticsearch.client.RestClientBuilder;
    import org.elasticsearch.client.RestHighLevelClient;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
    
    
    
    @Configuration
    @Data
    public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {
        private String host="localhost";
        private Integer port=9200;
        @Override
        public RestHighLevelClient elasticsearchClient() {
            RestClientBuilder builder = RestClient.builder(new HttpHost(host,port));
            RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);
            return restHighLevelClient;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    dsl查询

    import com.alibaba.fastjson.JSONObject;
    import org.elasticsearch.action.search.SearchRequest;
    import org.elasticsearch.action.search.SearchResponse;
    import org.elasticsearch.client.RequestOptions;
    import org.elasticsearch.client.RestHighLevelClient;
    import org.elasticsearch.index.query.WrapperQueryBuilder;
    import org.elasticsearch.search.builder.SearchSourceBuilder;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.io.IOException;
    
    @RestController
    @RequestMapping("/es-client2")
    public class EsClientController2 {
        @Autowired
        private RestHighLevelClient rstHighLevelClient;
    
    
        @PostMapping("/query2")
        public SearchResponse userEsClient(
                @RequestBody JSONObject body
        ) throws IOException {
            String index_name = body.getOrDefault("index_name", "").toString();
            System.out.println("body:" + body);
    
            String json = "{\"query\": {\"match\": {\"title\": \"hello world\"}}}";
            //language=JSON5
            String dsl = "{\n" +
                    "  \"match_all\": {}\n" +
                    "}";
    
    
            WrapperQueryBuilder queryBuilder = new WrapperQueryBuilder(dsl);
            SearchRequest searchRequest = new SearchRequest();
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            // 设置分页
            searchSourceBuilder.query(queryBuilder).from((int) 1).size((int) 10);
            searchRequest.source(searchSourceBuilder).indices(index_name);
            SearchResponse searchResp = rstHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            // 计算返回的条数
    
    
            return searchResp;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    结果

    在这里插入图片描述

  • 相关阅读:
    自己在家给电脑重装系统Win10教程
    vue3 electron 打包后进程通信无效,开发环境正常
    外贸案例分享:我是这样“教”客户做事的!
    Go Web开发GoFrame+Vue+ElementUI框架实战教程
    稀碎从零算法笔记Day55-LeetCode:100291. 统计特殊字母的数量 II
    《动手学深度学习 Pytorch版》 9.7 序列到序列学习(seq2seq)
    分享:身份证读卡器java工程乱码解决办法
    CSS 字体:Font
    2023中国山东·第五届济南国际大健康产业博览会·山东健博会
    Effective第三版 中英 | 第二章 创建和销毁对象 | 固定资源首选使用依赖注入
  • 原文地址:https://blog.csdn.net/JavaBigData/article/details/134230068