对 Term 使用 Missing 聚合,而不是字段

Using a Missing aggregation for a Term, as opposed to a field

我希望这是问这个问题的正确地点。我正在尝试获取不包含特定字段的项目的聚合。它们是具有嵌套字段的项目,如下所示:

             "customFields": [
                 {
                     "key": "firstType",
                     "keywordValue": "1"
                 },
                 {
                     "key": "secondType",
                     "keywordValue": "A"
                 }
             ]

我一直在尝试使用缺少的聚合,如果我试图找到不包含值为 firstType 的键的所有项目,但运气不佳。我所做的查询是:

 "aggregations": {
       "custom.firstType": {
               "missing": {
                 "field": "firstType"
               },
               "aggregations": {
                 "values": {
                   "reverse_nested": {}
                 }
               }
             }
           }

不幸的是,这对我来说并不走运。我在看什么?

谢谢!

缺少聚合用于查找不包含给定字段(不是值)的文档

{
   "product_id": "p1",
   "price":100
}
{
   "product_id":"P2"
}

第二个文档不包含字段价格。要查找所有没有价格的产品,您将使用缺失聚合。

在您的情况下,您希望对没有特定值的文档进行聚合,您需要使用带有过滤器子聚合的嵌套聚合

映射:

PUT index19
{
  "mappings": {
    "properties": {
      "customFields": {
        "type": "nested",
        "properties": {
          "key": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword"
              }
            }
          },
          "keywordValue":{
            "type":"integer"
          }
        }
      }
    }
  }
}

查询:

GET index19/_search
{
  "size": 0,
  "aggs": {
    "without_firsttype": {
      "nested": {
        "path": "customFields"
      },
      "aggs": {
        "key_check": {
          "filter": {
            "bool": {
              "must_not": [
                {
                  "term": {
                    "customFields.key.keyword": {
                      "value": "firstType"
                    }
                  }
                }
              ]
            }
          },
          "aggs": {
            "Document": {
              "reverse_nested": {}
            }
          }
        }
      }
    }
  }
}

结果:

"aggregations" : {
    "without_firsttype" : {
      "doc_count" : 4,
      "key_check" : {
        "doc_count" : 3,
        "Document" : {
          "doc_count" : 2
        }
      }
   }
}