From 1a59c1b4fe5480cd7001926a72a1cdda3a00b406 Mon Sep 17 00:00:00 2001 From: Vacha Shah Date: Thu, 18 Jan 2024 00:50:07 +0000 Subject: [PATCH] Fixing tests Signed-off-by: Vacha Shah --- .../search/DefaultSearchContext.java | 2 +- .../search/fetch/FetchSearchResult.java | 16 +------ .../search/query/QuerySearchResult.java | 8 +--- .../action/search/DfsQueryPhaseTests.java | 13 ++---- .../action/search/FetchSearchPhaseTests.java | 28 ++++-------- .../search/SearchPhaseControllerTests.java | 43 ++++++------------- .../SearchQueryThenFetchAsyncActionTests.java | 4 +- .../search/query/QuerySearchResultTests.java | 3 +- 8 files changed, 32 insertions(+), 85 deletions(-) diff --git a/server/src/main/java/org/opensearch/search/DefaultSearchContext.java b/server/src/main/java/org/opensearch/search/DefaultSearchContext.java index b955d3fcfee87..960b46d68977b 100644 --- a/server/src/main/java/org/opensearch/search/DefaultSearchContext.java +++ b/server/src/main/java/org/opensearch/search/DefaultSearchContext.java @@ -210,7 +210,7 @@ final class DefaultSearchContext extends SearchContext { // SearchContexts use a BigArrays that can circuit break this.bigArrays = bigArrays.withCircuitBreaking(); this.dfsResult = new DfsSearchResult(readerContext.id(), shardTarget, request); - this.queryResult = new QuerySearchResult(readerContext.id(), shardTarget, request, clusterService.getSettings()); + this.queryResult = new QuerySearchResult(readerContext.id(), shardTarget, request); this.fetchResult = new FetchSearchResult(readerContext.id(), shardTarget); this.indexService = readerContext.indexService(); this.indexShard = readerContext.indexShard(); diff --git a/server/src/main/java/org/opensearch/search/fetch/FetchSearchResult.java b/server/src/main/java/org/opensearch/search/fetch/FetchSearchResult.java index 76368f3f2de07..29dc3d5889d50 100644 --- a/server/src/main/java/org/opensearch/search/fetch/FetchSearchResult.java +++ b/server/src/main/java/org/opensearch/search/fetch/FetchSearchResult.java @@ -32,10 +32,9 @@ package org.opensearch.search.fetch; -import com.google.protobuf.ByteString; import org.apache.lucene.search.TotalHits.Relation; import org.opensearch.common.annotation.PublicApi; -import org.opensearch.core.common.bytes.BytesReference; +import org.opensearch.common.util.FeatureFlags; import org.opensearch.core.common.io.stream.StreamInput; import org.opensearch.core.common.io.stream.StreamOutput; import org.opensearch.search.SearchHit; @@ -105,7 +104,7 @@ public FetchSearchResult fetchResult() { public void hits(SearchHits hits) { assert assertNoSearchTarget(hits); this.hits = hits; - if (this.fetchSearchResultProto != null) { + if (FeatureFlags.isEnabled(FeatureFlags.PROTOBUF_SETTING) && this.fetchSearchResultProto != null) { QuerySearchResultProto.TotalHits.Builder totalHitsBuilder = QuerySearchResultProto.TotalHits.newBuilder(); totalHitsBuilder.setValue(hits.getTotalHits().value); totalHitsBuilder.setRelation( @@ -116,17 +115,6 @@ public void hits(SearchHits hits) { FetchSearchResultProto.SearchHits.Builder searchHitsBuilder = FetchSearchResultProto.SearchHits.newBuilder(); searchHitsBuilder.setMaxScore(hits.getMaxScore()); searchHitsBuilder.setTotalHits(totalHitsBuilder.build()); - for (SearchHit hit : hits.getHits()) { - FetchSearchResultProto.SearchHit.Builder searchHitBuilder = FetchSearchResultProto.SearchHit.newBuilder(); - searchHitBuilder.setIndex(hit.getIndex()); - searchHitBuilder.setId(hit.getId()); - searchHitBuilder.setScore(hit.getScore()); - searchHitBuilder.setSeqNo(hit.getSeqNo()); - searchHitBuilder.setPrimaryTerm(hit.getPrimaryTerm()); - searchHitBuilder.setVersion(hit.getVersion()); - searchHitBuilder.setSource(ByteString.copyFrom(BytesReference.toBytes(hit.getSourceRef()))); - searchHitsBuilder.addHits(searchHitBuilder.build()); - } this.fetchSearchResultProto = this.fetchSearchResultProto.toBuilder().setHits(searchHitsBuilder.build()).build(); } } diff --git a/server/src/main/java/org/opensearch/search/query/QuerySearchResult.java b/server/src/main/java/org/opensearch/search/query/QuerySearchResult.java index f786c00d05377..599ba66850409 100644 --- a/server/src/main/java/org/opensearch/search/query/QuerySearchResult.java +++ b/server/src/main/java/org/opensearch/search/query/QuerySearchResult.java @@ -40,7 +40,6 @@ import org.opensearch.common.annotation.PublicApi; import org.opensearch.common.io.stream.DelayableWriteable; import org.opensearch.common.lucene.search.TopDocsAndMaxScore; -import org.opensearch.common.settings.Settings; import org.opensearch.common.util.FeatureFlags; import org.opensearch.core.common.io.stream.StreamInput; import org.opensearch.core.common.io.stream.StreamOutput; @@ -119,12 +118,7 @@ public QuerySearchResult(byte[] in) throws IOException { isNull = false; } - public QuerySearchResult( - ShardSearchContextId contextId, - SearchShardTarget shardTarget, - ShardSearchRequest shardSearchRequest, - Settings settings - ) { + public QuerySearchResult(ShardSearchContextId contextId, SearchShardTarget shardTarget, ShardSearchRequest shardSearchRequest) { this.contextId = contextId; setSearchShardTarget(shardTarget); isNull = false; diff --git a/server/src/test/java/org/opensearch/action/search/DfsQueryPhaseTests.java b/server/src/test/java/org/opensearch/action/search/DfsQueryPhaseTests.java index 68ae014c9d562..6952841c295e2 100644 --- a/server/src/test/java/org/opensearch/action/search/DfsQueryPhaseTests.java +++ b/server/src/test/java/org/opensearch/action/search/DfsQueryPhaseTests.java @@ -39,7 +39,6 @@ import org.apache.lucene.tests.store.MockDirectoryWrapper; import org.opensearch.action.OriginalIndices; import org.opensearch.common.lucene.search.TopDocsAndMaxScore; -import org.opensearch.common.settings.Settings; import org.opensearch.common.util.concurrent.AtomicArray; import org.opensearch.common.util.concurrent.OpenSearchExecutors; import org.opensearch.core.common.breaker.CircuitBreaker; @@ -102,8 +101,7 @@ public void sendExecuteQuery( QuerySearchResult queryResult = new QuerySearchResult( new ShardSearchContextId("", 123), new SearchShardTarget("node1", new ShardId("test", "na", 0), null, OriginalIndices.NONE), - null, - Settings.EMPTY + null ); queryResult.topDocs( new TopDocsAndMaxScore( @@ -118,8 +116,7 @@ public void sendExecuteQuery( QuerySearchResult queryResult = new QuerySearchResult( new ShardSearchContextId("", 123), new SearchShardTarget("node2", new ShardId("test", "na", 0), null, OriginalIndices.NONE), - null, - Settings.EMPTY + null ); queryResult.topDocs( new TopDocsAndMaxScore( @@ -202,8 +199,7 @@ public void sendExecuteQuery( QuerySearchResult queryResult = new QuerySearchResult( new ShardSearchContextId("", 123), new SearchShardTarget("node1", new ShardId("test", "na", 0), null, OriginalIndices.NONE), - null, - Settings.EMPTY + null ); queryResult.topDocs( new TopDocsAndMaxScore( @@ -290,8 +286,7 @@ public void sendExecuteQuery( QuerySearchResult queryResult = new QuerySearchResult( new ShardSearchContextId("", 123), new SearchShardTarget("node1", new ShardId("test", "na", 0), null, OriginalIndices.NONE), - null, - Settings.EMPTY + null ); queryResult.topDocs( new TopDocsAndMaxScore( diff --git a/server/src/test/java/org/opensearch/action/search/FetchSearchPhaseTests.java b/server/src/test/java/org/opensearch/action/search/FetchSearchPhaseTests.java index 09f03c5ba2e18..1eb3a44642806 100644 --- a/server/src/test/java/org/opensearch/action/search/FetchSearchPhaseTests.java +++ b/server/src/test/java/org/opensearch/action/search/FetchSearchPhaseTests.java @@ -38,7 +38,6 @@ import org.opensearch.action.OriginalIndices; import org.opensearch.common.UUIDs; import org.opensearch.common.lucene.search.TopDocsAndMaxScore; -import org.opensearch.common.settings.Settings; import org.opensearch.common.util.concurrent.OpenSearchExecutors; import org.opensearch.core.common.breaker.CircuitBreaker; import org.opensearch.core.common.breaker.NoopCircuitBreaker; @@ -141,8 +140,7 @@ public void testFetchTwoDocument() { QuerySearchResult queryResult = new QuerySearchResult( ctx1, new SearchShardTarget("node1", new ShardId("test", "na", 0), null, OriginalIndices.NONE), - null, - Settings.EMPTY + null ); queryResult.topDocs( new TopDocsAndMaxScore( @@ -159,8 +157,7 @@ public void testFetchTwoDocument() { queryResult = new QuerySearchResult( ctx2, new SearchShardTarget("node2", new ShardId("test", "na", 1), null, OriginalIndices.NONE), - null, - Settings.EMPTY + null ); queryResult.topDocs( new TopDocsAndMaxScore( @@ -239,8 +236,7 @@ public void testFailFetchOneDoc() { QuerySearchResult queryResult = new QuerySearchResult( ctx, new SearchShardTarget("node1", new ShardId("test", "na", 0), null, OriginalIndices.NONE), - null, - Settings.EMPTY + null ); queryResult.topDocs( new TopDocsAndMaxScore( @@ -256,8 +252,7 @@ public void testFailFetchOneDoc() { queryResult = new QuerySearchResult( new ShardSearchContextId("", 321), new SearchShardTarget("node2", new ShardId("test", "na", 1), null, OriginalIndices.NONE), - null, - Settings.EMPTY + null ); queryResult.topDocs( new TopDocsAndMaxScore( @@ -338,8 +333,7 @@ public void testFetchDocsConcurrently() throws InterruptedException { QuerySearchResult queryResult = new QuerySearchResult( new ShardSearchContextId("", i), new SearchShardTarget("node1", new ShardId("test", "na", 0), null, OriginalIndices.NONE), - null, - Settings.EMPTY + null ); queryResult.topDocs( new TopDocsAndMaxScore( @@ -429,8 +423,7 @@ public void testExceptionFailsPhase() { QuerySearchResult queryResult = new QuerySearchResult( new ShardSearchContextId("", 123), new SearchShardTarget("node1", new ShardId("test", "na", 0), null, OriginalIndices.NONE), - null, - Settings.EMPTY + null ); queryResult.topDocs( new TopDocsAndMaxScore( @@ -446,8 +439,7 @@ public void testExceptionFailsPhase() { queryResult = new QuerySearchResult( new ShardSearchContextId("", 321), new SearchShardTarget("node2", new ShardId("test", "na", 1), null, OriginalIndices.NONE), - null, - Settings.EMPTY + null ); queryResult.topDocs( new TopDocsAndMaxScore( @@ -524,8 +516,7 @@ public void testCleanupIrrelevantContexts() { // contexts that are not fetched s QuerySearchResult queryResult = new QuerySearchResult( ctx1, new SearchShardTarget("node1", new ShardId("test", "na", 0), null, OriginalIndices.NONE), - null, - Settings.EMPTY + null ); queryResult.topDocs( new TopDocsAndMaxScore( @@ -542,8 +533,7 @@ public void testCleanupIrrelevantContexts() { // contexts that are not fetched s queryResult = new QuerySearchResult( ctx2, new SearchShardTarget("node2", new ShardId("test", "na", 1), null, OriginalIndices.NONE), - null, - Settings.EMPTY + null ); queryResult.topDocs( new TopDocsAndMaxScore( diff --git a/server/src/test/java/org/opensearch/action/search/SearchPhaseControllerTests.java b/server/src/test/java/org/opensearch/action/search/SearchPhaseControllerTests.java index eeb72969990fc..a927f733cc504 100644 --- a/server/src/test/java/org/opensearch/action/search/SearchPhaseControllerTests.java +++ b/server/src/test/java/org/opensearch/action/search/SearchPhaseControllerTests.java @@ -410,12 +410,7 @@ private static AtomicArray generateQueryResults( clusterAlias, OriginalIndices.NONE ); - QuerySearchResult querySearchResult = new QuerySearchResult( - new ShardSearchContextId("", shardIndex), - searchShardTarget, - null, - Settings.EMPTY - ); + QuerySearchResult querySearchResult = new QuerySearchResult(new ShardSearchContextId("", shardIndex), searchShardTarget, null); final TopDocs topDocs; float maxScore = 0; if (searchHitsSize == 0) { @@ -690,8 +685,7 @@ private void consumerTestCase(int numEmptyResponses) throws Exception { QuerySearchResult result = new QuerySearchResult( new ShardSearchContextId("", 0), new SearchShardTarget("node", new ShardId("a", "b", 0), null, OriginalIndices.NONE), - null, - Settings.EMPTY + null ); result.topDocs( new TopDocsAndMaxScore(new TopDocs(new TotalHits(0, TotalHits.Relation.EQUAL_TO), new ScoreDoc[0]), Float.NaN), @@ -705,8 +699,7 @@ private void consumerTestCase(int numEmptyResponses) throws Exception { result = new QuerySearchResult( new ShardSearchContextId("", 1), new SearchShardTarget("node", new ShardId("a", "b", 0), null, OriginalIndices.NONE), - null, - Settings.EMPTY + null ); result.topDocs( new TopDocsAndMaxScore(new TopDocs(new TotalHits(0, TotalHits.Relation.EQUAL_TO), new ScoreDoc[0]), Float.NaN), @@ -720,8 +713,7 @@ private void consumerTestCase(int numEmptyResponses) throws Exception { result = new QuerySearchResult( new ShardSearchContextId("", 1), new SearchShardTarget("node", new ShardId("a", "b", 0), null, OriginalIndices.NONE), - null, - Settings.EMPTY + null ); result.topDocs( new TopDocsAndMaxScore(new TopDocs(new TotalHits(0, TotalHits.Relation.EQUAL_TO), new ScoreDoc[0]), Float.NaN), @@ -797,8 +789,7 @@ public void testConsumerConcurrently() throws Exception { QuerySearchResult result = new QuerySearchResult( new ShardSearchContextId("", id), new SearchShardTarget("node", new ShardId("a", "b", id), null, OriginalIndices.NONE), - null, - Settings.EMPTY + null ); result.topDocs( new TopDocsAndMaxScore( @@ -859,8 +850,7 @@ public void testConsumerOnlyAggs() throws Exception { QuerySearchResult result = new QuerySearchResult( new ShardSearchContextId("", i), new SearchShardTarget("node", new ShardId("a", "b", i), null, OriginalIndices.NONE), - null, - Settings.EMPTY + null ); result.topDocs( new TopDocsAndMaxScore(new TopDocs(new TotalHits(1, TotalHits.Relation.EQUAL_TO), new ScoreDoc[0]), number), @@ -913,8 +903,7 @@ public void testConsumerOnlyHits() throws Exception { QuerySearchResult result = new QuerySearchResult( new ShardSearchContextId("", i), new SearchShardTarget("node", new ShardId("a", "b", i), null, OriginalIndices.NONE), - null, - Settings.EMPTY + null ); result.topDocs( new TopDocsAndMaxScore( @@ -970,8 +959,7 @@ public void testReduceTopNWithFromOffset() throws Exception { QuerySearchResult result = new QuerySearchResult( new ShardSearchContextId("", i), new SearchShardTarget("node", new ShardId("a", "b", i), null, OriginalIndices.NONE), - null, - Settings.EMPTY + null ); ScoreDoc[] docs = new ScoreDoc[3]; for (int j = 0; j < docs.length; j++) { @@ -1026,8 +1014,7 @@ public void testConsumerSortByField() throws Exception { QuerySearchResult result = new QuerySearchResult( new ShardSearchContextId("", i), new SearchShardTarget("node", new ShardId("a", "b", i), null, OriginalIndices.NONE), - null, - Settings.EMPTY + null ); result.topDocs(new TopDocsAndMaxScore(topDocs, Float.NaN), docValueFormats); result.setShardIndex(i); @@ -1076,8 +1063,7 @@ public void testConsumerFieldCollapsing() throws Exception { QuerySearchResult result = new QuerySearchResult( new ShardSearchContextId("", i), new SearchShardTarget("node", new ShardId("a", "b", i), null, OriginalIndices.NONE), - null, - Settings.EMPTY + null ); result.topDocs(new TopDocsAndMaxScore(topDocs, Float.NaN), docValueFormats); result.setShardIndex(i); @@ -1121,8 +1107,7 @@ public void testConsumerSuggestions() throws Exception { QuerySearchResult result = new QuerySearchResult( new ShardSearchContextId("", i), new SearchShardTarget("node", new ShardId("a", "b", i), null, OriginalIndices.NONE), - null, - Settings.EMPTY + null ); List>> suggestions = new ArrayList<>(); @@ -1263,8 +1248,7 @@ public void onFinalReduce(List shards, TotalHits totalHits, Interna QuerySearchResult result = new QuerySearchResult( new ShardSearchContextId("", id), new SearchShardTarget("node", new ShardId("a", "b", id), null, OriginalIndices.NONE), - null, - Settings.EMPTY + null ); result.topDocs( new TopDocsAndMaxScore( @@ -1350,8 +1334,7 @@ private void testReduceCase(boolean shouldFail) throws Exception { QuerySearchResult result = new QuerySearchResult( new ShardSearchContextId(UUIDs.randomBase64UUID(), index), new SearchShardTarget("node", new ShardId("a", "b", index), null, OriginalIndices.NONE), - null, - Settings.EMPTY + null ); result.topDocs( new TopDocsAndMaxScore(new TopDocs(new TotalHits(0, TotalHits.Relation.EQUAL_TO), Lucene.EMPTY_SCORE_DOCS), Float.NaN), diff --git a/server/src/test/java/org/opensearch/action/search/SearchQueryThenFetchAsyncActionTests.java b/server/src/test/java/org/opensearch/action/search/SearchQueryThenFetchAsyncActionTests.java index 3f0a1e3a177a7..faf6f86c69c27 100644 --- a/server/src/test/java/org/opensearch/action/search/SearchQueryThenFetchAsyncActionTests.java +++ b/server/src/test/java/org/opensearch/action/search/SearchQueryThenFetchAsyncActionTests.java @@ -43,7 +43,6 @@ import org.opensearch.cluster.node.DiscoveryNode; import org.opensearch.cluster.routing.GroupShardsIterator; import org.opensearch.common.lucene.search.TopDocsAndMaxScore; -import org.opensearch.common.settings.Settings; import org.opensearch.common.unit.TimeValue; import org.opensearch.common.util.concurrent.OpenSearchExecutors; import org.opensearch.core.common.Strings; @@ -127,8 +126,7 @@ public void sendExecuteQuery( QuerySearchResult queryResult = new QuerySearchResult( new ShardSearchContextId("N/A", 123), new SearchShardTarget("node1", new ShardId("idx", "na", shardId), null, OriginalIndices.NONE), - null, - Settings.EMPTY + null ); SortField sortField = new SortField("timestamp", SortField.Type.LONG); if (withCollapse) { diff --git a/server/src/test/java/org/opensearch/search/query/QuerySearchResultTests.java b/server/src/test/java/org/opensearch/search/query/QuerySearchResultTests.java index 81e9f4433cdc9..41e4e1ae45a73 100644 --- a/server/src/test/java/org/opensearch/search/query/QuerySearchResultTests.java +++ b/server/src/test/java/org/opensearch/search/query/QuerySearchResultTests.java @@ -84,8 +84,7 @@ private static QuerySearchResult createTestInstance() throws Exception { QuerySearchResult result = new QuerySearchResult( new ShardSearchContextId(UUIDs.base64UUID(), randomLong()), new SearchShardTarget("node", shardId, null, OriginalIndices.NONE), - shardSearchRequest, - Settings.EMPTY + shardSearchRequest ); if (randomBoolean()) { result.terminatedEarly(randomBoolean());