Skip to content

Commit

Permalink
Add unit test for serialization of derived fields from query
Browse files Browse the repository at this point in the history
Signed-off-by: Rishabh Maurya <[email protected]>
  • Loading branch information
rishabhmaurya committed Apr 10, 2024
1 parent ef7e859 commit 81ee8e9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1570,14 +1570,15 @@ public XContentBuilder innerToXContent(XContentBuilder builder, Params params) t
if (derivedFieldsObject != null || derivedFields != null) {
builder.startObject(DERIVED_FIELDS_FIELD.getPreferredName());
if (derivedFieldsObject != null) {
builder.map(derivedFieldsObject);
builder.mapContents(derivedFieldsObject);
}
if (derivedFields != null) {
for (DerivedField derivedField : derivedFields) {
derivedField.toXContent(builder, params);
}
}
builder.endObject();

}

return builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.opensearch.index.query.QueryRewriteContext;
import org.opensearch.index.query.RandomQueryBuilder;
import org.opensearch.index.query.Rewriteable;
import org.opensearch.script.Script;
import org.opensearch.search.AbstractSearchTestCase;
import org.opensearch.search.rescore.QueryRescorerBuilder;
import org.opensearch.search.sort.FieldSortBuilder;
Expand Down Expand Up @@ -311,7 +312,7 @@ public void testParseSort() throws IOException {
}
}

public void testDerivedFieldsParsing() throws IOException {
public void testDerivedFieldsParsingAndSerialization() throws IOException {
{
String restContent = "{\n"
+ " \"derived\": {\n"
Expand All @@ -328,10 +329,29 @@ public void testDerivedFieldsParsing() throws IOException {
+ " \"match\": { \"content\": { \"query\": \"foo bar\" }}\n"
+ " }\n"
+ "}";

String expectedContent =
"{\"query\":{\"match\":{\"content\":{\"query\":\"foo bar\",\"operator\":\"OR\",\"prefix_length\":0,\"max_expansions\":50,\"fuzzy_transpositions\":true,\"lenient\":false,\"zero_terms_query\":\"NONE\",\"auto_generate_synonyms_phrase_query\":true,\"boost\":1.0}}},"
+ "\"derived\":{"
+ "\"duration\":{\"type\":\"long\",\"script\":\"emit(doc['test'])\"},\"ip_from_message\":{\"type\":\"keyword\",\"script\":\"emit(doc['message'])\"},\"derived_field\":{\"type\":\"keyword\",\"script\":{\"source\":\"emit(doc['message']\",\"lang\":\"painless\"}}}}";

try (XContentParser parser = createParser(JsonXContent.jsonXContent, restContent)) {
SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.fromXContent(parser);
searchSourceBuilder.derivedField("derived_field", "keyword", new Script("emit(doc['message']"));
searchSourceBuilder = rewrite(searchSourceBuilder);
assertEquals(2, searchSourceBuilder.getDerivedFieldsObject().size());
assertEquals(1, searchSourceBuilder.getDerivedFields().size());

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);
}
}
}
}

Expand Down

0 comments on commit 81ee8e9

Please sign in to comment.