ElasticSearch 8.x 版本如何使用 SearchRequestBuilder 检索

ElasticSearch

1、ElasticSearch学习随笔之基础介绍

2、ElasticSearch学习随笔之简单操作

3、ElasticSearch学习随笔之java api 操作

4、ElasticSearch学习随笔之SpringBoot Starter 操作

5、ElasticSearch学习随笔之嵌套操作

6、ElasticSearch学习随笔之分词算法

7、ElasticSearch学习随笔之高级检索

8、ELK技术栈介绍

9、Logstash部署与使用

10、ElasticSearch 7.x 版本使用 BulkProcessor 实现批量添加数据

11、ElasticSearch 8.x 弃用了 High Level REST Client,移除了 Java Transport Client,推荐使用 Elasticsearch Java API

12、ElasticSearch 8.x 使用 snapshot(快照)进行数据迁移

13、ElasticSearch 8.x 版本如何使用 SearchRequestBuilder 检索

ElasticSearch,创始人 Shay Banon(谢巴农)

本文主要讲解ElasticSearch 高级搜索实战,来满足复杂的业务场景,还是用 Kibana 来操作。


文章目录


前言

在之前的项目中都是用 SearchRequestBuilder 来构件 ElasticSearch 检索请求的,然后使用了新的 High Level Client 之后新的客户端构建查询是用了 SearchSourceBuilder 来构建检索请求的,若使使用新的检索方式请移步《Elasticsearch Java API 如何使用》查看。

那用 SearchSourceBuilder 进行构建查询时需要使用 RestHighLevelClient 客戶端来发送请求的,简单代码如下:

java 复制代码
RestHighLevelClient client = createClient();
SearchRequest searchRequest = new SearchRequest();
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.matchAllQuery());
searchRequest.source(sourceBuilder);
sourceBuilder.from(0).size(10);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

以上这种写法就是 High Level Client 常见的写法。

那如果特殊情况下,比如需要和老项目融合的时候,可能需要使用到 SearchRequestBuilder 来检索,该如何做呢?

就是用 ElasticsearchClient 来包装 RestHighLevelClient 来执行查询,真正的检索使用 RestHighLevelClient 来执行的,而 ElasticsearchClient 则是构建为了融合并初始化 SearchRequestBuilder 而创建的。

一:pom 依赖

java 复制代码
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.17.16</version>
</dependency>

二:初始化客户端

2.1 初始化 HighLevelClient 客户端

这里首先要初始化 HighLevelClient 客户端,因为真正的检索需要使用此客户端来执行的。

java 复制代码
/**
 * 初始化客户端
 * @return RestHighLevelClient
 */
private static RestHighLevelClient getClient(){
    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("Your username", "Your password"));
    RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost("192.168.*.*", 9200))
            .setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
    RestHighLevelClient highLevelClient = new RestHighLevelClient(restClientBuilder);
    return highLevelClient;
}

2.2 初始化 ElasticsearchClient 客户端

这里我们来初始化 ElasticsearchClient 客户端,为什么要初始化此客户端呢?ElasticsearchClient 就像是一个代理客户端,调用了 HighLevelClient 来执行查询。

这里,我们把 RestHighLevelClient 作为参数传入,用于封装。

java 复制代码
private static ElasticsearchClient getEsClient(RestHighLevelClient highLevelClient){
    return new ElasticsearchClient() {
        @Override
        public <Request extends ActionRequest, Response extends ActionResponse> ActionFuture<Response> execute(ActionType<Response> action, Request request) {
            if (action.equals(SearchAction.INSTANCE) && request instanceof SearchRequest) {
                return new ActionFuture<Response>() {
                    @Override
                    public Response actionGet() {
                        try {
                        	// 执行检索
                            return (Response) highLevelClient.search((SearchRequest) request, RequestOptions.DEFAULT);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        return null;
                    }

                    @Override
                    public Response actionGet(String timeout) {
                        try {
                            // 将字符串形式的时间转换为 TimeValue
                            TimeValue timeValue = TimeValue.parseTimeValue(timeout, "timeout");
                            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
                            sourceBuilder.timeout(timeValue);
                            SearchRequest searchRequest = new SearchRequest();
                            searchRequest.source(sourceBuilder);
                            // 执行检索
                            return (Response) highLevelClient.search((SearchRequest) request, RequestOptions.DEFAULT);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }

                    @Override
                    public Response actionGet(long timeoutMillis) {
                        try {
                            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
                            sourceBuilder.timeout(TimeValue.timeValueMillis(timeoutMillis));
                            SearchRequest searchRequest = new SearchRequest();
                            searchRequest.source(sourceBuilder);
                            // 执行检索
                            return (Response) highLevelClient.search((SearchRequest) request, RequestOptions.DEFAULT);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }

                    @Override
                    public Response actionGet(long timeout, TimeUnit unit) {
                        return null;
                    }

                    @Override
                    public Response actionGet(TimeValue timeout) {
                        return null;
                    }

                    @Override
                    public boolean cancel(boolean mayInterruptIfRunning) {
                        return false;
                    }

                    @Override
                    public boolean isCancelled() {
                        return false;
                    }

                    @Override
                    public boolean isDone() {
                        return false;
                    }

                    @Override
                    public Response get() throws InterruptedException, ExecutionException {
                        return null;
                    }

                    @Override
                    public Response get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
                        return null;
                    }
                };
            } else {
                throw new UnsupportedOperationException("Unsupported action: " + action.name());
            }
        }

        @Override
        public <Request extends ActionRequest, Response extends ActionResponse> void execute(ActionType<Response> action, Request request, ActionListener<Response> listener) {
            if (action.equals(SearchAction.INSTANCE) && request instanceof SearchRequest) {
                highLevelClient.searchAsync((SearchRequest) request, RequestOptions.DEFAULT, (ActionListener<SearchResponse>) listener);
            } else {
                throw new UnsupportedOperationException("Unsupported action: " + action.name());
            }
        }

        @Override
        public ThreadPool threadPool() {
            return null;
        }
    };
}

以上初始化的 ElasticsearchClient 客户端中,只实现了常用的部分 actionGet 方法,其他方法在使用时再实现,实现方法基本上都差不多。

三:执行查询

ElasticsearchClient 则是可以通过 SearchRequestBuilder 来调用执行检索,在 new SearchRequestBuilder() 时,ElasticsearchClient 作为参数传递,则直接可以通过 execute().actionGet() 来执行检索并得到结果了。

java 复制代码
public static void main(String[] args) {
    RestHighLevelClient highLevelClient = getClient();
    ElasticsearchClient esClient = getEsClient(highLevelClient);
    SearchRequestBuilder requestBuilder = new SearchRequestBuilder(esClient, SearchAction.INSTANCE);
    requestBuilder.setIndices("product_info");
    requestBuilder.setFrom(0);
    requestBuilder.setSize(10);
    requestBuilder.setQuery(QueryBuilders.matchAllQuery());
    requestBuilder.addAggregation(AggregationBuilders.terms("productName").field("productName.keyword"));
    // 执行检索并获取检索结果
    String s = requestBuilder.execute().actionGet().toString();
    System.out.println("检索结果:" + s);
    JSONObject jsonObject = JSONObject.parseObject(s);
    // 获取到 agg 统计结果
    JSONObject aggregations = jsonObject.getJSONObject("aggregations");
    System.out.println(aggregations);
    try {
        highLevelClient.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

执行结果:

bash 复制代码
检索结果:{"took":0,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":2,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"product_info","_id":"1001","_score":1.0,"_source":{"productName":"小米汽车","productDescription":"相当于保时捷特斯拉的小米超跑","color":"海蓝色","price":19}},{"_index":"product_info","_id":"1002","_score":1.0,"_source":{"productName":"小米手机","productDescription":"小米,智能手机,价格实惠便宜","color":"黑色","price":2999}}]},"aggregations":{"sterms#productName":{"doc_count_error_upper_bound":0,"sum_other_doc_count":0,"buckets":[{"key":"小米手机","doc_count":1},{"key":"小米汽车","doc_count":1}]}}}
统计结果:{"sterms#productName":{"doc_count_error_upper_bound":0,"sum_other_doc_count":0,"buckets":[{"doc_count":1,"key":"小米手机"},{"doc_count":1,"key":"小米汽车"}]}}
相关推荐
jwolf21 小时前
Elasticsearch向量搜索:从语义搜索到图搜图只有一步之遥
elasticsearch·搜索引擎·ai
你可以叫我仔哥呀2 小时前
ElasticSearch学习笔记三:基础操作(一)
笔记·学习·elasticsearch
hummhumm3 小时前
第 25 章 - Golang 项目结构
java·开发语言·前端·后端·python·elasticsearch·golang
java1234_小锋6 小时前
Elasticsearch中的节点(比如共20个),其中的10个选了一个master,另外10个选了另一个master,怎么办?
大数据·elasticsearch·jenkins
Elastic 中国社区官方博客6 小时前
Elasticsearch 开放推理 API 增加了对 IBM watsonx.ai Slate 嵌入模型的支持
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
我的运维人生6 小时前
Elasticsearch实战应用:构建高效搜索与分析平台
大数据·elasticsearch·jenkins·运维开发·技术共享
jwolf26 小时前
摸一下elasticsearch8的AI能力:语义搜索/vector向量搜索案例
人工智能·搜索引擎
Mephisto.java11 小时前
【大数据学习 | Spark】Spark的改变分区的算子
大数据·elasticsearch·oracle·spark·kafka·memcache
mqiqe11 小时前
Elasticsearch 分词器
python·elasticsearch
小马爱打代码11 小时前
Elasticsearch简介与实操
大数据·elasticsearch·搜索引擎