簡體   English   中英

如何根據 Elasticsearch 中的特定字段獲取非空值的計數

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

我有彈性搜索索引,我需要字段之一(“actual_start”)不為空的記錄總數,我該怎么做?

我已經寫了這個查詢,我想將非空實際起始值的計數附加到我的查詢結果中:

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

                                ]
                            ],
                      
                        ]

                    ],
                ]
            ]
        ];

查看Exists 查詢

嘗試這個:

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

然后您可以讀取count數值,該值將為您提供actual_start不為空的記錄總數。

您還可以將_count替換為_searchhits下的total價值將為您提供總計數(如果您還需要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"
          }
        }
      ]
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM