简体   繁体   中英

how to get count of not-null value based on specific field in Elasticsearch

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

                                ]
                            ],
                      
                        ]

                    ],
                ]
            ]
        ];

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"
          }
        }
      ]
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM