簡體   English   中英

Elasticsearch Aggregations:使用嵌套查詢過濾全局聚合

[英]Elasticsearch Aggregations: filtering a global aggregation with nested queries

我有這樣的“嵌套”映射:

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

在構建多面搜索時,我相信我需要進行全局匯總。 即,當一個供應商被用戶過濾時,結果集將不包含來自其他供應商的文檔,因此常規聚合將不包含任何其他供應商。

該查詢可以包含以下子句:

"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。

經過更多挖掘后我的建議答案...

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM