ElasticSearch:具有空嵌套数组的文档计数的聚合

hwamh0ep  于 2022-11-02  发布在  ElasticSearch
关注(0)|答案(1)|浏览(151)

问题-我想要计算嵌套数组MATCHES为空(如“MATCHES”)的文档数:[ ]。我的文档结构如下所示(为简单起见,显示了两条记录)-

{
  "hits": [
    {
      "_type": "_doc",
      "_id": "ef0a2c44179a513476b080cc2a585d95",
      "_source": {
        "DIVISION_NUMBER": 44,
        "MATCHES": [
          {
            "MATCH_STATUS": "APPROVED",
            "UPDATED_ON": 1599171303000
          }
        ]
      }
    },
    {
      "_type": "_doc",
      "_id": "ef0a2c44179a513476b080cc2a585d95",
      "_source": {
        "DIVISION_NUMBER": 44,
        "MATCHES": [ ]
      }
    }
  ]
}

已尝试解决方案-我尝试了以下不同的聚合方法(解决方法)(empty-match-agg 1、empty-match-agg 2...),但这些方法都没有给出正确的结果。请帮助!

"aggs": {
    "sku": {
      "nested": {
        "path": "MATCHES"
      },
      "aggs": {
        "empty-match-agg1": {
          "missing": {
            "field": "MATCHES"
          }
        },
        "empty-match-agg2": {
          "terms": {
            "field": "MATCHES",
            "missing": "N/A"
          }
        },
        "empty-match-agg3": {
          "sum": {
            "script": {
              "lang": "painless",
              "source": "params['_source'].MATCHES"
            }
          }
        },
        "empty-match-agg4": {
          "filter": {
            "bool": {
              "must_not": {
                "nested": {
                  "query": {
                    "match_all": {}
                  },
                  "path": "MATCHES"
                }
              }
            }
          }
        },
        "empty-match-agg5": {
          "terms": {
            "field": "MATCHES"
          }
        }
      }
    }
  }
ebdffaop

ebdffaop1#

缺少的聚合暂时不支持嵌套字段。到目前为止存在open问题。
要获取空匹配项的计数,可以使用筛选器聚合,并将嵌套查询 Package 到布尔查询的must_not子句中。

{
  "aggs": {
    "missing_matches_agg": {
      "filter": {
        "bool": {
          "must_not": {
            "nested": {
              "query": {
                "match_all": {}
              },
              "path": "MATCHES"
            }
          }
        }
      }
    }
  }
}

相关问题