【问题标题】:ElasticSearch - Nested Filtered AggregationsElasticSearch - 嵌套过滤聚合
【发布时间】:2018-01-30 23:29:51
【问题描述】:

我正在寻找一种从已过滤的 2 级嵌套对象中获取最小值和最大值的方法。所以下面我试图获得具有currencyCode GBP 的最低和最高价格。每个产品可以有多个 sku,每个 sku 可以有多个价格(尽管只有 1 个是英镑):

"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 AggregationNested Aggregation,但找不到答案。

我使用的是 ElasticSearch 5.5 版。

在转换为 Nest .net 之前,我正在尝试先让查询正常工作。

任何帮助将不胜感激。

【问题讨论】:

    标签: elasticsearch elasticsearch-5 elasticsearch-net


    【解决方案1】:

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

    {
      "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...
                }
              }
            }
          }
        }
      }
    }
    

    映射不是很简洁,但是查询起来会更高效,查询也更好。 (几乎)任何时候您都可以对数据进行非规范化以摆脱嵌套,即使您必须复制信息(以满足不同类型查询的需求),这也是一个好主意。

    【讨论】:

    • 谢谢。我也能够移除第一个巢,所以从价格巢开始。您的架构也看起来不错,但我需要动态添加价格。非常感谢!
    猜你喜欢
    • 2014-12-31
    • 2015-11-14
    • 2017-04-15
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 2015-11-18
    • 1970-01-01
    相关资源
    最近更新 更多