我在这里看到了帖子,你们为下面给出的链接提供了解决方案。
ElasticSearch Java API to get distinct values from the Query Builders
有什么方法可以使用org.elasticsearch.client.RestHighLevelClient
获得不同的电子邮件吗?
您能帮我吗,我尝试了很多方法但无法解决。但是我能够在SQL Workbench中实现相同的功能,并且在下面使用Kibana转换器给出了等效的json查询。
SELECT DISTINCT email_client.keyword
FROM email_reference;
等效的Elasticsearch查询如下:
{
"from": 0,
"size": 0,
"_source": {
"includes": ["email_client.keyword"],
"excludes": []
},
"stored_fields": "email_client.keyword",
"aggregations": {
"email_client.keyword": {
"terms": {
"field": "email_client.keyword",
"size": 200,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
}
}
}
}
因此,现在我想使用RestHighLevelClient进行此JSON查询,但我尝试过,但问题是RestHighLevelClient中没有prepareSearch(),使用RestHighLevelClient还有其他实现吗?
最佳答案
这应该与RestHighLevelClient一起使用:
MultiSearchRequest multiRequest = new MultiSearchRequest();
SearchRequest searchRequest = new SearchRequest();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.size(0); // return only aggregation results
searchSourceBuilder.aggregation(AggregationBuilders.terms("label_agg").field("email_client.keyword").size(200));
searchRequest.indices("email_reference"); // index name
searchRequest.source(searchSourceBuilder);
multiRequest.add(searchRequest);
MultiSearchResponse response = restHighLevelClient.msearch(multiRequest, RequestOptions.DEFAULT);
关于java - 如何在Elasticsearch中使用RestHighLevelClient获得不同的值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62346196/