0

I'm trying to build a product search with facet filtering for a eCommerce app. For the product brand I have the following structure:

"brand": {
    "type": "nested",
    "properties": {
        "name": {
            "type": "text"
        },
        "id": {
            "type": "integer"
        }
    }
}

I want to make an aggregation by brand id and return the whole object and the count of the documents. Something like this:

"brands" : {
    "doc_count_error_upper_bound" : 0,
    "sum_other_doc_count" : 0,
    "buckets" : [
    {
        "key" : {
            "name": "Apple",
            "id": 1
        },
        "doc_count" : 34
    },
    {
        "key" : {
            "name": "Samsung",
            "id": 2
        },
        "doc_count" : 23
    }
    ]
}

Currently I'm writing the aggregation like this:

"aggs": {
    "brands": {
        "nested": {
            "path": "brand"
        }, 
        "aggs": {
            "brandIds": {
                "terms": {
                "field": "brand.id"
                }
            }
        }
    },
}

and the result looks like this:

"aggregations" : {
    "brands" : {
        "doc_count" : 15,
        "brandIds" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
                {
                    "key" : 1,
                    "doc_count" : 4
                },
                {
                    "key" : 2,
                    "doc_count" : 2
                }
            ]
        }
    }
}

1 Answer 1

2

You can use a Term Aggregation within a Terms Aggregation like this :

GET {index_name}/_search
{
  "size": 0,
  "query": {
   "match_all": {}
  }, 
  "aggs": {
    "brands": {
      "nested": {
        "path": "brand"
      },
      "aggs": {
        "brandIds": {
          "terms": {
            "field": "brand.id"
          }, 
          "aggs": {
            "by name": {
              "terms": {
                "field": "brand.name.keyword",
                "size": 10
              }
            }
          }
        }
      }
    }
  }
}

This would result in something like this:

"aggregations": {
    "brands": {
      "doc_count": 68,
      "brandIds": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": 1,
            "doc_count": 46,
            "by name": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": [
                {
                  "key": "Apple",
                  "doc_count": 46
                }
              ]
            }
          },
          {
            "key": 2,
            "doc_count": 22,
            "ny id": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": [
                {
                  "key": "Samsung",
                  "doc_count": 22
                }
              ]
            }
          }
        ]
      }
    }
  }

Hope this helps!!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.