Skip to content

Commit

Permalink
Updated version and added another test for serialization
Browse files Browse the repository at this point in the history
Signed-off-by: Owais <owaiskazi19@gmail.com>
  • Loading branch information
owaiskazi19 committed Sep 22, 2024
1 parent a1bc82b commit a6a64ee
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -1239,6 +1239,7 @@ private SearchSourceBuilder shallowCopy(
rewrittenBuilder.pointInTimeBuilder = pointInTimeBuilder;
rewrittenBuilder.derivedFieldsObject = derivedFieldsObject;
rewrittenBuilder.derivedFields = derivedFields;
rewrittenBuilder.searchPipeline = searchPipeline;
return rewrittenBuilder;
}

Expand Down Expand Up @@ -1637,6 +1638,10 @@ public XContentBuilder innerToXContent(XContentBuilder builder, Params params) t

}

if (searchPipeline != null) {
builder.field(SEARCH_PIPELINE.getPreferredName(), searchPipeline);
}

return builder;
}

Expand Down Expand Up @@ -1914,7 +1919,8 @@ public int hashCode() {
trackTotalHitsUpTo,
pointInTimeBuilder,
derivedFieldsObject,
derivedFields
derivedFields,
searchPipeline
);
}

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit a6a64ee

Please sign in to comment.