sree
sree

Reputation: 902

filter aggregation not working elastic search

I am trying to use filter aggregation to support multi-select facet values from the frontend.

I have color and depth filters.

color filter holds values: black(5),blue(3),red(2)

depth filter holds values: 70mm(3),60mm(5),50mm(3)

When I select the Black in the color filter all other options(blue, red) not coming in response.

I have tried by adding

"colourAgg": {
        "filter": {
            "match_all": {
                "boost": 1.0
            }
        },
        "aggregations": {
            "colourAgg": {
                "terms": {
                    "field": "colour.keyword",
                    "size": 10,
                    "min_doc_count": 1,
                    "shard_min_doc_count": 0,
                    "show_term_doc_count_error": false,
                    "order": [
                        {
                            "_count": "desc"
                        },
                        {
                            "_key": "asc"
                        }
                    ]
                }
            }
        }
    }

Mapping:

{
"products": {
    "mappings": {
        "properties": {
            "assemblyrequired": {
                "type": "boolean"
            },
            "australianmade": {
                "type": "boolean"
            },
            "australiasellable": {
                "type": "boolean"
            },
            "avgRating": {
                "type": "float"
            },
            "category": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "categorylevel1": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "categorylevel2": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "categorylevel3": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "categoryname": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "colour": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "commercialuse": {
                "type": "boolean"
            },
            "customisable": {
                "type": "boolean"
            },
            "depth": {
                "type": "float"
            },
            "freedelivery": {
                "type": "boolean"
            },
            "height": {
                "type": "float"
            },
            "listprice": {
                "type": "float"
            },
            "materialcode": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "moneybackguarantee": {
                "type": "boolean"
            },
            "newrelease": {
                "type": "boolean"
            },
            "numberOfRating": {
                "type": "long"
            },
            "online": {
                "type": "boolean"
            },
            "outdooruse": {
                "type": "boolean"
            },
            "predictivecategorydata": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "pricematchguarantee": {
                "type": "boolean"
            },
            "productid": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "productimageurl": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "productname": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "producttypecode": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "promotedprice": {
                "type": "float"
            },
            "sale": {
                "type": "integer"
            },
            "saleprice": {
                "type": "float"
            },
            "sellable": {
                "type": "boolean"
            },
            "sellercode": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "shortdescription": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "sku": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "state": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "stylecode": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "warrantycode": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "weight": {
                "type": "float"
            },
            "width": {
                "type": "float"
            }
        }
    }
}

}

Document:

{
                "sellercode": "F2OVRX",
                "productid": 13410,
                "saleprice": 4000.2,
                "producttypecode": "Recliner Lounge or Sofa",
                "productimageurl": null,
                "sellable": false,
                "freedelivery": false,
                "promotedprice": null,
                "listprice": 4000.2,
                "productname": "louise chair - cream",
                "outdooruse": null,
                "avgRating": 0.0,
                "warrantycode": null,
                "state": [
                    "SA",
                    "NT"
                ],
                "newrelease": null,
                "sku": "VSTALDBED415_8",
                "height": 66.0,
                "shortdescription": "Stackable light weight chair made from Virgin materials.High strength one piece gas injection moulded technology.160kg weight tested, Catas test certificated",
                "commercialuse": true,
                "customisable": true,
                "weight": null,
                "pricematchguarantee": true,
                "colour": "Black",
                "australianmade": true,
                "depth": 65.0,
                "moneybackguarantee": false,
                "width": 31.0,
                "predictivecategorydata": [
                    "Dressing Table-Dining Table-52"
                ],
                "online": true,
                "materialcode": "Timber",
                "numberOfRating": 0,
                "categoryname": [
                    "Bed - Double",
                    "Dining Table",
                    "Dressing Table",
                    "444",
                    "Bench",
                    null,
                    "M13",
                    "Bed Head"
                ],
                "location": [],
                "assemblyrequired": null,
                "category": [
                    "outdoor",
                    "bed",
                    "doublebeds",
                    "444",
                    "beds",
                    "cat1220",
                    "m13",
                    "cafebarrestaurant"
                ],
                "stylecode": "Safari"
            }

but still I am getting black only.Any inputs?

Thanks, Sree.

Upvotes: 1

Views: 287

Answers (1)

jaspreet chahal
jaspreet chahal

Reputation: 9109

Aggregation processes documents returned by "query" part. So if you are filtering on "black" in query, all documents with color black will be returned and aggregation will get applied on these docs .

You need your aggregation to apply on all documents in index, not just those returned by query. You can use Global aggregation for this

Defines a single bucket of all the documents within the search execution context. This context is defined by the indices and the document types you’re searching on, but is not influenced by the search query itself.

{
  "aggs": {
    "products": {
      "global": {}, --> note
      "aggs": {
        "filtered_docs": {   --> filter certain docs for entire index 
          "filter": {
            "range": {
              "depth": {
                "gte": 60
              }
            }
          },
          "aggs": {
            "colourAgg": {   --> facets for color
              "terms": {
                "field": "colour.keyword",
                "size": 10,
                "min_doc_count": 1,
                "shard_min_doc_count": 0,
                "show_term_doc_count_error": false,
                "order": [
                  {
                    "_count": "desc"
                  },
                  {
                    "_key": "asc"
                  }
                ]
              }
            }
          }
        }
      }
    }
  }
}

Upvotes: 1

Related Questions