Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elastic Search doesn't respect size when using aggregations

I am new to Elastic Search so please forgive me if the answer is obvious.

I have modified a query to use aggs to show 'distinct' results. However, after adding the aggs the size doesn't seem to work anymore - it always returns 10 results no matter what I set size to.

Would anyone know how I could use both aggs and size together?

My query is:

{
    "size": "15",
    "from": "0",
    "query": {
      "bool": {
        "filter": [
          {
            "term": {
              "category": "Cars"
            }
          },
          {
            "term": {
              "location": "Sydney"
            }
          },
          {
            "term": {
              "status": true
            }
          }
        ]
      }
    },
   "sort": [
      {
        "_score": "desc"
      },
      {
        "brand": "asc"
      }
    ],
    "aggs": {
        "brand": {
            "terms": {
                "field": "brand",
                "order": {
                    "price": "asc"
                }
            },
            "aggs": {
                "brand": {
                    "top_hits": {
                        "size": 1,
                        "sort": [
                            {
                                "price": {
                                    "order": "asc"
                                }
                            }
                        ]
                    }
                },
                "price": {
                    "min": {
                        "field": "price"
                    }
                }
            }
        }
    }
  }
like image 357
MeltingDog Avatar asked Dec 20 '25 18:12

MeltingDog


1 Answers

The size parameter you have mentioned before the query, is used to set the size for the query hits, and will not affect the aggregations bucket size. Use the size parameter inside the parent aggregation just like you have mentioned in the sub-aggregation as "size":1 The modified query to get top 10 aggs is :

{
    "size": "15",
    "from": "0",
    "query": {
      "bool": {
        "filter": [
          {
            "term": {
              "category": "Cars"
            }
          },
          {
            "term": {
              "location": "Sydney"
            }
          },
          {
            "term": {
              "status": true
            }
          }
        ]
      }
    },
   "sort": [
      {
        "_score": "desc"
      },
      {
        "brand": "asc"
      }
    ],
    "aggs": {
        "brand": {
            "terms": {
                "field": "brand",
                "size": 10, 
                "order": {
                    "price": "asc"
                }
            },
            "aggs": {
                "brand": {
                    "top_hits": {
                        "size": 1,
                        "sort": [
                            {
                                "price": {
                                    "order": "asc"
                                }
                            }
                        ]
                    }
                },
                "price": {
                    "min": {
                        "field": "price"
                    }
                }
            }
        }
    }
  }

Hope this helps.

like image 103
soumitra goswami Avatar answered Dec 24 '25 10:12

soumitra goswami



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!