| endpoint | count |
|---|---|
| lang | javascript |
| es_version | 9.3 |
| client | @elastic/elasticsearch@9.3.0 |
Use client.count() to get the number of documents matching a
query, without returning the documents themselves:
const response = await client.count({
index: "products",
query: { term: { category: "electronics" } },
});
console.log(`Electronics: ${response.count}`);Omit the query parameter to count all documents in an index:
const response = await client.count({ index: "products" });
console.log(`Total products: ${response.count}`);Pass a comma-separated string or an array of index names to count across multiple indices:
const response = await client.count({
index: ["products", "orders"],
});
console.log(`Total across indices: ${response.count}`);