Skip to content

Conversation

javanna
Copy link
Member

@javanna javanna commented Sep 26, 2025

This is similar to #135524, but adding the time range bucketing attribute to the took time latency metric.

That requires a bit of ceremony as the took time metric is recorded on the coordinating node, while the time range filter is parsed on each shard. We don't have mappings available on the coord node, which are needed to parse dates. Thus we need to rely on date parsing done on the data nodes, which requires sending back the parsed value to the coord node, performing some simple reduction on it, and adding it back to the search response.

This also includes two improvements to the existing mechanism:

  • take date precision into account
  • track the time range as part of query rewrite, additionally to query execution. This way queries that are rewritten to a match_all still see their original range reported

This is similar to elastic#135524, but adding the attribute to the took time latency metric.
That requires a bit of ceremony as the took time metric is recorded on the
coordinating node, while the time range filter is parsed on each shard. We
don't have mappings available on the coord node, which are needed to parse
dates on the coord node. Thus we need to rely on date parsing done on the
data nodes, which requires sending back the parsed value to the coord node,
performing some simple reduction on it, and adding it back to the search
response.
@javanna javanna added >enhancement :Search Foundations/Search Catch all for Search Foundations v9.2.0 labels Sep 26, 2025
@elasticsearchmachine elasticsearchmachine added the Team:Search Foundations Meta label for the Search Foundations team in Elasticsearch label Sep 26, 2025
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-search-foundations (Team:Search Foundations)

@elasticsearchmachine
Copy link
Collaborator

Hi @javanna, I've created a changelog YAML for you.

)
) {
QueryPhase.addCollectorsAndSearch(rankSearchContext);
QueryPhase.addCollectorsAndSearch(rankSearchContext, null);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will look into this as a follow-up, need to write a test that leverages this and see if it makes sense to track timestamp from for it too, it probably does.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking deeper at this, I think we are fine not tracking the time range from in this case. It's only used when multiple ranking steps are required. I don't think that we can reliably track the time range from in that case, as each sub-query may have its own independent filters. It's not that helpful either because using time range filters combined with ranking is a bit of an edge case that we don't want to focus on at the moment.

assertEquals("hits_only", attributes.get("query_type"));
assertEquals("_score", attributes.get("sort"));
assertEquals("pit", attributes.get("pit_scroll"));
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a follow-up, I have more work to do around retrievers, as they optionally hold a query too. Need to walk the retrievers tree like I do for queries, and add more tests.

} else {
this.rangeTimestampFrom = Math.min(rangeTimestampFrom, this.rangeTimestampFrom);
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realized that we need this in QueryRewriteContext, that this extends, to do the same tracking when we parse the time range filter as part of query rewrite. There's cases when the range gets rewritten to a range with no bounds (match all), for which we still want to bucket the request based on its original time range.

Copy link
Contributor

@benchaplin benchaplin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change looks good.

Copy link
Contributor

@andreidan andreidan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks Luca

Comment on lines +1446 to +1447
// range queries may get rewritten to match_all or a range with open bounds. Rewriting in that case is the only place
// where we parse the date and set it to the context. We need to propagate it back from the clone into the original context
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️ this is very useful and a true comment, documenting the why. Thank you!

@javanna javanna added the auto-backport Automatically create backport pull requests when merged label Oct 6, 2025
@javanna javanna merged commit 0cc854d into elastic:main Oct 7, 2025
34 checks passed
@javanna javanna deleted the enhancement/took_time_time_range_bucketing branch October 7, 2025 06:38
@javanna javanna removed the auto-backport Automatically create backport pull requests when merged label Oct 7, 2025
@elasticsearchmachine
Copy link
Collaborator

💔 Backport failed

The backport operation could not be completed due to the following error:

There are no branches to backport to. Aborting.

You can use sqren/backport to manually backport by running backport --upstream elastic/elasticsearch --pr 135549

javanna added a commit that referenced this pull request Oct 7, 2025
There's situations where the time range filter is provided as part of the retriever tree.
In that case, we capture the time range filter from while parsing it, but we don't do
the corresponding introspection of the retriever tree to extract which fields were
the time range filters made against.

This commit introduces a very basic introspection of retrievers and tests around it, expanding on #135549.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport pending >enhancement :Search Foundations/Search Catch all for Search Foundations Team:Search Foundations Meta label for the Search Foundations team in Elasticsearch v9.3.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants