Get all value for a field with the JAVA API

Hello,

How can i get all the value that correspond to a specific field ?
For example i want all the existing value for the field "affaireName", check the exemple below :

SearchRequest request = SearchRequest.of(a -> a.index(index)
                                        .size(0)
                                        .aggregations("price-histogram", hist -> hist.histogram(h -> h
                                        .field("affaireName")
                                        .interval(100.0)))
                                        .query(new Query.Builder().bool(boolQuery).build()));

SearchResponse<Void> search = client.search(request, Void.class);

I already try this but it doesn’t work.

Bonjour :wink:

I think you are looking for a terms aggregation.

Though it won't give exactly "all values" unless the number of distinct values is low...
But have a look at it. It will e a good start I think.

Merci :grin:

I tried to use a terms aggregation and implemented it a couple but i dont understand how to add it to the query.
Can you help me ?

                        TermsAggregation termsAggregation = new TermsAggregation.Builder().field("affaireName")
                                        .size(100)
                                        .build();

                        BoolQuery boolQuery = new BoolQuery.Builder()
                                        .filter(new Query.Builder().range(rangeEchelle).build())
                                        .filter(new Query.Builder().range(rangeDate).build())
                                        .must(new Query.Builder().queryString(queryString).build())
                                        .build();

                        SearchRequest request = SearchRequest.of(a -> a.index(index)
                                        .size(0)
                                        .aggregations( termsAggregation)
                                        .query(new Query.Builder().bool(boolQuery).build()));

                        SearchResponse<Void> search = client.search(
                                        request,

                                        Void.class);

In the docs, it says you need to use something like:

GET /_search
{
  "aggs": {
    "affaires": {
      "terms": { "field": "affaireName", "size": 100 }
    }
  }
}

Which translates to Java to something like this (I did not test it):

SearchResponse<Void> response = esClient.search(b -> b
    .index("index")
    .size(0) 
    .query(q -> q.match(t -> t   
            .field("name")  
            .query(searchText)
        )
    )
    .aggregations("affaires", a -> a 
        .terms(h -> h 
            .field("affaireName")
            .size(100)
        )
    ),
    Void.class 
);

Great thanks my request is working now, I was wrong about the name of the field.

Now i have this code :

                        SearchResponse<Void> response = client.search(b -> b
                                        .index(index)
                                        .size(0)
                                        .query(new Query.Builder().bool(boolQuery).build())
                                        .aggregations("affaires", a -> a
                                                        .terms(h -> h
                                                                        .field("affaire")
                                                                        .size(100))),
                                        Void.class);

                                System.out.println("Facet affaires");
                                System.out.println(response.aggregations().get("affaires").sterms().buckets().array());
                                response.aggregations().get("affaires").sterms().buckets().array().forEach((bucket) -> {
                                        System.out.println(bucket.key() + " : " + bucket.docCount());
                                });

no matter what field i choose, the bucket array is empty. Do you know where could my problem come from ?

May be the mapping.

But could you reproduce the problem using the Kibana Console?