3

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']

                                ]
                            ],
                      
                        ]

                    ],
                ]
            ]
        ];
Farzane Khazaei
  • 147
  • 1
  • 2
  • 12

1 Answers1

7

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"
          }
        }
      ]
    }
  }
}
Assael Azran
  • 2,863
  • 1
  • 9
  • 13
  • thank you but i have another query and I need to append count of not-null value of actual_start field to it, also i need total to have count of all records in my index whould you please help me how can i do it? – Farzane Khazaei Sep 13 '20 at 08:08
  • Can you please share your query? what have you tried so far? – Assael Azran Sep 13 '20 at 08:28
  • What do you mean "append count of not-null actual start value to the result of my query"? if you want the count result of not-null actual_start you will have to run the `exists` query as a different query. Elastic Search doesn't support subqueries. If you intended to filter your query with the not-null actual_start field then use the query I've written under my update. – Assael Azran Sep 13 '20 at 09:00
  • If you find my solution helpful please feel free to upvote/accept the answer. – Assael Azran Sep 13 '20 at 11:41