# How to get count of specific value in elasticsearch java clinet

**URL:** <https://discuss.elastic.co/t/how-to-get-count-of-specific-value-in-elasticsearch-java-clinet/352465>\
**Category:** Elasticsearch\
**Tags:** aggregations\
**Created:** [February 3, 2024, 4:07pm UTC](https://discuss.elastic.co/t/how-to-get-count-of-specific-value-in-elasticsearch-java-clinet/352465 "2024-02-03T16:07:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abdalrazag\_Al-Shrufa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdalrazag_al-shrufa/32/122177_2.png) [@Abdalrazag\_Al-Shrufa](https://discuss.elastic.co/u/Abdalrazag_Al-Shrufa)\
**Post date:** [February 3, 2024, 4:07pm UTC](https://discuss.elastic.co/t/how-to-get-count-of-specific-value-in-elasticsearch-java-clinet/352465/1 "2024-02-03T16:07:24Z")

</div>

I am using elasticsearch-java 8.12, and I want to get the count of specific value,  
in my example I have log index, and there is a field called result, I want to get the number of documents that the result field is "correct", how to do that?  
I made a search but the document does not contains an explanation for it

---

<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:** [February 5, 2024, 9:43pm UTC](https://discuss.elastic.co/t/how-to-get-count-of-specific-value-in-elasticsearch-java-clinet/352465/2 "2024-02-05T21:43:49Z")

</div>

Something like this should work:

```auto
try {
    client.indices().delete(dir -> dir.index("search-data"));
} catch (ElasticsearchException ignored) { }
client.index(ir -> ir.index("search-data").id("1").withJson(new StringReader("{\"result\":\"correct\"}")));
client.indices().refresh(rr -> rr.index("search-data"));
SearchResponse<Void> response = client.search(sr -> sr
                .index("search-data")
                .query(q -> q.match(mq -> mq.field("result").query("correct"))),
        Void.class);
logger.info("response.hits.total.value = {}", response.hits().total().value());

```

---

<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:** [March 4, 2024, 9:44pm UTC](https://discuss.elastic.co/t/how-to-get-count-of-specific-value-in-elasticsearch-java-clinet/352465/3 "2024-03-04T21:44:05Z")

</div>

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