Skip to content

Commit

Permalink
add serialization/deserialization test for DerivedField class
Browse files Browse the repository at this point in the history
Signed-off-by: Rishabh Maurya <[email protected]>
  • Loading branch information
rishabhmaurya committed May 15, 2024
1 parent 18a2d61 commit cfd32a9
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.index.mapper.DerivedField;
import org.opensearch.index.mapper.DerivedFieldMapper;
import org.opensearch.index.mapper.DerivedFieldSupportedTypes;
import org.opensearch.index.query.QueryBuilder;
import org.opensearch.index.query.QueryRewriteContext;
import org.opensearch.index.query.Rewriteable;
Expand Down Expand Up @@ -1004,6 +1005,40 @@ public SearchSourceBuilder derivedField(String name, String type, Script script)
return this;
}

/**
* Adds a derived field with the given name with provided type, script and other parameters
* @param name name of the derived field
* @param type type of the derived field
* @param script script associated with derived field
* @param properties map of field name and type of field for nested fields within object derived field
* @param sourceIndexedField source text field which is indexed to filter documents for better performance
* @param format date format
* @param ignoreMalformed ignores malformed fields instead of failing search request
*/
public SearchSourceBuilder derivedField(
String name,
String type,
Script script,
Map<String, String> properties,
String sourceIndexedField,
String format,
Boolean ignoreMalformed
) {
if (derivedFields == null) {
derivedFields = new ArrayList<>();
}
DerivedField derivedField = new DerivedField(name, type, script);
if (properties != null && !type.equals(DerivedFieldSupportedTypes.OBJECT.getName())) {
throw new IllegalArgumentException("[properties] parameter is only valid when type is [object]");
}
derivedField.setProperties(properties);
derivedField.setSourceIndexedField(sourceIndexedField);
derivedField.setFormat(format);
derivedField.setIgnoreMalformed(ignoreMalformed);
derivedFields.add(derivedField);
return this;
}

/**
* Sets the boost a specific index or alias will receive when the query is executed
* against it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,70 @@ public void testDerivedFieldsParsingAndSerialization() throws IOException {

}

public void testDerivedFieldsParsingAndSerializationObjectType() throws IOException {
{
String restContent = "{\n"
+ " \"derived\": {\n"
+ " \"duration\": {\n"
+ " \"type\": \"long\",\n"
+ " \"script\": \"emit(doc['test'])\"\n"
+ " },\n"
+ " \"ip_from_message\": {\n"
+ " \"type\": \"keyword\",\n"
+ " \"script\": \"emit(doc['message'])\"\n"
+ " },\n"
+ " \"object\": {\n"
+ " \"type\": \"object\",\n"
+ " \"script\": \"emit(doc['test'])\",\n"
+ " \"format\": \"dd-MM-yyyy\",\n"
+ " \"source_indexed_field\": \"test\",\n"
+ " \"ignore_malformed\": true,\n"
+ " \"properties\": {\n"
+ " \"sub_field\": \"text\"\n"
+ " }\n"
+ " }\n"
+ " },\n"
+ " \"query\" : {\n"
+ " \"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'])\"},\"object\":{\"format\":\"dd-MM-yyyy\",\"source_indexed_field\":\"test\",\"ignore_malformed\":true,\"type\":\"object\",\"script\":\"emit(doc['test'])\",\"properties\":{\"sub_field\":\"text\"}},\"derived_field\":{\"type\":\"object\",\"script\":{\"source\":\"emit(doc['message']\",\"lang\":\"painless\"},\"properties\":{\"sub_field_2\":\"keyword\"},\"source_indexed_field\":\"message\",\"format\":\"dd-MM-yyyy\",\"ignore_malformed\":true}}}";

try (XContentParser parser = createParser(JsonXContent.jsonXContent, restContent)) {
SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.fromXContent(parser);
searchSourceBuilder.derivedField(
"derived_field",
"object",
new Script("emit(doc['message']"),
Map.of("sub_field_2", "keyword"),
"message",
"dd-MM-yyyy",
true
);
searchSourceBuilder = rewrite(searchSourceBuilder);
assertEquals(3, searchSourceBuilder.getDerivedFieldsObject().size());
assertEquals(1, searchSourceBuilder.getDerivedFields().size());
assertEquals(1, searchSourceBuilder.getDerivedFields().get(0).getProperties().size());
assertEquals("message", searchSourceBuilder.getDerivedFields().get(0).getSourceIndexedField());
assertEquals("dd-MM-yyyy", searchSourceBuilder.getDerivedFields().get(0).getFormat());
assertTrue(searchSourceBuilder.getDerivedFields().get(0).getIgnoreMalformed());

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 cfd32a9

Please sign in to comment.