【问题标题】:how to get count of not-null value based on specific field in Elasticsearch如何根据 Elasticsearch 中的特定字段获取非空值的计数
【发布时间】:2020-12-31 05:03:34
【问题描述】:

我有弹性搜索索引,我需要其中一个字段(“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']

                                ]
                            ],
                      
                        ]

                    ],
                ]
            ]
        ];

【问题讨论】:

    标签: elasticsearch elasticsearch-aggregation


    【解决方案1】:

    看看Exists Query

    试试这个:

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

    然后您可以读取count 值,该值将为您提供actual_start 不为空的记录总数。

    您还可以将_count 替换为_searchtotal 下的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"
              }
            }
          ]
        }
      }
    }
    

    【讨论】:

    • 谢谢,但我有另一个查询,我需要将actual_start 字段的非空值计数附加到它,我还需要总计有我的索引中所有记录的计数,请你帮帮我我该怎么做?
    • 您能分享您的查询吗?到目前为止你尝试过什么?
    • 您是什么意思“将非空实际起始值的计数附加到我的查询结果”?如果您想要 not-null actual_start 的计数结果,则必须将 exists 查询作为不同的查询运行。 Elastic Search 不支持子查询。如果您打算使用非空 actual_start 字段过滤查询,请使用我在更新下编写的查询。
    • 如果您觉得我的解决方案有帮助,请随时投票/接受答案。
    猜你喜欢
    • 1970-01-01
    • 2023-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-06
    • 1970-01-01
    • 2018-10-22
    相关资源
    最近更新 更多