Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

elasticsearch get documents total count in index using php API

I have created elasticsearch index:

$es = Elasticsearch\ClientBuilder::create()->build();

$params = [
    'index'=>'articles',
    'type'  => 'article'
];

for ($i=0; $i<30; $i++) {
    $params['body'] = [ 'title'=>'title '.$i, 'body'=>'text '.$i ];
    $response = $es->index($params);
}

So, 30 documents are added, now I need get total count of records. This works

$search_params= [
    'index'=>'articles',
    'type'  => 'article',
];

$query = $es->search($search_params);

echo $query['hith']['total'];

But as I've read, more efficient is using straight counting _count method.

My problem is, that I not understood how to implement _count in php API?

tried:

$search_params= [
    'index'=>'articles',
    'type'  => 'article',
    'body' => [ 
        'query'  => ['_count'=>[] ] 
    ]
];

$query = $es->search($search_params);

and several another variants, but don't get right one syntax.

Help?

like image 254
Oto Shavadze Avatar asked Jun 25 '26 15:06

Oto Shavadze


1 Answers

Have you tried $query = $es->count($search_params);

https://www.elastic.co/guide/en/elasticsearch/client/php-api/2.0/ElasticsearchPHP_Endpoints.html#Elasticsearch_Clientcount_count

like image 159
jay Avatar answered Jun 29 '26 19:06

jay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!