-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Using cached StoredFieldsReader for scroll query optimizations #20112
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
prudhvigodithi
merged 13 commits into
opensearch-project:main
from
prudhvigodithi:scroll_query
Dec 5, 2025
+447
−2
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
56dca5e
Scroll query optimizations
prudhvigodithi c5e1e19
spotless fix
prudhvigodithi 9a047cd
Scroll query optimizations
prudhvigodithi 10a1340
spotless fix
prudhvigodithi 7e57b5d
spotless fix
prudhvigodithi e066765
Merge remote-tracking branch 'upstream/main' into scroll_query
prudhvigodithi 8bd03d5
Fix assertions
prudhvigodithi dc6a4ae
Fix assertions
prudhvigodithi 5bedf51
Update tests
prudhvigodithi bdaafa2
Merge remote-tracking branch 'upstream/main' into scroll_query
prudhvigodithi 1ba5185
update changelog
prudhvigodithi 94d599a
Fix SearchSliceIT
prudhvigodithi 1cc0d36
Fix RetryTests
prudhvigodithi 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
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
151 changes: 151 additions & 0 deletions
151
.../src/internalClusterTest/java/org/opensearch/search/scroll/ScrollStoredFieldsCacheIT.java
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 |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.search.scroll; | ||
|
|
||
| import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; | ||
|
|
||
| import org.apache.lucene.tests.util.LuceneTestCase; | ||
| import org.opensearch.action.search.SearchResponse; | ||
| import org.opensearch.common.settings.Settings; | ||
| import org.opensearch.common.unit.TimeValue; | ||
| import org.opensearch.search.SearchHit; | ||
| import org.opensearch.search.sort.SortOrder; | ||
| import org.opensearch.test.ParameterizedStaticSettingsOpenSearchIntegTestCase; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
|
|
||
| import static org.opensearch.common.xcontent.XContentFactory.jsonBuilder; | ||
| import static org.opensearch.index.query.QueryBuilders.matchAllQuery; | ||
| import static org.opensearch.search.SearchService.CLUSTER_CONCURRENT_SEGMENT_SEARCH_SETTING; | ||
| import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertNoFailures; | ||
| import static org.hamcrest.Matchers.equalTo; | ||
|
|
||
| /** | ||
| * Integration tests for scroll query StoredFieldsReader caching optimization. | ||
| * | ||
| * Tests verify that scroll queries correctly return all documents when using | ||
| * the sequential reader cache optimization for stored fields. | ||
| */ | ||
| @LuceneTestCase.SuppressCodecs("*") | ||
| public class ScrollStoredFieldsCacheIT extends ParameterizedStaticSettingsOpenSearchIntegTestCase { | ||
|
|
||
| public ScrollStoredFieldsCacheIT(Settings settings) { | ||
| super(settings); | ||
| } | ||
|
|
||
| @ParametersFactory | ||
| public static Collection<Object[]> parameters() { | ||
| return Arrays.asList( | ||
| new Object[] { Settings.builder().put(CLUSTER_CONCURRENT_SEGMENT_SEARCH_SETTING.getKey(), false).build() }, | ||
| new Object[] { Settings.builder().put(CLUSTER_CONCURRENT_SEGMENT_SEARCH_SETTING.getKey(), true).build() } | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that scroll queries with sequential document access correctly utilize | ||
| * the StoredFieldsReader cache and return all documents without data corruption. | ||
| */ | ||
| public void testScrollWithSequentialReaderCache() throws Exception { | ||
| int numDocs = randomIntBetween(100, 500); | ||
| int scrollSize = randomIntBetween(10, 50); | ||
| createIndex("test", Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0).build()); | ||
| ensureGreen("test"); | ||
| // Index documents | ||
| for (int i = 0; i < numDocs; i++) { | ||
| client().prepareIndex("test") | ||
| .setId(Integer.toString(i)) | ||
| .setSource(jsonBuilder().startObject().field("field", i).field("text", "document " + i).endObject()) | ||
| .get(); | ||
| } | ||
| refresh("test"); | ||
| indexRandomForConcurrentSearch("test"); | ||
| Set<String> retrievedIds = new HashSet<>(); | ||
| SearchResponse searchResponse = client().prepareSearch("test") | ||
| .setQuery(matchAllQuery()) | ||
| .setSize(scrollSize) | ||
| .setScroll(TimeValue.timeValueMinutes(2)) | ||
| .addSort("field", SortOrder.ASC) | ||
| .get(); | ||
|
|
||
| try { | ||
| assertNoFailures(searchResponse); | ||
| assertThat(searchResponse.getHits().getTotalHits().value(), equalTo((long) numDocs)); | ||
| do { | ||
| for (SearchHit hit : searchResponse.getHits().getHits()) { | ||
| // Verify no duplicate documents | ||
| assertTrue("Duplicate document id: " + hit.getId(), retrievedIds.add(hit.getId())); | ||
| // Verify document content is correct _source field | ||
| assertNotNull(hit.getSourceAsMap()); | ||
| assertEquals(Integer.parseInt(hit.getId()), hit.getSourceAsMap().get("field")); | ||
| } | ||
| searchResponse = client().prepareSearchScroll(searchResponse.getScrollId()).setScroll(TimeValue.timeValueMinutes(2)).get(); | ||
| assertNoFailures(searchResponse); | ||
| } while (searchResponse.getHits().getHits().length > 0); | ||
| // Verify all documents were retrieved | ||
| assertThat(retrievedIds.size(), equalTo(numDocs)); | ||
| } finally { | ||
| clearScroll(searchResponse.getScrollId()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Tests scroll queries across multiple segments with batch sizes that | ||
| * trigger the sequential reader optimization (>= 10 docs). | ||
| */ | ||
| public void testScrollAcrossMultipleSegments() throws Exception { | ||
| int docsPerSegment = randomIntBetween(20, 50); | ||
| int numSegments = randomIntBetween(3, 5); | ||
| int totalDocs = docsPerSegment * numSegments; | ||
| int scrollSize = randomIntBetween(10, 50); | ||
| int expectedBatches = (totalDocs + scrollSize - 1) / scrollSize; | ||
| createIndex("test", Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0).build()); | ||
| ensureGreen("test"); | ||
| // Index documents in batches to create multiple segments | ||
| for (int seg = 0; seg < numSegments; seg++) { | ||
| for (int i = 0; i < docsPerSegment; i++) { | ||
| int docId = seg * docsPerSegment + i; | ||
| client().prepareIndex("test") | ||
| .setId(Integer.toString(docId)) | ||
| .setSource(jsonBuilder().startObject().field("field", docId).field("segment", seg).endObject()) | ||
| .get(); | ||
| } | ||
| refresh("test"); | ||
| } | ||
| indexRandomForConcurrentSearch("test"); | ||
| Set<String> retrievedIds = new HashSet<>(); | ||
| SearchResponse searchResponse = client().prepareSearch("test") | ||
| .setQuery(matchAllQuery()) | ||
| .setSize(scrollSize) | ||
| .setScroll(TimeValue.timeValueMinutes(2)) | ||
| .addSort("field", SortOrder.ASC) | ||
| .get(); | ||
| try { | ||
| assertNoFailures(searchResponse); | ||
| assertThat(searchResponse.getHits().getTotalHits().value(), equalTo((long) totalDocs)); | ||
| int batchCount = 0; | ||
| do { | ||
| batchCount++; | ||
| for (SearchHit hit : searchResponse.getHits().getHits()) { | ||
| assertTrue("Duplicate document id: " + hit.getId(), retrievedIds.add(hit.getId())); | ||
| } | ||
| searchResponse = client().prepareSearchScroll(searchResponse.getScrollId()).setScroll(TimeValue.timeValueMinutes(2)).get(); | ||
| assertNoFailures(searchResponse); | ||
| } while (searchResponse.getHits().getHits().length > 0); | ||
| // Verify all documents retrieved | ||
| assertThat(retrievedIds.size(), equalTo(totalDocs)); | ||
| // Verify exact batch count | ||
| assertThat(batchCount, equalTo(expectedBatches)); | ||
| } finally { | ||
| clearScroll(searchResponse.getScrollId()); | ||
| } | ||
| } | ||
| } |
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
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
Oops, something went wrong.
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.