Query with Many Aggregations

Good Evening,

I have this situation:

  • products
    |--- product
    |------ title
    |------ category
    |------ subcategory
    |------ brand
    |------ attributes
    |--------- attribute
    |------------ id
    |------------ value
    |------------ type

I did a products's query by category and subcategory with brand aggregation and specific attributes aggregation:

{
  "size": 0
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "category": "Tecnologia e Eletrônicos"
          }
        },
        {
          "match": {
            "subcategory": "TV"
          }
        }
      ]
    }
  },
  "aggregations": {
    "brands": {
      "terms": {
        "field": "brand",
        "size": 1000
      }
    },
    "inches": {
      "terms": {
        "query": {
          "bool": {
            "must": [
              {
                "match": {
                  "attributes.id": "polegadas"
                }
              }
            ]
          }
        },
        "field": "attributes.value",
        "size": 1000
      }
    }
  }
}

Can someyone help me?

Hi Alberto,

What is your problem specifically? The aggregations are not working as you expected?

Guilherme

Hi, Guilherme.

In my query, the result is:

  • All brands (OK!);
  • All attributes's values (NOK).

I expected all possible inches (polegadas).

Can you help me?

According to your product mapping, the attributes property doesn't contain an id and value. They are nested to the attribute object, aren't they? If that's the case, "attributes.id" and "attributes.value" won't work. You should use "attributes.attribute.id".

Try this solution

If even this change doesn't work, maybe you'll need to define your attributes object with :type :nested, as you need to retrieve the attributes.attribute.value and attributes.attribute.id together. The flat structure won't enable it.

https://www.elastic.co/guide/en/elasticsearch/reference/5.0/nested.html#_using_literal_nested_literal_fields_for_arrays_of_objects

Sorry, the attribute object should be defined as :type 'nested', not the attributes.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.