| endpoint | count |
|---|---|
| lang | java |
| es_version | 9.3 |
| client | co.elastic.clients:elasticsearch-java:9.3.0 |
Use client.count() to get the number of documents matching a
query, without returning the documents themselves:
var response = client.count(c -> c
.index("products")
.query(q -> q
.term(t -> t.field("category").value("electronics"))
)
);
System.out.println("Electronics: " + response.count());Omit the query to count all documents in an index:
var response = client.count(c -> c.index("products"));
System.out.println("Total products: " + response.count());Pass multiple index names to count across indices:
var response = client.count(c -> c
.index("products", "orders")
);
System.out.println("Total across indices: " + response.count());