ElasticSearch 1x - 根据对象条件聚合

ElasticSearch 1x - aggregate on object conditions

我想聚合具有内部对象的数据。例如:

{
    "_index": "product_index-en",
    "_type": "elasticproductmodel",
    "_id": "000001111",
    "_score": 6.3316255,
    "_source": {
        "productId": "11111111111",
        "productIdOnlyLetterAndDigit": "11111111111",
        "productIdOnlyDigit": "11111111111",
        "productNumber": "11111111111",
        "name": "Glow Plug",
        "nameOnlyLetterAndDigit": "glowplug",
        "productImageLarge": "11111111111.jpg",
        "itemGroupId": "11111",
        "relatedProductIds": [],
        "dataAreaCountries": [
            "fra",
            "pol",
            "uk",
            "sie",
            "sve",
            "atl",
            "ita",
            "hol",
            "dk"
        ],
        "oemItems": [
            {
                "manufactorName": "BERU",
                "manufacType": "0"
            },
            {
                "manufactorName": "LUCAS",
                "manufacType": "0"
            }
        ]
    }
}

我需要能够聚合 oemItems.manufactorName 值,但仅当 oemItems.manufacType 为“0”时。我已经尝试了很多示例,例如此处接受的示例 ( ),但我似乎无法理解它。

我试过跟随,希望它会首先聚合在 manufacType 上,它确实如此,然后是每种类型的 manufactorName,它似乎显示正确的命中数。但是,manufactorName 的存储桶是空的:

GET /product_index-en/_search
{
"size": 0, 
  "aggs": {
    "baked_goods": {
      "nested": {
        "path": "oemItems"
      },
      "aggs": {
        "test1": {
          "terms": {
            "field": "oemItems.manufacType",
            "size": 500
          },
          "aggs": {
            "test2": {
              "terms": {
                "field": "oemItems.manufactorName",
                "size": 500
              }
            }
          }
        }
      }
    }
  }
}

结果:

{
   "took": 27,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 471214,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "baked_goods": {
         "doc_count": 677246,
         "test1": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
               {
                  "key": "0",
                  "doc_count": 436557,
                  "test2": {
                     "doc_count_error_upper_bound": 0,
                     "sum_other_doc_count": 0,
                     "buckets": []
                  }
               },
               {
                  "key": "1",
                  "doc_count": 240689,
                  "test2": {
                     "doc_count_error_upper_bound": 0,
                     "sum_other_doc_count": 0,
                     "buckets": []
                  }
               }
            ]
         }
      }
   }
}

我还尝试添加一个嵌套的术语过滤器,以仅查看带有以下查询的 manufacType 1 的 oemItems。然而,它 returns oemItems 包含 manufacType 1 的对象,这意味着产品中的 oemItems 仍然包含 1 或 0 manufacType。我看不出如何对这个响应进行聚合只会 return oemItems.manufactorName 其中 oemItems.manufacType 是 0

GET /product_index-en/_search 
{
        "query" : { "match_all" : {} },
        "filter" : {
            "nested" : {
                "path" : "oemItems",
                "filter" : {
                    "bool" : {
                        "must" : [
                            {
                                "term" : {"oemItems.manufacType" : "1"}
                            }
                        ]
                    }
                }
            }
        }    
}

到目前为止开端不错。就这样试试吧:

POST /product_index-en/_search
{
  "size": 0,  
  "query": {
     "nested": {
        "path": "oemItems",
        "query": {
           "term": {
              "oemItems.manufacType": "0"
           }
        }
     }
  },
  "aggs": {
    "baked_goods": {
      "nested": {
        "path": "oemItems"
      },
      "aggs": {
        "test1": {
          "terms": {
            "field": "oemItems.manufactorName",
            "size": 500
          }
        }
      }
    }
  }
}