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?

1

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.