diff --git a/server/src/main/java/org/opensearch/search/builder/SearchSourceBuilder.java b/server/src/main/java/org/opensearch/search/builder/SearchSourceBuilder.java index 73cfca3ec912f..dd4e4d073cb1b 100644 --- a/server/src/main/java/org/opensearch/search/builder/SearchSourceBuilder.java +++ b/server/src/main/java/org/opensearch/search/builder/SearchSourceBuilder.java @@ -299,7 +299,7 @@ public SearchSourceBuilder(StreamInput in) throws IOException { derivedFields = in.readList(DerivedField::new); } } - if (in.getVersion().onOrAfter(Version.V_2_18_0)) { + if (in.getVersion().onOrAfter(Version.V_3_0_0)) { searchPipeline = in.readOptionalString(); } } @@ -382,7 +382,7 @@ public void writeTo(StreamOutput out) throws IOException { out.writeList(derivedFields); } } - if (out.getVersion().onOrAfter(Version.V_2_18_0)) { + if (out.getVersion().onOrAfter(Version.V_3_0_0)) { out.writeOptionalString(searchPipeline); } } @@ -1239,6 +1239,7 @@ private SearchSourceBuilder shallowCopy( rewrittenBuilder.pointInTimeBuilder = pointInTimeBuilder; rewrittenBuilder.derivedFieldsObject = derivedFieldsObject; rewrittenBuilder.derivedFields = derivedFields; + rewrittenBuilder.searchPipeline = searchPipeline; return rewrittenBuilder; } @@ -1637,6 +1638,10 @@ public XContentBuilder innerToXContent(XContentBuilder builder, Params params) t } + if (searchPipeline != null) { + builder.field(SEARCH_PIPELINE.getPreferredName(), searchPipeline); + } + return builder; } @@ -1914,7 +1919,8 @@ public int hashCode() { trackTotalHitsUpTo, pointInTimeBuilder, derivedFieldsObject, - derivedFields + derivedFields, + searchPipeline ); } @@ -1959,7 +1965,8 @@ public boolean equals(Object obj) { && Objects.equals(trackTotalHitsUpTo, other.trackTotalHitsUpTo) && Objects.equals(pointInTimeBuilder, other.pointInTimeBuilder) && Objects.equals(derivedFieldsObject, other.derivedFieldsObject) - && Objects.equals(derivedFields, other.derivedFields); + && Objects.equals(derivedFields, other.derivedFields) + && Objects.equals(searchPipeline, other.searchPipeline); } @Override diff --git a/server/src/test/java/org/opensearch/action/search/SearchRequestTests.java b/server/src/test/java/org/opensearch/action/search/SearchRequestTests.java index a916ea522ca30..acda1445bacbb 100644 --- a/server/src/test/java/org/opensearch/action/search/SearchRequestTests.java +++ b/server/src/test/java/org/opensearch/action/search/SearchRequestTests.java @@ -56,7 +56,6 @@ import java.io.IOException; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.function.IntConsumer; @@ -249,44 +248,6 @@ public void testCopyConstructor() throws IOException { assertNotSame(deserializedRequest, searchRequest); } - public void testParseSearchRequest() throws IOException { - RestRequest restRequest = new FakeRestRequest(); - SearchRequest searchRequest = createSearchRequest(); - IntConsumer setSize = mock(IntConsumer.class); - - restRequest.params().put("index", "index1,index2"); - restRequest.params().put("batched_reduce_size", "512"); - restRequest.params().put("pre_filter_shard_size", "128"); - restRequest.params().put("max_concurrent_shard_requests", "10"); - restRequest.params().put("allow_partial_search_results", "true"); - restRequest.params().put("phase_took", "false"); - restRequest.params().put("search_type", "dfs_query_then_fetch"); - restRequest.params().put("request_cache", "true"); - restRequest.params().put("scroll", "1m"); - restRequest.params().put("routing", "routing_value"); - restRequest.params().put("preference", "preference_value"); - restRequest.params().put("search_pipeline", "pipeline_value"); - restRequest.params().put("ccs_minimize_roundtrips", "true"); - restRequest.params().put("cancel_after_time_interval", "5s"); - - RestSearchAction.parseSearchRequest(searchRequest, restRequest, null, namedWriteableRegistry, setSize); - - assertEquals(Arrays.asList("index1", "index2"), Arrays.asList(searchRequest.indices())); - assertEquals(512, searchRequest.getBatchedReduceSize()); - assertEquals(Integer.valueOf(128), searchRequest.getPreFilterShardSize()); - assertEquals(10, searchRequest.getMaxConcurrentShardRequests()); - assertTrue(searchRequest.allowPartialSearchResults()); - assertFalse(searchRequest.isPhaseTook()); - assertEquals(DFS_QUERY_THEN_FETCH, searchRequest.searchType()); - assertEquals(true, searchRequest.requestCache()); - assertEquals(TimeValue.timeValueMinutes(1), searchRequest.scroll().keepAlive()); - assertEquals("routing_value", searchRequest.routing()); - assertEquals("preference_value", searchRequest.preference()); - assertEquals("pipeline_value", searchRequest.pipeline()); - assertTrue(searchRequest.isCcsMinimizeRoundtrips()); - assertEquals(TimeValue.timeValueSeconds(5), searchRequest.getCancelAfterTimeInterval()); - } - public void testParseSearchRequestWithUnsupportedSearchType() throws IOException { RestRequest restRequest = new FakeRestRequest(); SearchRequest searchRequest = createSearchRequest(); diff --git a/server/src/test/java/org/opensearch/search/builder/SearchSourceBuilderTests.java b/server/src/test/java/org/opensearch/search/builder/SearchSourceBuilderTests.java index 9697f4cee0d58..da8ccc9e121e0 100644 --- a/server/src/test/java/org/opensearch/search/builder/SearchSourceBuilderTests.java +++ b/server/src/test/java/org/opensearch/search/builder/SearchSourceBuilderTests.java @@ -421,6 +421,27 @@ public void testDerivedFieldsParsingAndSerializationObjectType() throws IOExcept } } + public void testSearchPipelineParsingAndSerialization() throws IOException { + String restContent = "{ \"query\": { \"match_all\": {} }, \"from\": 0, \"size\": 10, \"search_pipeline\": \"my_pipeline\" }"; + String expectedContent = "{\"from\":0,\"size\":10,\"query\":{\"match_all\":{\"boost\":1.0}},\"search_pipeline\":\"my_pipeline\"}"; + + try (XContentParser parser = createParser(JsonXContent.jsonXContent, restContent)) { + SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.fromXContent(parser); + searchSourceBuilder = rewrite(searchSourceBuilder); + + try (BytesStreamOutput output = new BytesStreamOutput()) { + searchSourceBuilder.writeTo(output); + try (StreamInput in = new NamedWriteableAwareStreamInput(output.bytes().streamInput(), namedWriteableRegistry)) { + SearchSourceBuilder deserializedBuilder = new SearchSourceBuilder(in); + String actualContent = deserializedBuilder.toString(); + assertEquals(expectedContent, actualContent); + assertEquals(searchSourceBuilder.hashCode(), deserializedBuilder.hashCode()); + assertNotSame(searchSourceBuilder, deserializedBuilder); + } + } + } + } + public void testAggsParsing() throws IOException { { String restContent = "{\n"