# How does rescore query affect aggregation results?

**URL:** <https://discuss.elastic.co/t/how-does-rescore-query-affect-aggregation-results/328892>\
**Category:** Elasticsearch\
**Created:** [March 30, 2023, 7:00am UTC](https://discuss.elastic.co/t/how-does-rescore-query-affect-aggregation-results/328892 "2023-03-30T07:00:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abhishek3](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@Abhishek3](https://discuss.elastic.co/u/Abhishek3)\
**Post date:** [March 30, 2023, 7:00am UTC](https://discuss.elastic.co/t/how-does-rescore-query-affect-aggregation-results/328892/1 "2023-03-30T07:00:33Z")

</div>

I am running rescore\_query with ltr model with window\_size=5,  
I am seeing the recore\_query is being applied to top 5 docs in hits and top 5 docs of every aggregated bucket.  
Is the how rescore\_query designed to work?

following is the trimmed down query.

```auto

{
    "sort": [
        {
            "_score": "desc"
        }
    ],
    
    "size": 30,
    "aggs": {
        "section_wise_results": {
            "terms": {
                "field": "home_search_section_id",
                "size": 6,
                "order": {
                    "score_": "desc"
                }
            },
            "aggs": {
                "section_bucket": {
                    "top_hits": {
                        "size": 20
                    }
                },
                "score_": {
                    "max": {
                        "script": {
                            "inline": "_score"
                        }
                    }
                }
            }
        }
    },
    "query": {
        "function_score": {
            "query": {
                "bool": {
                    "must": {
                        "multi_match": {
                            "query": "pay",
                            "fields": [
                                "tags",
                                "display_text",
                                "display_subtext"
                            ],
                            "type": "best_fields",
                            "operator": "and",
                            "fuzziness": "AUTO:100,102",
                            "prefix_length": 2
                        }
                    }
                }
            },
            "script_score": {
                "script": {
                    "source": "(_score * params.relevance_weight * params.new_max / params.old_max) + (doc['ctr_score'].size()>0 ? doc['ctr_score'].value * params.ctr_weight : 0) + (doc['freshness_score'].size()>0 ? doc['freshness_score'].value * params.freshness_weight : 0)",
                    "params": {
                        "relevance_weight": 0.85,
                        "new_max": 100,
                        "old_max": 100,
                        "ctr_weight": 0.15,
                        "freshness_weight": 0.15
                    }
                }
            },
            "boost_mode": "replace"
        }
    },
    
   "rescore": {
   "window_size":5,
    "query": {
      "rescore_query": {
        "function_score": {
          "boost_mode": "replace",
          "query": {
            "sltr": {
              "params": {
                "keywords": "pay"
              },
              "model": "xgboost_model_v1"
            }
          },
          "script_score": {
            "script": {
              "source": "100.0 + _score"
            }
          }
        }
      },
      "query_weight": 0.001,
      "rescore_query_weight": 1
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 27, 2023, 7:00am UTC](https://discuss.elastic.co/t/how-does-rescore-query-affect-aggregation-results/328892/2 "2023-04-27T07:00:39Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
