Akash Salunkhe
Akash Salunkhe

Reputation: 314

Elastic-search aggregate top 3 common result

My indexed data is of below structure, i want to aggregate top 3 most repeted productProperty, so top 3 most repeated productProperty will be there in aggregation result

  [
    {
        productProperty: "material",
        productValue:[{value: wood},{value: plastic}] ,
    },
    {
        productProperty: "material",
        productValuea:[{value: wood},{value: plastic}] ,
    },
    {
        productProperty: "type",
        productValue:[{value: 26A},{value: 23A}] ,
    },
    {
        productProperty: "type",
        productValue:[{value: 22B},{value: 90C}] ,
    },
    {
        productProperty: "material",
        productValue:[{value: wood},{value: plastic}] ,
    },
    {
        productProperty: "age_rating",
        productValue:[{value: 18},{value: 13}] ,
    }
    ]

Below query aggregates all based on productProperty but how can i get top 3 results out of that

{
  "query": {},
  "aggs": {
    "filtered_product_property": {
      "filter": {
        "bool": {
          "must": []
        }
      },
      "aggs": {
        "aggs": {
          "productProperty": {
            "terms": {
              "field": "productProperty"
            }
          }
        }
      }
    }
  }
}

Upvotes: 0

Views: 37

Answers (2)

Bhavya
Bhavya

Reputation: 16182

As mentioned by @Tushar you can use the size param. According to the ES official documentation

when there are lots of unique terms, Elasticsearch only returns the top terms; this number is the sum of the document counts for all buckets that are not part of the response

However, you can define the order in which the sorting of the results should be done of the aggregation response, using the order param. By default, the result is sorted on the basis of doc count in descending order

Search Query will be

{
  "aggs": {
    "productProperty": {
      "terms": {
        "field": "productProperty.keyword",
        "size": 3
      }
    }
  }
}

And, search result would be

"aggregations": {
    "productProperty": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "material",
          "doc_count": 3
        },
        {
          "key": "type",
          "doc_count": 2
        },
        {
          "key": "age_rating",
          "doc_count": 1
        }
      ]
    }
  }

Upvotes: 1

Tushar Shahi
Tushar Shahi

Reputation: 20486

You can use the size parameter in your term aggregation.

{
  "query": {},
  "aggs": {
    "filtered_product_property": {
      "filter": {
        "bool": {
          "must": []
        }
      },
      "aggs": {
        "aggs": {
          "productProperty": {
            "terms": {
              "field": "productProperty", 
             "size" : 3
            }
          }
        }
      }
    }
  }
}

Important to point out, that terms aggregations are not the most accurate in some cases.

Upvotes: 1

Related Questions