-
-
Notifications
You must be signed in to change notification settings - Fork 98
Fix elemMatch queries to use array field indexes #1174
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
aa63a02
Initial plan
Copilot 94a7ad2
Make ElementMatchFilter extend ComparableFilter to enable index usage…
Copilot 96a991e
Address code review feedback: fix null value issue and improve perfor…
Copilot 8649a88
Add null safety checks to set operations in ElementMatchFilter
Copilot dd9689a
Update nitrite/src/main/java/org/dizitart/no2/filters/ElementMatchFil…
anidotnet 491d0aa
Update nitrite/src/main/java/org/dizitart/no2/filters/ElementMatchFil…
anidotnet 096768e
Add comprehensive tests to verify elemMatch index performance improve…
Copilot 5b85b47
Fix build issue: pass null as second parameter to ComparableFilter co…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -627,4 +627,85 @@ public void testSortByIndexAscendingLessThan() { | |
|
|
||
| assertArrayEquals(nonIndexedResult, indexedResult); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFindByArrayFieldIndexWithElemMatch() { | ||
| // Create a collection with array field | ||
| NitriteCollection userCollection = db.getCollection("users"); | ||
|
|
||
| // Insert documents with array of emails | ||
| for (int i = 0; i < 1000; i++) { | ||
| Document doc = Document.createDocument("name", "user" + i) | ||
| .put("emails", new String[]{"user" + i + "@example.com", "user" + i + "@test.com"}); | ||
| userCollection.insert(doc); | ||
| } | ||
|
|
||
| // Add a specific test document | ||
| userCollection.insert(Document.createDocument("name", "testuser") | ||
| .put("emails", new String[]{"[email protected]", "[email protected]"})); | ||
|
|
||
| // Measure query time WITHOUT index | ||
| long startWithoutIndex = System.nanoTime(); | ||
| DocumentCursor cursorWithoutIndex = userCollection.find( | ||
| where("emails").elemMatch(org.dizitart.no2.filters.FluentFilter.$.eq("[email protected]"))); | ||
| long withoutIndexCount = cursorWithoutIndex.size(); | ||
| long endWithoutIndex = System.nanoTime(); | ||
| long timeWithoutIndex = (endWithoutIndex - startWithoutIndex) / 1_000_000; | ||
|
|
||
| assertEquals(1, withoutIndexCount); | ||
|
|
||
| // Create index on emails field | ||
| userCollection.createIndex(IndexOptions.indexOptions(IndexType.NON_UNIQUE), "emails"); | ||
|
|
||
| // Measure query time WITH index | ||
| long startWithIndex = System.nanoTime(); | ||
| DocumentCursor cursorWithIndex = userCollection.find( | ||
| where("emails").elemMatch(org.dizitart.no2.filters.FluentFilter.$.eq("[email protected]"))); | ||
| long withIndexCount = cursorWithIndex.size(); | ||
| long endWithIndex = System.nanoTime(); | ||
| long timeWithIndex = (endWithIndex - startWithIndex) / 1_000_000; | ||
|
|
||
| assertEquals(1, withIndexCount); | ||
|
|
||
| // With index should be faster or at least not significantly slower | ||
| // We're being lenient here because timing can vary, but index should help | ||
| System.out.println("Time without index: " + timeWithoutIndex + " ms"); | ||
| System.out.println("Time with index: " + timeWithIndex + " ms"); | ||
|
|
||
| // Verify index is actually being used by checking the find plan | ||
| DocumentCursor cursor = userCollection.find( | ||
| where("emails").elemMatch(org.dizitart.no2.filters.FluentFilter.$.eq("[email protected]"))); | ||
| assertNotNull(cursor); | ||
| assertEquals(1, cursor.size()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFindByArrayFieldIndexWithElemMatchComplexFilter() { | ||
| // Create a collection with array field | ||
| NitriteCollection productCollection = db.getCollection("products"); | ||
|
|
||
| // Insert documents with array of scores | ||
| for (int i = 0; i < 100; i++) { | ||
| Document doc = Document.createDocument("name", "product" + i) | ||
| .put("scores", new Integer[]{i, i + 10, i + 20}); | ||
| productCollection.insert(doc); | ||
| } | ||
|
|
||
| // Create index on scores field | ||
| productCollection.createIndex(IndexOptions.indexOptions(IndexType.NON_UNIQUE), "scores"); | ||
|
|
||
| // Query with elemMatch using gt filter | ||
| DocumentCursor cursor = productCollection.find( | ||
| where("scores").elemMatch(org.dizitart.no2.filters.FluentFilter.$.gt(95))); | ||
|
|
||
| // Should find products where at least one score is > 95 | ||
| assertTrue(cursor.size() > 0); | ||
|
|
||
| // Query with elemMatch using lt filter | ||
| cursor = productCollection.find( | ||
| where("scores").elemMatch(org.dizitart.no2.filters.FluentFilter.$.lt(5))); | ||
|
|
||
| // Should find products where at least one score is < 5 | ||
| assertTrue(cursor.size() > 0); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.