Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only aggregations require at least one shard request #115314

Merged

Conversation

piergm
Copy link
Member

@piergm piergm commented Oct 22, 2024

In the special case where we have no hits because the coordinator rewrote the query to match none we need to get at least one search response in order to produce a valid search result. This is true only if we have an aggregation in the query.
Only for aggs we need to contact the shard because we need the mappings for the queried index in order to produce a valid aggregation response, and mappings are only materialized in memory in data nodes where a certain index is allocated.

This pr updates this logic and sets requireAtLeastOneMatch only when searchRequest has aggregations.

The updated logic will set searchResponse.skippedShards == searchResponse.successfulShards == totalShards when we can skip all the shards because we have no aggs.

@piergm piergm added >enhancement Team:Search Foundations Meta label for the Search Foundations team in Elasticsearch :Search Foundations/Search Catch all for Search Foundations v9.0.0 labels Oct 22, 2024
@piergm piergm self-assigned this Oct 22, 2024
@elasticsearchmachine
Copy link
Collaborator

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

@elasticsearchmachine
Copy link
Collaborator

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

@piergm piergm requested review from javanna and andreidan October 23, 2024 13:18
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.

Thanks for working on this Matteo.

Really nice PR description and fix 🚀

LGTM, assuming CI is happy

@piergm piergm merged commit 7f573c6 into elastic:main Oct 25, 2024
16 checks passed
@javanna
Copy link
Member

javanna commented Oct 25, 2024

Why is this a 9.0 only change? Should we backport it?

@@ -580,13 +581,14 @@ public void testSortByField() throws Exception {

public void testSortByFieldOneClusterHasNoResults() throws Exception {
assumeMultiClusterSetup();
// set to a value greater than the number of shards to avoid differences due to the skipping of shards
// setting aggs to avoid differences due to the skipping of shards when matching none
Copy link
Member

Choose a reason for hiding this comment

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

this is interesting, the previous suggested we were skipping can match, that would have been a better work-around, but if your code change was required, that means that we were not skipping can match? was it outdated then?

Copy link
Member Author

Choose a reason for hiding this comment

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

The previous comment was outdated, there was a refactor of this method a while back and the setPreFilterShardSize the comments referees to was removed

@@ -206,10 +207,10 @@ public void testCanMatchCoordinator() throws Exception {
)
.setSize(5),
response -> {
assertNull(response.getHits().getTotalHits());
assertEquals(new TotalHits(0, TotalHits.Relation.EQUAL_TO), response.getHits().getTotalHits());
Copy link
Member

Choose a reason for hiding this comment

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

Interesting, why was it null before? We were unskipping one shard, yet getting null total hits?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's because of SearchPhaseController#merge when we skip all the shards we have an empty result and therefore we return SearchResponseSections.EMPTY_WITH_TOTAL_HITS;. I wonder if this should instead be EMPTY_WITHOUT_TOTAL_HITS.

Copy link
Member

@javanna javanna left a comment

Choose a reason for hiding this comment

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

I left a couple of questions, this is a good cleanup!

@piergm
Copy link
Member Author

piergm commented Oct 25, 2024

Why is this a 9.0 only change? Should we backport it?

Thanks for the review @javanna, my reasoning here is that this is not a bug therefore I wasn't sure if we need to backport it. Perhaps just to 8.x?

@piergm
Copy link
Member Author

piergm commented Oct 28, 2024

💚 All backports created successfully

Status Branch Result
8.x

Questions ?

Please refer to the Backport tool documentation

piergm added a commit to piergm/elasticsearch that referenced this pull request Oct 28, 2024
* unskipping shards only when aggs

* Update docs/changelog/115314.yaml

* fixed more tests

* null check for searchRequest.source()

(cherry picked from commit 7f573c6)
@javanna
Copy link
Member

javanna commented Oct 28, 2024

Yes, we tend to backport changes to 8.x for now (next 8.x minor release), bugfixes only to upcoming patch releases.

jfreden pushed a commit to jfreden/elasticsearch that referenced this pull request Nov 4, 2024
* unskipping shards only when aggs

* Update docs/changelog/115314.yaml

* fixed more tests

* null check for searchRequest.source()
cbuescher added a commit that referenced this pull request Dec 2, 2024
This test started failing with the changes made in #115314 when
we only have one shard in the index. This change adjusts test expectations.

Closes #115631
@cbuescher
Copy link
Member

I wanted to backport this change but apparently the original PR that create the test issue never got merged to 8x (contrary to what the labeling says). #115794
I'll refrain from backporting for now because of this.

piergm added a commit that referenced this pull request Dec 5, 2024
…115794)

* Only aggregations require at least one shard request (#115314)

* unskipping shards only when aggs

* Update docs/changelog/115314.yaml

* fixed more tests

* null check for searchRequest.source()

(cherry picked from commit 7f573c6)

* applying #115774

* skipped test

* fixed test

---------

Co-authored-by: Elastic Machine <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>enhancement :Search Foundations/Search Catch all for Search Foundations Team:Search Foundations Meta label for the Search Foundations team in Elasticsearch v8.17.0 v9.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants