简体   繁体   中英

Elasticsearch Aggregations: filtering a global aggregation with nested queries

I have 'nested' mapping like so:

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

and thus have docs that such as:

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

In building faceted search I believe I need a global aggregation. Ie when a supplier is filtered by a user, the result set will not contains docs from other suppliers, so the regular aggregation will not contain any of the other supplier.

The query could include the following clauses:

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

So in this case I need a global aggregation that is then filtered by all OTHER filters applied (eg by brand) that will return the other suppliers that could be selected given these other filters.

This is what I have so far. It returns the 'global' unfiltered results however. At this point I am completely stumped.

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

Any suggestions for filtering a global aggregation with nested attributes? I have read through a lot of documentation of various other answers on SO but still struggling to understand why this particular agg is not being filtered.

My suggested answer after some more digging...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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