Reputation: 187
I have elastic search index and I need total count of records that one of fields ("actual_start") is not-null how can I do this?
I have wrote this query and I want to append count of not-null actual start value to the result of my query:
$params = [
'index' => "appointment_request",
'body' => [
'from' => $request['from'],
'size' => $request['size'],
"query" => [
"bool"=>[
"must" => [
[
"term" => [
"doctor_id" => [
"value" => $request['doctor_id']
]
]
],
[
"match" => [
"book_date" => $request['book_date']
]
],
]
],
]
]
];
Upvotes: 3
Views: 4404
Reputation: 2993
Take a look at Exists Query
Try this:
GET <your-index>/_count
{
"query": {
"exists": {
"field": "actual_start"
}
}
}
Then you can read the count
value which will give you the total count of records that actual_start
is not-null.
You can also replace _count
with _search
and total
value under hits
will give you a total count (in case you also want the hits
) .
If you want to do the opposite (all records which actual_start
is null):
GET <your-index>/_count
{
"query": {
"bool": {
"must_not": [
{
"exists": {
"field": "actual_start"
}
}
]
}
}
}
UPDATE
If I understand you correctly you want to append your current query with the exists
query.
Example:
GET <your-index>/_search
{
"query": {
"bool": {
"must": [
{
<put-your-query-here>
},
{
"exists": {
"field": "actual_start"
}
}
]
}
}
}
Upvotes: 7