elasticsearch中如何根据特定字段获取非空值计数

mbzjlibv  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(1)|浏览(525)

我有ElasticSearch索引,我需要的记录总数,其中一个字段(“实际\开始”)不为空我怎么能做到这一点?
我已经编写了这个查询,我想在查询结果中附加not null实际开始值的计数:

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

                                ]
                            ],

                        ]

                    ],
                ]
            ]
        ];
b1zrtrql

b1zrtrql1#

看看exists查询
试试这个:

GET <your-index>/_count
{
  "query": {
    "exists": {
      "field": "actual_start"
    }
  }
}

然后你就可以阅读 count 值,它将为您提供 actual_start 不为空。
您也可以替换 _count_search 以及 total 价值低于 hits 会给你一个总数(如果你也想要 hits ) .
如果你想做相反的事情(所有记录 actual_start 为空):

GET <your-index>/_count
{
  "query": {
    "bool": {
      "must_not": [
        {
          "exists": {
            "field": "actual_start"
          }
        }
      ]
    }
  }
}

更新
如果我理解正确,您希望在当前查询中附加 exists 查询。
例子:

GET <your-index>/_search
{
  "query": {
    "bool": {
      "must": [
        {
          <put-your-query-here>
        },
        {
          "exists": {
            "field": "actual_start"
          }
        }
      ]
    }
  }
}

相关问题