2

Currently I'm using this code which I had written to get the values from a field :

QueryBuilder qb = QueryBuilders.matchPhrasePrefixQuery("tagName", "june");
            SearchResponse response = esclient.prepareSearch(index).setTypes(type)
                    .setQuery(qb)
                    .execute().actionGet();

            SearchHit[] hits = response.getHits().getHits();
            System.out.println(hits.length);

            for (SearchHit hit : hits) {
                Map map = hit.getSource();
                System.out.println((String) map.get("tagName"));
                strings.add((String) map.get("tagName"));
            }

But is there an alternative to get distinct values from an index? Or can the above can be modified?

Aravind S
  • 463
  • 2
  • 9
  • 25
  • Please look in this question. if you are able to help https://stackoverflow.com/questions/59570496/elasticsearch-error-invalid-term-aggregator-order-path-key-unknown-aggregat – Siva Jan 02 '20 at 22:05

1 Answers1

1

You Can Use terms aggregation

This requires the field to be not analyzed.

    public static List<String> getDistinctAgg(TransportClient client, String indexName, String fieldName, int requiredSize) {
    List<String> values = new LinkedList<>();

    try {


        TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder(fieldName + "_Agg", ValueType.STRING);
        aggregationBuilder.field(fieldName);
        aggregationBuilder.bucketCountThresholds(new TermsAggregator.BucketCountThresholds(1, 1, requiredSize, 5));

        QueryBuilder query = QueryBuilders.matchAllQuery();


        SearchResponse response = client.prepareSearch(indexName)
                .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
                .setQuery(query).addAggregation(aggregationBuilder).setSize(0).get();
        if (response == null) return null;

        StringTerms terms = ((StringTerms) response.getAggregations().get(fieldName + "_Agg"));

        if (terms != null) {

            List<Terms.Bucket> buckets = terms.getBuckets();
            if (buckets != null && buckets.size() > 0) {


                for (Terms.Bucket bucket : buckets) {
                    values.add(bucket.getKeyAsString());
                }
            }
            return values;
        }

    } catch (Exception e) {
        return null;
    }
    return null;
}