OTARIKI OTARIKI - 1 month ago 25
PHP Question

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?

jay jay
Answer
Comments