弹性动态聚合|过滤器结构

时间:2016-09-28 14:49:43

标签: elasticsearch

我正在使用Elasticsearch聚合,这是我的映射:

  "products": {
    "mappings": {
     "product": {
         ...
     "properties": {
         "skus": {
        "type": "nested",
        "properties": {
          "availabilities": {
            "type": "nested",
            "properties": {
              "crossed_price": {
                "type": "float"
              },
              "discount": {
                "type": "float"
              },
              "price": {
                "type": "float",
                "fields": {
                  "raw": {
                    "type": "string",
                    "index": "not_analyzed"
                  }
                }
              },
              "price_per_unit": {
                "type": "nested",
                "properties": {
                  "crossed_price": {
                    "type": "float"
                  },
                  "price": {
                    "type": "float"
                  },
                  "unit": {
                    "type": "string",
                    "fields": {
                      "raw": {
                        "type": "string",
                        "index": "not_analyzed"
                      }
                    }
                  }
                }
              },
              "shop_id": {
                "type": "string",
                "fields": {
                  "raw": {
                    "type": "string",
                    "index": "not_analyzed"
                  }
                }
              },
              "stock": {
                "type": "float"
              }
            }
          },
          "desc": {
            "type": "nested",
            "properties": {
              "lang": {
                "type": "string",
                "fields": {
                  "raw": {
                    "type": "string",
                    "index": "not_analyzed"
                  }
                }
              },
              "text": {
                "type": "string",
                "fields": {
                  "raw": {
                    "type": "string",
                    "index": "not_analyzed"
                  }
                }
              }
            }
          },
          "ean": {
            "type": "string",
            "fields": {
              "raw": {
                "type": "string",
                "index": "not_analyzed"
              }
            }
          },
          "id": {
            "type": "string",
            "fields": {
              "raw": {
                "type": "string",
                "index": "not_analyzed"
              }
            }
          },
          "medias": {
            "type": "nested",
            "properties": {
              "generated": {
                "type": "boolean"
              },
              "hash": {
                "type": "string",
                "fields": {
                  "raw": {
                    "type": "string",
                    "index": "not_analyzed"
                  }
                }
              },
              "source": {
                "type": "string",
                "fields": {
                  "raw": {
                    "type": "string",
                    "index": "not_analyzed"
                  }
                }
              },
              "type": {
                "type": "string",
                "fields": {
                  "raw": {
                    "type": "string",
                    "index": "not_analyzed"
                  }
                }
              }
            }
          },
          "name": {
            "type": "nested",
            "properties": {
              "lang": {
                "type": "string",
                "fields": {
                  "raw": {
                    "type": "string",
                    "index": "not_analyzed"
                  }
                }
              },
              "text": {
                "type": "string",
                "fields": {
                  "raw": {
                    "type": "string",
                    "index": "not_analyzed"
                  }
                }
              }
            }
          },
          "notify_stock": {
            "type": "integer"
          },
          "popularity": {
            "type": "float"
          },
          "specs": {
            "type": "nested",
            "properties": {
              "id": {
                "type": "string"
              },
              "spec": {
                "type": "nested",
                "properties": {
                  "lang": {
                    "type": "string",
                    "fields": {
                      "raw": {
                        "type": "string",
                        "index": "not_analyzed"
                      }
                    }
                  },
                  "text": {
                    "type": "string",
                    "fields": {
                      "raw": {
                        "type": "string",
                        "index": "not_analyzed"
                      }
                    }
                  }
                }
              },
              "unit": {
                "type": "nested",
                "properties": {
                  "lang": {
                    "type": "string",
                    "fields": {
                      "raw": {
                        "type": "string",
                        "index": "not_analyzed"
                      }
                    }
                  },
                  "text": {
                    "type": "string",
                    "fields": {
                      "raw": {
                        "type": "string",
                        "index": "not_analyzed"
                      }
                    }
                  }
                }
              },
              "value": {
                "type": "nested",
                "properties": {
                  "lang": {
                    "type": "string",
                    "fields": {
                      "raw": {
                        "type": "string",
                        "index": "not_analyzed"
                      }
                    }
                  },
                  "text": {
                    "type": "string",
                    "fields": {
                      "raw": {
                        "type": "string",
                        "index": "not_analyzed"
                      }
                    }
                  }
                }
              }
            }
          },
          "status": {
            "type": "string"
          },
          "unit": {
            "properties": {
              "lang": {
                "type": "string"
              },
              "text": {
                "type": "string"
              }
            }
          }
        }
      }
   ...
}

我希望针对产品进行聚合,以根据规格值构建过滤器。我正在处理多语言规范 - >获取基于specs.spec.lang的多语言过滤器。

这是我的要求:

        {
             "size": 0,
               "aggs": {
            "skus": {
              "nested": {
                "path": "skus"
              },
              "aggs": {
                "skus.specs": {
                  "nested": {
                    "path": "skus.specs"
                  },
                  "aggs": {
                    "skus.specs.spec": {
                      "nested": {
                        "path": "skus.specs.spec"
                      },
                      "aggs": {
                        "skus.specs.spec.text": {
                          "terms": {
                            "field": "skus.specs.spec.text.raw"
                          },
                          "aggs": {
                            "skus.specs.value": {
                              "nested": {
                                "path": "skus.specs.value"
                              },
                              "aggs": {
                                "skus.specs.value.text": {
                                  "terms": {
                                    "field": "skus.specs.value.text.raw"
                                  }
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }

问题:嵌套aggs的值是混合的

{
              "took": 52,
              "timed_out": false,
              "_shards": {
                "total": 3,
                "successful": 3,
                "failed": 0
              },
              "hits": {
                "total": 5020,
                "max_score": 0,
                "hits": []
              },
              "aggregations": {
                "skus": {
                  "doc_count": 10258,
                  "skus.specs": {
                    "doc_count": 41678,
                    "skus.specs.spec": {
                      "doc_count": 41678,
                      "skus.specs.spec.text": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                          {
                            "key": "Contenance",
                            "doc_count": 9603,
                            "skus.specs.value": {
                              "doc_count": 9602,
                              "skus.specs.value.text": {
                                "doc_count_error_upper_bound": 58,
                                "sum_other_doc_count": 3600,
                                "buckets": [
                                  {
                                    "key": "Haute",
                                    "doc_count": 1642
                                  },
                                  {
                                    "key": "Moyenne",
                                    "doc_count": 1437
                                  },
                                  {
                                    "key": "Tube",
                                    "doc_count": 571
                                  },
                                  {
                                    "key": "Flacon",
                                    "doc_count": 466
                                  },
                                  {
                                    "key": "Pot",
                                    "doc_count": 406
                                  },
                                  {
                                    "key": "Flacon pompe",
                                    "doc_count": 397
                                  },
                                  {
                                    "key": "Faible",
                                    "doc_count": 331
                                  },
                                  {
                                    "key": "Floral",
                                    "doc_count": 322
                                  },
                                  {
                                    "key": "Floral fruité",
                                    "doc_count": 244
                                  },
                                  {
                                    "key": "Frais (hespéridé)",
                                    "doc_count": 186
                                  }
                                ]
                              }
                            }
                          },
                          {
                            "key": "Format",
                            "doc_count": 7988,
                            "skus.specs.value": {
                              "doc_count": 7985,
                              "skus.specs.value.text": {
                                "doc_count_error_upper_bound": 36,
                                "sum_other_doc_count": 2363,
                                "buckets": [
                                  {
                                    "key": "Moyen",
                                    "doc_count": 1291
                                  },
                                  {
                                    "key": "Clair",
                                    "doc_count": 1095
                                  },
                                  {
                                    "key": "Crème",
                                    "doc_count": 782
                                  },
                                  {
                                    "key": "Foncé",
                                    "doc_count": 686
                                  },
                                  {
                                    "key": "Fluide",
                                    "doc_count": 394
                                  },
                                  {
                                    "key": "50",
                                    "doc_count": 342
                                  },
                                  {
                                    "key": "100",
                                    "doc_count": 314
                                  },
                                  {
                                    "key": "Liquide",
                                    "doc_count": 271
                                  },
                                  {
                                    "key": "Ethnique",
                                    "doc_count": 229
                                  },
                                  {
                                    "key": "Gel",
                                    "doc_count": 218
                                  }
                                ]
                              }
                            }
                          },
                          {
                            "key": "Texture",
                            "doc_count": 6119,
                            "skus.specs.value": {
                              "doc_count": 6096,
                              "skus.specs.value.text": {
                                "doc_count_error_upper_bound": 55,
                                "sum_other_doc_count": 2727,
                                "buckets": [
                                  {
                                    "key": "Eclat",
                                    "doc_count": 1026
                                  },
                                  {
                                    "key": "Hydrate",
                                    "doc_count": 654
                                  },
                                  {
                                    "key": "Uniformise",
                                    "doc_count": 364
                                  },
                                  {
                                    "key": "Protège",
                                    "doc_count": 295
                                  },
                                  {
                                    "key": "30",
                                    "doc_count": 211
                                  },
                                  {
                                    "key": "Anti rides",
                                    "doc_count": 208
                                  },
                                  {
                                    "key": "1",
                                    "doc_count": 178
                                  },
                                  {
                                    "key": "Repulpe",
                                    "doc_count": 170
                                  },
                                  {
                                    "key": "100",
                                    "doc_count": 132
                                  },
                                  {
                                    "key": "Matifie",
                                    "doc_count": 131
                                  }
                                ]
                              }
                            }
                          },
                          {
                            "key": "Action",
                            "doc_count": 4779,
                            "skus.specs.value": {
                              "doc_count": 4779,
                              "skus.specs.value.text": {
                                "doc_count_error_upper_bound": 27,
                                "sum_other_doc_count": 1847,
                                "buckets": [
                                  {
                                    "key": "30",
                                    "doc_count": 595
                                  },
                                  {
                                    "key": "50",
                                    "doc_count": 553
                                  },
                                  {
                                    "key": "10",
                                    "doc_count": 322
                                  },
                                  {
                                    "key": "4",
                                    "doc_count": 288
                                  },
                                  {
                                    "key": "15",
                                    "doc_count": 255
                                  },
                                  {
                                    "key": "200",
                                    "doc_count": 227
                                  },
                                  {
                                    "key": "3",
                                    "doc_count": 214
                                  },
                                  {
                                    "key": "6",
                                    "doc_count": 173
                                  },
                                  {
                                    "key": "150",
                                    "doc_count": 160
                                  },
                                  {
                                    "key": "100",
                                    "doc_count": 145
                                  }
                                ]
                              }
                            }
                          },
                          {
                            "key": "Effet",
                            "doc_count": 4047,
                            "skus.specs.value": {
                              "doc_count": 4047,
                              "skus.specs.value.text": {
                                "doc_count_error_upper_bound": 2,
                                "sum_other_doc_count": 380,
                                "buckets": [
                                  {
                                    "key": "Flacon",
                                    "doc_count": 927
                                  },
                                  {
                                    "key": "Tube",
                                    "doc_count": 725
                                  },
                                  {
                                    "key": "Stick",
                                    "doc_count": 608
                                  },
                                  {
                                    "key": "Solide",
                                    "doc_count": 360
                                  },
                                  {
                                    "key": "Flacon pompe",
                                    "doc_count": 278
                                  },
                                  {
                                    "key": "Crayon",
                                    "doc_count": 245
                                  },
                                  {
                                    "key": "Pot",
                                    "doc_count": 242
                                  },
                                  {
                                    "key": "Stylo",
                                    "doc_count": 125
                                  },
                                  {
                                    "key": "Pinceau",
                                    "doc_count": 99
                                  },
                                  {
                                    "key": "Moyen",
                                    "doc_count": 58
                                  }
                                ]
                              }
                            }
                          },
                          {
                            "key": "Couvrance",
                            "doc_count": 3546,
                            "skus.specs.value": {
                              "doc_count": 3546,
                              "skus.specs.value.text": {
                                "doc_count_error_upper_bound": 3,
                                "sum_other_doc_count": 495,
                                "buckets": [
                                  {
                                    "key": "Brillant",
                                    "doc_count": 703
                                  },
                                  {
                                    "key": "Naturel",
                                    "doc_count": 616
                                  },
                                  {
                                    "key": "Lumineux",
                                    "doc_count": 528
                                  },
                                  {
                                    "key": "Mat",
                                    "doc_count": 525
                                  },
                                  {
                                    "key": "Satiné",
                                    "doc_count": 212
                                  },
                                  {
                                    "key": "Tout en un",
                                    "doc_count": 148
                                  },
                                  {
                                    "key": "Irisé",
                                    "doc_count": 142
                                  },
                                  {
                                    "key": "Pailleté",
                                    "doc_count": 77
                                  },
                                  {
                                    "key": "Crayon",
                                    "doc_count": 50
                                  },
                                  {
                                    "key": "Volume",
                                    "doc_count": 50
                                  }
                                ]
                              }
                            }
                          }
                        ]
                      }
                    }
                  }
                }
              }
            }

1 个答案:

答案 0 :(得分:0)

使用反向嵌套解决。