ElasticSearch 1x - 聚合对象条件

时间:2016-06-21 06:45:22

标签: elasticsearch nest

我想聚合数据,它有内部对象。例如:

{
    "_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”的位置。我已经尝试了很多例子,例如这里接受的例子(Elastic Search Aggregate into buckets on conditions),但我似乎无法绕过它。

我已经尝试过了,希望它首先会对manufacType进行加强,然后对每种类型进行makeorName,它似乎显示正确的命中数。但是,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。但是,它返回其中oemItems包含manufacType 1的对象,这意味着它在产品中的oemItems仍然包含1或0 manufacType。我没有看到如何在此响应上执行聚合只返回oemItems.manufactorName,其中oemItems.manufacType为0

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

1 个答案:

答案 0 :(得分: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
          }
        }
      }
    }
  }
}