-
Notifications
You must be signed in to change notification settings - Fork 25k
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
Throw if two inner_hits have the same name #37645
Conversation
Pinging @elastic/es-search |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @dvehar , the change looks great.
I left some comments regarding the unit tests, we also need an integration test that check that setting two inner hits with the same name throws an exception during the execution of the query. Can you add one in NestedIT
for instance ?
|
||
try { | ||
queryBuilder.extractInnerHitBuilders(Collections.singletonMap("some_name", null)); | ||
fail("Expected an IllegalArgumentException to be thrown"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> queryBuilder.extractInnerHitBuilders(Collections.singletonMap("some_name", null));
instead ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you change the test to use the static function InnerHitContextBuilder#extractInnerHits
to retrieve the builders ? It's the function we use to build the inner hits map.
|
||
try { | ||
queryBuilder.extractInnerHitBuilders(Collections.singletonMap("some_name", null)); | ||
fail("Expected an IllegalArgumentException to be thrown"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
|
||
try { | ||
queryBuilder.extractInnerHitBuilders(Collections.singletonMap("some_name", null)); | ||
fail("Expected an IllegalArgumentException to be thrown"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and here ;)
I'll work on the integration test soon |
@jimczi Could you provide some guidance as to what my test case should look like?
I've tried a few things but can't seem to get NestedQueryBuilder::extractInnerHitBuilders to be called. I'll keep working on it meanwhile. |
I tested locally and the following query failed with the check that you added in this pr:
You should not test aggregations, that's a different issue but we should not allow inner hits in the filter aggregations. They are not supported there so extractInnerHitBuilders is never called. |
aebb18b
to
b205f9d
Compare
@jimczi looks good? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for updating @dvehar . I left more comments.
@@ -460,6 +460,10 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryShardContext) throws I | |||
@Override | |||
protected void extractInnerHitBuilders(Map<String, InnerHitContextBuilder> innerHits) { | |||
if (innerHitBuilder != null) { | |||
if (innerHits.containsKey(innerHitBuilder.getName())) { | |||
throw new IllegalArgumentException("innerHits already contains an entry for key [" + innerHitBuilder.getName() + "]"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should use the same format for the message than the option in the request, can you change innerHits
to [inner_hits]
?
@@ -285,6 +285,10 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryShardContext) throws I | |||
@Override | |||
protected void extractInnerHitBuilders(Map<String, InnerHitContextBuilder> innerHits) { | |||
if (innerHitBuilder != null) { | |||
if (innerHits.containsKey(innerHitBuilder.getName())) { | |||
throw new IllegalArgumentException("innerHits already contains an entry for key [" + innerHitBuilder.getName() + "]"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, innerHits
-> [inner_hits]
@@ -317,10 +318,14 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws | |||
@Override | |||
public void extractInnerHitBuilders(Map<String, InnerHitContextBuilder> innerHits) { | |||
if (innerHitBuilder != null) { | |||
if (innerHits.containsKey(innerHitBuilder.getName())) { | |||
throw new IllegalArgumentException("innerHits already contains an entry for key [" + innerHitBuilder.getName() + "]"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
innerHits
-> [inner_hits]
@@ -460,6 +460,10 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryShardContext) throws I | |||
@Override | |||
protected void extractInnerHitBuilders(Map<String, InnerHitContextBuilder> innerHits) { | |||
if (innerHitBuilder != null) { | |||
if (innerHits.containsKey(innerHitBuilder.getName())) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If innerHitBuilder.getName()
is null we use the type
as the name (see below). Can you build the name first and check in the inner hits map with the inferred one ?:
String name = innerHitBuilder.getName() != null ? innerHitBuilder.getName() : type;
if (innerHits.containsKey(name)) {
...
@@ -285,6 +285,10 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryShardContext) throws I | |||
@Override | |||
protected void extractInnerHitBuilders(Map<String, InnerHitContextBuilder> innerHits) { | |||
if (innerHitBuilder != null) { | |||
if (innerHits.containsKey(innerHitBuilder.getName())) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See above, we should check with the inferred name as well if innerHitBuilder.getName is null.
@@ -317,10 +318,14 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws | |||
@Override | |||
public void extractInnerHitBuilders(Map<String, InnerHitContextBuilder> innerHits) { | |||
if (innerHitBuilder != null) { | |||
if (innerHits.containsKey(innerHitBuilder.getName())) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here the path
is used if the name is null, can you infer the name first and then check inside the map ?
.should(nestedQuery("property", termQuery("property.id", 1D), ScoreMode.None).innerHit(new InnerHitBuilder())) | ||
.should(nestedQuery("property", termQuery("property.id", 1D), ScoreMode.None).innerHit(new InnerHitBuilder())) | ||
).get(); | ||
assertNoFailures(response); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test should fail, we use the path of the nested field as the name when it is not provided so we should also detect the duplication here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah ok. When you posted the example here I was thinking it should not fail. I'll fix this up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah ok, sorry it was not clear
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no worries, I think it was just my limited understanding of things.
@elasticmachine test this please |
@elasticmachine test this please |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @dvehar , the changes look good to me. The tests are failing on unrelated issues so can you merge master in your branch (a simple merge) to make sure that we run the tests with the latest fixes ?
@jimczi done |
@elasticmachine test this please |
@jimczi |
hi @dvehar that last test that was failing is now addressed. could you merge master in so we can re-run tests and hopefully have a green build? ;) |
@javanna done |
@elasticmachine test this please |
retest this please |
retest this please |
retest this please |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @dvehar , CI is finally green. Thanks for iterating and sorry for the unrelated build failures ;).
This change throws an error if two inner_hits have the same name Closes elastic#37584
* master: Replace awaitBusy with assertBusy in atLeastDocsIndexed (elastic#38190) Adjust SearchRequest version checks (elastic#38181) AwaitsFix testClientSucceedsWithVerificationDisabled (elastic#38213) Zen2ify RareClusterStateIT (elastic#38184) ML: Fix error race condition on stop _all datafeeds and close _all jobs (elastic#38113) AwaitsFix PUT mapping with _doc on an index that has types (elastic#38204) Allow built-in monitoring_user role to call GET _xpack API (elastic#38060) Update geo_shape docs to include unsupported features (elastic#38138) [ML] Remove "8" prefixes from file structure finder timestamp formats (elastic#38016) Disable bwc tests while backporting elastic#38104 (elastic#38182) Enable TLSv1.3 by default for JDKs with support (elastic#38103) Fix _host based require filters (elastic#38173) RestoreService should update primary terms when restoring shards of existing indices (elastic#38177) Throw if two inner_hits have the same name (elastic#37645)
@jimczi Thanks for helping me get it through! |
…round-sync-6.x * elastic/6.x: Fix testRestoreIncreasesPrimaryTerms on 6.x (elastic#38314) SQL: Remove exceptions from Analyzer (elastic#38260) (elastic#38287) SQL: Move metrics tracking inside PlanExecutor (elastic#38259) (elastic#38288) Backport of elastic#38311: Move TokenService to seqno powered cas Handle scheduler exceptions (elastic#38183) Mute MlMigrationFullClusterRestartIT#testMigration (elastic#38316) 6.x Backport of elastic#38278: Move ML Optimistic Concurrency Control to Seq No Cleanup construction of interceptors (elastic#38296) Throw if two inner_hits have the same name (elastic#37645) (elastic#38194) AsyncTwoPhaseIndexerTests race condition fixed elastic#38195 Backport#37830 Enable SSL in reindex with security QA tests (elastic#38293) Ensure ILM policies run safely on leader indices (elastic#38140) Introduce ssl settings to reindex from remote (elastic#38292) Fix ordering problem in add or renew lease test (elastic#38281) Mute ReplicationTrackerRetentionLeaseTests#testAddOrRenewRetentionLease (elastic#38276) Fix NPE in Logfile Audit Filter (elastic#38120) (elastic#38271) Enable trace log in FollowerFailOverIT (elastic#38148) SQL: Generate relevant error message when grouping functions are not used in GROUP BY (elastic#38017)
Fixes #37584