Commit fc31eac
Add query rewriting infrastructure to reduce query complexity (opensearch-project#19060)
* Add query rewriting infrastructure to reduce query complexity
Implements three query optimizations that work together:
- Boolean flattening: removes unnecessary nested boolean queries
- Terms merging: combines multiple term queries on same field in filter/should contexts
- Match-all removal: eliminates redundant match_all queries
Key features:
- 60-70% reduction in query nodes for typical filtered queries
- Feature flag: search.query_rewriting.enabled (default: true)
- Preserves exact query semantics and results
Signed-off-by: Atri Sharma <[email protected]>
* Fix forbidden api issues
Signed-off-by: Atri Sharma <[email protected]>
* Update writers and get tests to pass
Signed-off-by: Atri Sharma <[email protected]>
* Update per CI
Signed-off-by: Atri Sharma <[email protected]>
* Fix term merging threshold and update comments
Signed-off-by: Atri Sharma <[email protected]>
* Expose setting and update per comments
Signed-off-by: Atri Sharma <[email protected]>
* Update CHANGELOG
Signed-off-by: Atri Sharma <[email protected]>
* Fix tests and ensure scoring MATCH ALL query is preserved
Signed-off-by: Atri Sharma <[email protected]>
* Migrate must to filter and must not to should optimizations to query rewriting infrastructure
This commit migrates two existing query optimizations from BoolQueryBuilder to the new
query rewriting infrastructure:
1. **MustToFilterRewriter**: Moves non scoring queries (range, geo, numeric term/terms/match)
from must to filter clauses to avoid unnecessary scoring calculations (from PR opensearch-project#18541)
2. **MustNotToShouldRewriter**: Transforms negative queries into positive complements for
better performance on single valued numeric fields (from PRs opensearch-project#17655 and opensearch-project#18498)
Changes:
Add MustToFilterRewriter with priority 150 (runs after boolean flattening)
Add MustNotToShouldRewriter with priority 175 (runs after must to filter)
Register both rewriters in QueryRewriterRegistry
Add comprehensive test suites (15 tests for must to filter, 14 for must not to should)
Disable legacy implementations in BoolQueryBuilder
Comment out BoolQueryBuilder tests that relied on the old implementations
The new rewriters maintain full backward compatibility while providing:
Better separation of concerns
Recursive rewriting for nested boolean queries
Proper error handling and logging
Consistent priority based execution order
Signed-off-by: Atri Sharma <[email protected]>
* Handle fields with missing fields
Signed-off-by: Atri Sharma <[email protected]>
---------
Signed-off-by: Atri Sharma <[email protected]>1 parent 1a0a971 commit fc31eac
File tree
19 files changed
+3012
-208
lines changed- server/src
- main/java/org/opensearch
- common/settings
- index/query
- search
- query
- rewriters
- test/java/org/opensearch
- index/query
- search/query
- rewriters
19 files changed
+3012
-208
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
| 63 | + | |
63 | 64 | | |
64 | 65 | | |
65 | 66 | | |
| |||
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
804 | 804 | | |
805 | 805 | | |
806 | 806 | | |
| 807 | + | |
| 808 | + | |
807 | 809 | | |
808 | 810 | | |
809 | 811 | | |
| |||
Lines changed: 0 additions & 54 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
52 | | - | |
53 | | - | |
54 | 52 | | |
55 | 53 | | |
56 | 54 | | |
| |||
402 | 400 | | |
403 | 401 | | |
404 | 402 | | |
405 | | - | |
406 | | - | |
407 | | - | |
408 | 403 | | |
409 | 404 | | |
410 | 405 | | |
| |||
559 | 554 | | |
560 | 555 | | |
561 | 556 | | |
562 | | - | |
563 | | - | |
564 | | - | |
565 | | - | |
566 | | - | |
567 | | - | |
568 | | - | |
569 | | - | |
570 | | - | |
571 | | - | |
572 | | - | |
573 | | - | |
574 | | - | |
575 | | - | |
576 | | - | |
577 | | - | |
578 | | - | |
579 | | - | |
580 | | - | |
581 | | - | |
582 | | - | |
583 | | - | |
584 | | - | |
585 | | - | |
586 | | - | |
587 | | - | |
588 | | - | |
589 | | - | |
590 | | - | |
591 | | - | |
592 | | - | |
593 | | - | |
594 | | - | |
595 | | - | |
596 | | - | |
597 | | - | |
598 | | - | |
599 | | - | |
600 | | - | |
601 | | - | |
602 | | - | |
603 | | - | |
604 | | - | |
605 | | - | |
606 | | - | |
607 | | - | |
608 | | - | |
609 | | - | |
610 | | - | |
611 | 557 | | |
Lines changed: 33 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
136 | 136 | | |
137 | 137 | | |
138 | 138 | | |
| 139 | + | |
139 | 140 | | |
140 | 141 | | |
141 | 142 | | |
| |||
276 | 277 | | |
277 | 278 | | |
278 | 279 | | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
279 | 301 | | |
280 | 302 | | |
281 | 303 | | |
| |||
507 | 529 | | |
508 | 530 | | |
509 | 531 | | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
510 | 536 | | |
511 | 537 | | |
512 | 538 | | |
| |||
1488 | 1514 | | |
1489 | 1515 | | |
1490 | 1516 | | |
1491 | | - | |
1492 | | - | |
| 1517 | + | |
| 1518 | + | |
| 1519 | + | |
| 1520 | + | |
| 1521 | + | |
| 1522 | + | |
| 1523 | + | |
1493 | 1524 | | |
1494 | 1525 | | |
1495 | 1526 | | |
| |||
Lines changed: 50 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
Lines changed: 113 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
0 commit comments