|
34 | 34 |
|
35 | 35 | import com.carrotsearch.randomizedtesting.generators.RandomPicks;
|
36 | 36 |
|
| 37 | +import org.apache.lucene.search.BooleanClause; |
| 38 | +import org.apache.lucene.search.BooleanQuery; |
| 39 | +import org.apache.lucene.search.MatchAllDocsQuery; |
37 | 40 | import org.apache.lucene.search.MatchNoDocsQuery;
|
38 | 41 | import org.apache.lucene.search.Query;
|
39 | 42 | import org.apache.lucene.search.join.ScoreMode;
|
40 | 43 | import org.opensearch.OpenSearchException;
|
41 | 44 | import org.opensearch.Version;
|
42 | 45 | import org.opensearch.action.admin.indices.mapping.put.PutMappingRequest;
|
| 46 | +import org.opensearch.cluster.metadata.IndexMetadata; |
43 | 47 | import org.opensearch.common.compress.CompressedXContent;
|
44 | 48 | import org.opensearch.common.settings.Settings;
|
45 | 49 | import org.opensearch.index.IndexSettings;
|
|
58 | 62 | import java.util.Collections;
|
59 | 63 | import java.util.HashMap;
|
60 | 64 | import java.util.Map;
|
| 65 | +import java.util.Optional; |
61 | 66 |
|
62 | 67 | import static org.opensearch.index.IndexSettingsTests.newIndexMeta;
|
63 | 68 | import static org.opensearch.index.query.InnerHitBuilderTests.randomNestedInnerHits;
|
@@ -431,4 +436,96 @@ public void testSetParentFilterInContext() throws Exception {
|
431 | 436 | assertNull(queryShardContext.getParentFilter());
|
432 | 437 | verify(innerQueryBuilder).toQuery(queryShardContext);
|
433 | 438 | }
|
| 439 | + |
| 440 | + public void testNestedDepthProhibited() throws Exception { |
| 441 | + assertThrows(IllegalArgumentException.class, () -> doWithDepth(0, context -> fail("won't call"))); |
| 442 | + } |
| 443 | + |
| 444 | + public void testNestedDepthAllowed() throws Exception { |
| 445 | + ThrowingConsumer<QueryShardContext> check = (context) -> { |
| 446 | + NestedQueryBuilder queryBuilder = new NestedQueryBuilder("nested1", new MatchAllQueryBuilder(), ScoreMode.None); |
| 447 | + OpenSearchToParentBlockJoinQuery blockJoinQuery = (OpenSearchToParentBlockJoinQuery) queryBuilder.toQuery(context); |
| 448 | + Optional<BooleanClause> childLeg = ((BooleanQuery) blockJoinQuery.getChildQuery()).clauses() |
| 449 | + .stream() |
| 450 | + .filter(c -> c.getOccur() == BooleanClause.Occur.MUST) |
| 451 | + .findFirst(); |
| 452 | + assertTrue(childLeg.isPresent()); |
| 453 | + assertEquals(new MatchAllDocsQuery(), childLeg.get().getQuery()); |
| 454 | + }; |
| 455 | + check.accept(createShardContext()); |
| 456 | + doWithDepth(randomIntBetween(1, 20), check); |
| 457 | + } |
| 458 | + |
| 459 | + public void testNestedDepthOnceOnly() throws Exception { |
| 460 | + doWithDepth(1, this::checkOnceNested); |
| 461 | + } |
| 462 | + |
| 463 | + public void testNestedDepthDefault() throws Exception { |
| 464 | + assertEquals(Integer.MAX_VALUE, createShardContext().getIndexSettings().getMaxNestedQueryDepth()); |
| 465 | + } |
| 466 | + |
| 467 | + private void checkOnceNested(QueryShardContext ctx) throws Exception { |
| 468 | + { |
| 469 | + NestedQueryBuilder depth2 = new NestedQueryBuilder( |
| 470 | + "nested1", |
| 471 | + new NestedQueryBuilder("nested1", new MatchAllQueryBuilder(), ScoreMode.None), |
| 472 | + ScoreMode.None |
| 473 | + ); |
| 474 | + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> depth2.toQuery(ctx)); |
| 475 | + assertEquals( |
| 476 | + "The depth of Nested Query is [2] has exceeded the allowed maximum of [1]. This maximum can be set by changing the [index.query.max_nested_depth] index level setting.", |
| 477 | + e.getMessage() |
| 478 | + ); |
| 479 | + } |
| 480 | + { |
| 481 | + QueryBuilder mustBjqMustBjq = new BoolQueryBuilder().must( |
| 482 | + new NestedQueryBuilder("nested1", new MatchAllQueryBuilder(), ScoreMode.None) |
| 483 | + ).must(new NestedQueryBuilder("nested1", new MatchAllQueryBuilder(), ScoreMode.None)); |
| 484 | + BooleanQuery bool = (BooleanQuery) mustBjqMustBjq.toQuery(ctx); |
| 485 | + assertEquals( |
| 486 | + "Can parse joins one by one without breaching depth limit", |
| 487 | + 2, |
| 488 | + bool.clauses().stream().filter(c -> c.getQuery() instanceof OpenSearchToParentBlockJoinQuery).count() |
| 489 | + ); |
| 490 | + } |
| 491 | + } |
| 492 | + |
| 493 | + public void testUpdateMaxDepthSettings() throws Exception { |
| 494 | + doWithDepth(2, (ctx) -> { |
| 495 | + assertEquals(ctx.getIndexSettings().getMaxNestedQueryDepth(), 2); |
| 496 | + NestedQueryBuilder depth2 = new NestedQueryBuilder( |
| 497 | + "nested1", |
| 498 | + new NestedQueryBuilder("nested1", new MatchAllQueryBuilder(), ScoreMode.None), |
| 499 | + ScoreMode.None |
| 500 | + ); |
| 501 | + Query depth2Query = depth2.toQuery(ctx); |
| 502 | + assertTrue(depth2Query instanceof OpenSearchToParentBlockJoinQuery); |
| 503 | + }); |
| 504 | + } |
| 505 | + |
| 506 | + void doWithDepth(int depth, ThrowingConsumer<QueryShardContext> test) throws Exception { |
| 507 | + QueryShardContext context = createShardContext(); |
| 508 | + int defLimit = context.getIndexSettings().getMaxNestedQueryDepth(); |
| 509 | + assertTrue(defLimit > 0); |
| 510 | + Settings updateSettings = Settings.builder() |
| 511 | + .put(context.getIndexSettings().getSettings()) |
| 512 | + .put("index.query.max_nested_depth", depth) |
| 513 | + .build(); |
| 514 | + context.getIndexSettings().updateIndexMetadata(IndexMetadata.builder("index").settings(updateSettings).build()); |
| 515 | + try { |
| 516 | + test.accept(context); |
| 517 | + } finally { |
| 518 | + context.getIndexSettings() |
| 519 | + .updateIndexMetadata( |
| 520 | + IndexMetadata.builder("index") |
| 521 | + .settings( |
| 522 | + Settings.builder() |
| 523 | + .put(context.getIndexSettings().getSettings()) |
| 524 | + .put("index.query.max_nested_depth", defLimit) |
| 525 | + .build() |
| 526 | + ) |
| 527 | + .build() |
| 528 | + ); |
| 529 | + } |
| 530 | + } |
434 | 531 | }
|
0 commit comments