繁体   English   中英

ElasticSearch-嵌套过滤聚合

[英]ElasticSearch - Nested Filtered Aggregations

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

"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个。我已经研究了“ 过滤器聚合”和“ 嵌套聚合”,但无法给出答案。

我正在使用ElasticSearch 5.5版。

我试图先查询正确工作,然后再转换为Nest .net。

任何帮助将非常感激。

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

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

但是,由于您说的一个价格只能是GBP,难道每个SKU的每种货币只能有一个价格吗? 如果是这种情况,我建议不要在此处使用嵌套数据类型作为价格。 相反,您可以使用类似以下的映射:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM