0

I'm an absolute newbie in Elasticsearch and trying to get my head around aggregations. I need to run an aggregation parallel to the existing one. This is the sample document page structure.

{
  "brand": {
    "list": [
      "Apple"
    ],
    "name": "Apple"
  },
  "title": {
    "list": [
      "Apple Macbook Pro 15 - Intel I7 3,10ghz (2tb|r560|touchid|space) 2017"
    ]
  },
  "category": {
    "l1": {
      "name": "Computers/Tablets & Networking",
      "id": 58058
    },
    "l2": {
      "name": "Laptops & Netbooks",
      "id": 175672
    },
    "l3": {
      "name": "Apple Laptops",
      "id": 111422
    },
    "l4": {},
    "l5": {},
    "l6": {}
  }
}

and this is the existing query I'm running to get the aggregation of top 5 category names for a given title, which is working perfectly.

{
    "size": 0,
    "aggs":
    {
        "categoryId":
        {
            "filter":
            {
                "bool":
                {
                    "filter": [
                    {
                        "term":
                        {
                            "title.list": "phone"
                        }
                    }]
                }
            },
            "aggs":
            {
                "results":
                {
                    "terms":
                    {
                        "field": "category.l2.id",
                        "size": 5
                    },
                    "aggs":
                    {
                        "categoryName":
                        {
                            "terms":
                            {
                                "field": "category.l2.name.keyword",
                                "size": 1
                            }
                        }
                    }
                }
            }
        }
    }
}

Next, I need to find top 5 brand names associated with title keyword search, sorted in descending order if possible. So i try this, but it is not working, also throwing an error.

{
    "size": 0,
    "aggs":
    {
        "categoryId":
        {
            "filter":
            {
                "bool":
                {
                    "filter": [
                    {
                        "term":
                        {
                            "title.list": "phone"
                        }
                    }]
                }
            },
            "aggs":
            {
                "results":
                {
                    "terms":
                    {
                        "field": "category.l2.id",
                        "size": 5
                    },
                    "aggs":
                    {
                        "categoryName":
                        {
                            "terms":
                            {
                                "field": "category.l2.name.keyword",
                                "size": 1
                            }
                        }
                    }
                },
                "brands_names":
                {
                    "terms":
                    {
                        "field": "brand.name",
                        "size": 10
                    },
                    "aggs":
                    {
                        "brand_top_hits":
                        {
                            "top_hits":
                            {
                                "size": 1
                            }
                        }
                    }
                }
            }
        }
    }
}

I know I'm missing something obvious. Should I be using a value-count or top hits query for finding the top 5 brands? And How do I combine it with existing category query?

anoop chandran
  • 1,460
  • 5
  • 23
  • 42

1 Answers1

0

I got it working by using this query

{
  "size": 0,
  "aggs": {
    "categoryId": {
      "filter": {
        "bool": {
          "filter": [
            {
              "term": {
                "title.list": "bag"
              }
            }
          ]
        }
      },
      "aggs": {
        "results": {
          "terms": {
            "field": "category.l2.id",
            "size": 5
          },
          "aggs": {
            "categoryName": {
              "terms": {
                "field": "category.l2.name.keyword",
                "size": 1
              }
            },
            "brands_names": {
              "terms": {
                "field": "brand.name.keyword",
                "size": 10
              }
            }
          }
        }
      }
    }
  }
}
anoop chandran
  • 1,460
  • 5
  • 23
  • 42