elasticsearch具有子聚合的多选方面功能

yvt65v4c  于 2021-06-15  发布在  ElasticSearch
关注(0)|答案(1)|浏览(297)

给出以下数据:

curl -XPUT 'http://localhost:9200/products/'

curl -XPOST 'http://localhost:9200/products/product/_mapping' -d '{
    "product": {
        "_parent": {"type": "product_group"}
    }
}'

curl -XPUT 'http://localhost:9200/products/product_group/1' -d '{
  "title": "Product 1"
}'

curl -XPOST localhost:9200/products/product/1?parent=1 -d '{
    "height": 190, 
    "width": 120 
}'

curl -XPOST localhost:9200/products/product/2?parent=1 -d '{
    "height": 120, 
    "width": 100
}'

curl -XPOST localhost:9200/products/product/3?parent=1 -d '{
    "height": 110, 
    "width": 120
}'

产品上的子聚合导致以下方面:
高度
110 (1)
120 (1)
190 (1)
宽度
120 (2)
100 (1)
如果我现在过滤高度190,我希望从过滤器中排除高度聚合,因此结果将是:
高度
110 (1)
120 (1)
190 (1)
宽度
120 (1)
这可以通过过滤器聚合来解决,但我不确定它是否有效,或者在使用父子关系时语法如何。
看到了吗http://distinctplace.com/2014/07/29/build-zappos-like-products-facets-with-elasticsearch/
到目前为止我试过的:

curl -XGET 'http://localhost:9200/products/product_group/_search?pretty=true' -d '{
    "filter": {
        "has_child": {
            "type": "product",
            "filter": {
                "term": {"height": 190}
            },
            "inner_hits": {} 
        }
    },
    "aggs": {
        "to-products": {
            "children": {"type": "product"},
            "aggs": {
                "height": {
                    "filter": {"match_all": {}},
                    "aggs": {
                        "height": {
                            "terms": {"field": "height", "size": 10}
                        }
                    }
                },
                "width": {
                    "filter": {
                        "and": [{"terms": { "height": [190]}}]
                    },
                    "aggs": {
                        "width": {
                            "terms": {"field": "width", "size": 10}
                        }
                    }
                }
            }  
        }
    }
}
'
watbbzwu

watbbzwu1#

我不完全理解您的问题,但如果您想在子聚合中有多个聚合,则必须在聚合中的每个字段之前附加父类型名称。
这里是修改后的查询,

curl -XPOST "http://localhost:9200/products/product_group/_search?pretty=true" -d'
    {
   "size": 0,
   "filter": {
      "has_child": {
         "type": "product",
         "filter": {
            "term": {
               "product.height": 190
            }
         },
         "inner_hits": {}
      }
   },
   "aggs": {
      "to-products": {
         "children": {
            "type": "product"
         },
         "aggs": {
            "height": {
               "filter": {
                  "match_all": {}
               },
               "aggs": {
                  "height": {
                     "terms": {
                        "field": "product.height",
                        "size": 10
                     }
                  }
               }
            },
            "width": {
               "filter": {
                  "and": [
                     {
                        "terms": {
                           "product.height": [
                              190
                           ]
                        }
                     }
                  ]
               },
               "aggs": {
                  "width": {
                     "terms": {
                        "field": "product.width",
                        "size": 10
                     }
                  }
               }
            }
         }
      }
   }
}'

文档中没有提到它,这让许多用户感到困惑,我猜他们对待子聚合和嵌套聚合是一样的,所以聚合的方式也是一样的。

相关问题