Skip to content

Latest commit

 

History

History
43 lines (33 loc) · 911 Bytes

File metadata and controls

43 lines (33 loc) · 911 Bytes
endpoint count
lang php
es_version 9.3
client elasticsearch/elasticsearch==9.3.0

Elasticsearch 9.3 count endpoint (PHP example)

Use $client->count() to get the number of documents matching a query, without returning the documents themselves:

$response = $client->count([
    'index' => 'products',
    'body' => [
        'query' => ['term' => ['category' => 'electronics']],
    ],
]);

echo "Electronics: {$response['count']}\n";

Counting all documents

Omit the body parameter to count all documents in an index:

$response = $client->count(['index' => 'products']);
echo "Total products: {$response['count']}\n";

Multiple indices

Pass a comma-separated string or an array of index names to count across multiple indices:

$response = $client->count([
    'index' => 'products,orders',
]);
echo "Total across indices: {$response['count']}\n";