Aggregated top hits with Date Range filter

I am trying to add top hits to an existing query with date range applied, but when I do that, the top hits returned include dates outside of the date range set in the query. What am I doing wrong here? Do I need to also add it inside the aggregations? If so, where do I put it.

Here is my request (in my application, this is created dynamically depending on the search params, here I am using a sample query so it won’t have a bunch of variables in the code).

{
    "size": 1,
    "sort": [
        {
            "timestamp": {
                "order": "desc",
                "unmapped_type": "boolean"
            }
        }
    ],
    "query": {
        "bool": {
            "must": [
                {
                    "query_string": {
                        "query": "gameMode.name.keyword:Ladder AND team.deck.name.keyword:giant,guards,hunter,mega-minion,miner,poison,the-log,zap"
                    }
                },
                {
                    "range": {
                        "timestamp": {
                            "gte": "now-2d/1h",
                            "lte": "now/1h",
                            "format": "epoch_millis"
                        }
                    }
                }
            ],
            "must_not": []
        }
    },
    "_source": {
        "includes": [
            "team.deck",
            "gameMode",
            "team.cards"
        ]
    },
    "aggs": {
        "winner": {
            "terms": {
                "field": "winner",
                "size": 10,
                "order": {
                    "_term": "asc"
                }
            }
        },
        "winner_over_time": {
            "terms": {
                "field": "winner",
                "size": 10,
                "order": {
                    "_term": "asc"
                }
            },
            "aggs": {
                "count_over_time": {
                    "date_histogram": {
                        "field": "timestamp",
                        "interval": "4h",
                        "min_doc_count": 0,
                        "missing": 0
                    }
                }
            }
        },
        "players": {
            "terms": {
                "field": "team.tag.keyword",
                "size": 20,
                "order": {
                    "_count": "desc"
                }
            },
            "aggs": {
                "battles": {
                    "top_hits": {
                        "sort": [
                            {
                                "team.endingTrophies": {
                                    "order": "desc"
                                }
                            }
                        ],
                        "_source": {
                            "excludes": [
                                "team.cards"
                            ]
                        },
                        "size": 100
                    }
                },
                "winner": {
                    "terms": {
                        "field": "winner",
                        "size": 10,
                        "order": {
                            "_term": "desc"
                        }
                    }
                }
            }
        }
    }
}

The implemented result of the response is implemented here: http://cr-api.com/decks/stats?name=bats,goblin-gang,inferno-dragon,mega-knight,miner,skeleton-barrel,spear-goblins,zap&time=1d

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