【问题标题】:Elasticsearch Aggregations: filtering a global aggregation with nested queriesElasticsearch Aggregations:使用嵌套查询过滤全局聚合
【发布时间】:2019-01-25 22:11:34
【问题描述】:

我有这样的“嵌套”映射:

"stringAttributes":{
   "type":"nested",
   "properties":{
      "Name":{
         "type":"keyword"
      },
      "Value":{
         "type":"keyword"
      }
   }
},

因此有如下文档:

stringAttributes:[
   {
      Name:"supplier",
      Value:"boohoo"
   },
   {
      Name:"brand",
      Value:"gucci"
   },
   {
      Name:"primaryColour",
      Value:"black"
   },
   {
      Name:"secondaryColour",
      Value:"green"
   },
   {
      Name:"size",
      Value:"12"
   }
]

在构建多面搜索时,我相信我需要一个全局聚合。 IE。当一个供应商被用户过滤时,结果集将不包含来自其他供应商的文档,因此常规聚合将不包含任何其他供应商。

查询可以包括以下子句:

"must": [
  {
    "nested": {
      "path": "stringAttributes",
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "stringAttributes.Name": "supplier"
              }
            },
            {
              "terms": {
                "stringAttributes.Value": [
                  "boohoo"
                ]
              }
            }
          ]
        }
      }
    }
  },
  {
    "nested": {
      "path": "stringAttributes",
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "stringAttributes.Name": "brand"
              }
            },
            {
              "terms": {
                "stringAttributes.Value": [
                  "warehouse"
                ]
              }
            }
          ]
        }
      }
    }
  }
]

因此,在这种情况下,我需要一个全局聚合,然后由应用的所有其他过滤器(例如按品牌)过滤,这将返回在给定这些其他过滤器的情况下可以选择的其他供应商。

这是我目前所拥有的。但是,它返回“全局”未过滤的结果。在这一点上,我完全被难住了。

{
   "global":{},
   "aggs":{
      "inner":{
         "filter":{
            "nested":{
               "query":{
                  "bool":{
                     "filter":[
                        {
                           "term":{
                              "stringAttributes.Name":{
                                 "value":"brand"
                              }
                           }
                        },
                        {
                           "terms":{
                              "stringAttributes.Value":[
                                 "warehouse"
                              ]
                           }
                        }
                     ]
                  }
               },
               "path":"stringAttributes"
            }
         }
      },
      "aggs":{
         "nested":{
            "path":"stringAttributes"
         },
         "aggs":{
            "aggs":{
               "filter":{
                  "match":{
                     "stringAttributes.Name":"supplier"
                  }
               },
               "aggs":{
                  "facet_value":{
                     "terms":{
                        "size":1000,
                        "field":"stringAttributes.Value"
                     }
                  }
               }
            }
         }
      }
   }
}

对于过滤具有嵌套属性的全局聚合有什么建议吗?我已经阅读了很多关于 SO 的各种其他答案的文档,但仍然难以理解为什么这个特定的 agg 没有被过滤。

【问题讨论】:

    标签: elasticsearch elasticsearch-aggregation


    【解决方案1】:

    经过更多挖掘后我的建议答案...

    {
       "global":{
    
       },
       "aggs":{
          "inner":{
             "filter":{
                "nested":{
                   "query":{
                      "bool":{
                         "filter":[
                            {
                               "term":{
                                  "stringAttributes.Name":{
                                     "value":"brand"
                                  }
                               }
                            },
                            {
                               "terms":{
                                  "stringAttributes.Value":[
                                     "warehouse"
                                  ]
                               }
                            }
                         ]
                      }
                   },
                   "path":"stringAttributes"
                }
             },
             "aggs":{
                "nested":{
                   "path":"stringAttributes"
                },
                "aggs":{
                   "agg_filtered_special":{
                      "filter":{
                         "match":{
                            "stringAttributes.Name":"supplier"
                         }
                      },
                      "aggs":{
                         "facet_value":{
                            "terms":{
                               "size":1000,
                               "field":"stringAttributes.Value"
                            }
                         }
                      }
                   }
                }
             }
          }
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-30
      • 2014-12-31
      • 2020-09-05
      • 1970-01-01
      • 2015-11-14
      • 2017-04-15
      • 2021-04-19
      • 2014-11-14
      相关资源
      最近更新 更多