# Get all value for a field with the JAVA API

**URL:** <https://discuss.elastic.co/t/get-all-value-for-a-field-with-the-java-api/322566>\
**Category:** Elasticsearch\
**Created:** [January 5, 2023, 4:45pm UTC](https://discuss.elastic.co/t/get-all-value-for-a-field-with-the-java-api/322566 "2023-01-05T16:45:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dorian](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dorian/32/99119_2.png) [@Dorian](https://discuss.elastic.co/u/Dorian)\
**Post date:** [January 5, 2023, 4:45pm UTC](https://discuss.elastic.co/t/get-all-value-for-a-field-with-the-java-api/322566/1 "2023-01-05T16:45:14Z")

</div>

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 :

```auto
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.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 5, 2023, 5:20pm UTC](https://discuss.elastic.co/t/get-all-value-for-a-field-with-the-java-api/322566/2 "2023-01-05T17:20:31Z")

</div>

Bonjour 😉

I think you are looking for [a terms aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html).

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.

---

<div class="post-metadata">

**Author:** ![Dorian](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dorian/32/99119_2.png) [@Dorian](https://discuss.elastic.co/u/Dorian)\
**Post date:** [January 5, 2023, 7:58pm UTC](https://discuss.elastic.co/t/get-all-value-for-a-field-with-the-java-api/322566/3 "2023-01-05T19:58:48Z")

</div>

Merci 😁

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 ?

```auto
                        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);

```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 6, 2023, 7:33am UTC](https://discuss.elastic.co/t/get-all-value-for-a-field-with-the-java-api/322566/4 "2023-01-06T07:33:35Z")

</div>

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

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

```

Which [translates to Java](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/aggregations.html) to something like this (I did not test it):

```auto
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 
);

```

---

<div class="post-metadata">

**Author:** ![Dorian](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dorian/32/99119_2.png) [@Dorian](https://discuss.elastic.co/u/Dorian)\
**Post date:** [January 6, 2023, 12:22pm UTC](https://discuss.elastic.co/t/get-all-value-for-a-field-with-the-java-api/322566/5 "2023-01-06T12:22:30Z")

</div>

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

Now i have this code :

```auto
                        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 ?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 6, 2023, 6:19pm UTC](https://discuss.elastic.co/t/get-all-value-for-a-field-with-the-java-api/322566/6 "2023-01-06T18:19:21Z")

</div>

May be the mapping.

But could you reproduce the problem using the Kibana Console?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 3, 2023, 6:20pm UTC](https://discuss.elastic.co/t/get-all-value-for-a-field-with-the-java-api/322566/7 "2023-02-03T18:20:16Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
