2

I saw the post here where you guys gave solution for the link given below. ElasticSearch Java API to get distinct values from the Query Builders

Is there any way to achieve distinct emails using org.elasticsearch.client.RestHighLevelClient? Could you please help me on this, i tried so many ways but could not able to solve it. But I am able to achieve the same in SQL Workbench and the equivalent json query is given below using Kibana translator.

SELECT DISTINCT email_client.keyword
FROM email_reference;

Equivalent Elasticsearch query given below:

{
  "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"
          }
        ]
      }
    }
  }
}

So now i wanted form this JSON query using RestHighLevelClient, I have tried but the problem is prepareSearch() is not there in RestHighLevelClient is there any other to achieve using RestHighLevelClient?

DB Reddy
  • 21
  • 2

2 Answers2

0

This should work with the 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);
0

For elasticsearch-rest-high-level-client with version 7.11.2, below example worked for me. You need to set an aggregation for a field with keyword.

    private List<String> queryForDistinctMetadata(String aggregationKey, String field) throws IOException {

        var aggregationBuilder = AggregationBuilders
                .terms(aggregationKey)
                .field(field);

        var searchSourceBuilder = new SearchSourceBuilder()
                .aggregation(aggregationBuilder)
                .size(0);

        var searchRequest = new SearchRequest()
                .indices("<your index here>")
                .source(searchSourceBuilder);

        var response = client.search(searchRequest, RequestOptions.DEFAULT);

        var aggregation = (ParsedStringTerms) response.getAggregations().get(aggregationKey);

        return aggregation.getBuckets()
                .parallelStream()
                .map(Terms.Bucket::getKeyAsString)
                .collect(Collectors.toList());
    }
Ahmet Koylu
  • 159
  • 1
  • 3
  • 14