Raja Rajan
Raja Rajan

Reputation: 91

Elasticsearch - Get top 'n' values & perform aggregation

I have an index from which I would like to search for a text & get top 100 results and perform aggregation to retrieve the uniqueIds from those 100 results & sort them by their max_value/sum_value. At present I am using the query below which performs aggregations on around 100K results. But I want this aggregation to be limited to my first 100 results

{
"size": 0,
"query": {
    "multi_match": {
        "query": "game of thrones",
        "fields": ["Textfield", "Textfield.shingles^3"]
                    }
          },
"aggs" : {
  "Bucketized_Results" : {
        "terms" : {
          "field" : "uniqueId",
            "order": {"max_score": "desc"}
        },
    "aggs": {
        "max_score": {
            "max": {
                "script": "_score"
                   }
        }
    }
    }
}
}

Upvotes: 0

Views: 231

Answers (1)

Raja Rajan
Raja Rajan

Reputation: 91

This works :

{
"size": 0,
"query": {
        "multi_match": {
            "query": "game of thrones",
            "fields": ["fullText", "fullText.shingles^3"]
                        }
              },
  "aggs": {
    "Bucketized_Results": {
      "sampler": {
        "shard_size": 100
      },
      "aggs": {
        "getting_uniqueId": {
          "terms": {
            "field": "uniqueId",
                "order": {"sum_score": "desc"}
          },
          "aggs": {
            "sum_score": {
              "sum": {
                "script": "_score"
              }
            }
          }
        }
      }
    }
  }
}

Upvotes: 0

Related Questions