代码之家  ›  专栏  ›  技术社区  ›  BenG

ElasticSearch-嵌套过滤聚合

  •  0
  • BenG  · 技术社区  · 7 年前

    currencyCode

    "hits": [
          {
            "_index": "product",
            "_type": "main",
            "_id": "1",
            "_score": 1,
            "_source": {         
              "skus": [
                {
                  "prices": [
                    {
                      "currencyCode": "GBP",
                      "price": 15
                    }
                  ]
                }
              ]
            }
          },{
            "_index": "product",
            "_type": "main",
            "_id": "2",
            "_score": 1,
            "_source": {         
              "skus": [
                {
                  "prices": [
                    {
                      "currencyCode": "GBP",
                      "price": 20
                    }
                  ]
                }
              ]
            }
          },
        {
            "_index": "product",
            "_type": "main",
            "_id": "3",
            "_score": 1,
            "_source": {         
              "skus": [
                {
                  "prices": [
                    {
                      "currencyCode": "GBP",
                      "price": 25
                    }
                  ]
                }
              ]
            }
          }]
      }
    

    所以我想要最小15,最大25。我已经调查过了 Filter Aggregation Nested Aggregation

    我正在使用ElasticSearch 5.5版。

    任何帮助都将不胜感激。

    1 回复  |  直到 7 年前
        1
  •  1
  •   dshockley    7 年前

    您可以嵌套“嵌套”和“过滤”聚合,如下所示:

    {
      "size": 0,
      "aggs": {
        "skus": {
          "nested": {
            "path": "skus"
          },
          "aggs": {
            "prices": {
              "nested": {
                "path": "skus.prices"
              },
              "aggs": {
                "gbp_filter": {
                  "filter": {
                    "term": {
                      "skus.prices.currencyCode": "GBP"
                    }
                  },
                  "aggs": {
                    "min_price": {
                      "min": {
                        "field": "skus.prices.price"
                      }
                    },
                    "max_price": {
                      "max": {
                        "field": "skus.prices.price"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    

    然而,既然你说只有一个价格可以是英镑,那么对于每个SKU,每种货币只能有一个价格,这也是真的吗?如果是这样的话,我建议不要在这里使用嵌套数据类型。相反,您可以使用这样的映射:

    {
      "product": {
        "properties": {
          "skus": {
            "type": "nested",
            "properties": {
              "prices": {
                "properties": {
                  "GBP": {"properties": {"price": {"type": "integer"}}},
                  "USD": {"properties": {"price": {"type": "integer"}}},
                  ...remaining currencies...
                }
              }
            }
          }
        }
      }
    }