Skip to content

Commit

Permalink
fix: support for non-string types in object fields
Browse files Browse the repository at this point in the history
  • Loading branch information
vignesh-hbk committed Sep 12, 2024
1 parent 118ff73 commit 343944b
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -282,33 +282,50 @@ public void setSearchableValue(
});
searchDocument.set(fieldName, dictDoc);
} else if (valueType == DataSchema.Type.MAP) {
ObjectNode dictDoc = JsonNodeFactory.instance.objectNode();
fieldValues
.subList(0, Math.min(fieldValues.size(), maxObjectKeys))
.forEach(
fieldValue -> {
ObjectNode dictDoc = JsonNodeFactory.instance.objectNode();
fieldValues
.subList(0, Math.min(fieldValues.size(), maxObjectKeys))
.forEach(
fieldValue -> {
String[] keyValues = fieldValue.toString().split("=");
String key = keyValues[0], value = "";
if (keyValues.length > 1) {
value = keyValues[1];
if (((MapDataSchema) fieldSpec.getPegasusSchema()).getValues().getType().equals(DataSchema.Type.BOOLEAN)) {
dictDoc.set(key, JsonNodeFactory.instance.booleanNode(Boolean.parseBoolean(value)));
} else if (((MapDataSchema) fieldSpec.getPegasusSchema()).getValues().getType().equals(DataSchema.Type.INT)) {
dictDoc.set(key, JsonNodeFactory.instance.numberNode(Integer.parseInt(value)));
} else if (((MapDataSchema) fieldSpec.getPegasusSchema()).getValues().getType().equals(DataSchema.Type.DOUBLE)) {
dictDoc.set(key, JsonNodeFactory.instance.numberNode(Double.parseDouble(value)));
} else if (((MapDataSchema) fieldSpec.getPegasusSchema()).getValues().getType().equals(DataSchema.Type.LONG)) {
dictDoc.set(key, JsonNodeFactory.instance.numberNode(Long.parseLong(value)));
} else if (((MapDataSchema) fieldSpec.getPegasusSchema()).getValues().getType().equals(DataSchema.Type.FLOAT)) {
dictDoc.set(key, JsonNodeFactory.instance.numberNode(Float.parseFloat(value)));
} else {
dictDoc.put(key, value);
}
} else {
value = keyValues[1];
if (((MapDataSchema) fieldSpec.getPegasusSchema())
.getValues()
.getType()
.equals(DataSchema.Type.BOOLEAN)) {
dictDoc.set(
key, JsonNodeFactory.instance.booleanNode(Boolean.parseBoolean(value)));
} else if (((MapDataSchema) fieldSpec.getPegasusSchema())
.getValues()
.getType()
.equals(DataSchema.Type.INT)) {
dictDoc.set(key, JsonNodeFactory.instance.numberNode(Integer.parseInt(value)));
} else if (((MapDataSchema) fieldSpec.getPegasusSchema())
.getValues()
.getType()
.equals(DataSchema.Type.DOUBLE)) {
dictDoc.set(
key, JsonNodeFactory.instance.numberNode(Double.parseDouble(value)));
} else if (((MapDataSchema) fieldSpec.getPegasusSchema())
.getValues()
.getType()
.equals(DataSchema.Type.LONG)) {
dictDoc.set(key, JsonNodeFactory.instance.numberNode(Long.parseLong(value)));
} else if (((MapDataSchema) fieldSpec.getPegasusSchema())
.getValues()
.getType()
.equals(DataSchema.Type.FLOAT)) {
dictDoc.set(key, JsonNodeFactory.instance.numberNode(Float.parseFloat(value)));
} else {
dictDoc.put(key, value);
}
} else {
dictDoc.put(key, value);
}
});
searchDocument.set(fieldName, dictDoc);
});
searchDocument.set(fieldName, dictDoc);
} else if (!fieldValues.isEmpty()) {
getNodeForValue(valueType, fieldValues.get(0), fieldType)
.ifPresent(node -> searchDocument.set(fieldName, node));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
import com.google.common.collect.ImmutableMap;
import com.linkedin.common.urn.TestEntityUrn;
import com.linkedin.common.urn.Urn;
import com.linkedin.data.template.StringArray;
import com.linkedin.data.template.StringMap;
import com.linkedin.data.template.BooleanMap;
import com.linkedin.data.template.DoubleMap;
import com.linkedin.data.template.FloatMap;
import com.linkedin.data.template.IntegerMap;
import com.linkedin.data.template.LongMap;
import com.linkedin.data.template.FloatMap;
import com.linkedin.data.template.DoubleMap;
import com.linkedin.data.template.StringArray;
import com.linkedin.data.template.StringMap;

public class TestEntityUtil {
private TestEntityUtil() {}
Expand Down Expand Up @@ -76,15 +76,12 @@ public static TestEntityInfo getTestEntityInfo(Urn urn) {
"0123456789")));
testEntityInfo.setDoubleField(100.456);
testEntityInfo.setEsObjectFieldBoolean(
new BooleanMap(ImmutableMap.of("key1", true, "key2", false)));
testEntityInfo.setEsObjectFieldLong(
new LongMap(ImmutableMap.of("key1", 1L, "key2", 2L)));
testEntityInfo.setEsObjectFieldFloat(
new FloatMap(ImmutableMap.of("key1", 1.0f, "key2", 2.0f)));
testEntityInfo.setEsObjectFieldDouble(
new DoubleMap(ImmutableMap.of("key1", 1.2, "key2", 2.4)));
new BooleanMap(ImmutableMap.of("key1", true, "key2", false)));
testEntityInfo.setEsObjectFieldLong(new LongMap(ImmutableMap.of("key1", 1L, "key2", 2L)));
testEntityInfo.setEsObjectFieldFloat(new FloatMap(ImmutableMap.of("key1", 1.0f, "key2", 2.0f)));
testEntityInfo.setEsObjectFieldDouble(new DoubleMap(ImmutableMap.of("key1", 1.2, "key2", 2.4)));
testEntityInfo.setEsObjectFieldInteger(
new IntegerMap(ImmutableMap.of("key1", 123, "key2", 456)));
new IntegerMap(ImmutableMap.of("key1", 123, "key2", 456)));
return testEntityInfo;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,36 @@ public void testTransform() throws IOException {
assertEquals(parsedJson.get("feature2").asInt(), 1);
JsonNode browsePathV2 = (JsonNode) parsedJson.get("browsePathV2");
assertEquals(browsePathV2.asText(), "␟levelOne␟levelTwo");
assertEquals(parsedJson.get("esObjectFieldBoolean").get("key1").getNodeType(), JsonNodeFactory.instance.booleanNode(true).getNodeType());
assertEquals(parsedJson.get("esObjectFieldLong").get("key1").getNodeType(), JsonNodeFactory.instance.numberNode(1L).getNodeType());
assertEquals(parsedJson.get("esObjectFieldFloat").get("key2").getNodeType(), JsonNodeFactory.instance.numberNode(2.0f).getNodeType());
assertEquals(parsedJson.get("esObjectFieldDouble").get("key1").getNodeType(), JsonNodeFactory.instance.numberNode(1.2).getNodeType());
assertEquals(parsedJson.get("esObjectFieldInteger").get("key2").getNodeType(), JsonNodeFactory.instance.numberNode(456).getNodeType());
assertEquals(parsedJson.get("esObjectFieldBoolean").get("key2").getNodeType(), JsonNodeFactory.instance.booleanNode(false).getNodeType());
assertEquals(parsedJson.get("esObjectFieldLong").get("key2").getNodeType(), JsonNodeFactory.instance.numberNode(2L).getNodeType());
assertEquals(parsedJson.get("esObjectFieldFloat").get("key1").getNodeType(), JsonNodeFactory.instance.numberNode(1.0f).getNodeType());
assertEquals(parsedJson.get("esObjectFieldDouble").get("key2").getNodeType(), JsonNodeFactory.instance.numberNode(2.4).getNodeType());
assertEquals(parsedJson.get("esObjectFieldInteger").get("key1").getNodeType(), JsonNodeFactory.instance.numberNode(123).getNodeType());
assertEquals(
parsedJson.get("esObjectFieldBoolean").get("key1").getNodeType(),
JsonNodeFactory.instance.booleanNode(true).getNodeType());
assertEquals(
parsedJson.get("esObjectFieldLong").get("key1").getNodeType(),
JsonNodeFactory.instance.numberNode(1L).getNodeType());
assertEquals(
parsedJson.get("esObjectFieldFloat").get("key2").getNodeType(),
JsonNodeFactory.instance.numberNode(2.0f).getNodeType());
assertEquals(
parsedJson.get("esObjectFieldDouble").get("key1").getNodeType(),
JsonNodeFactory.instance.numberNode(1.2).getNodeType());
assertEquals(
parsedJson.get("esObjectFieldInteger").get("key2").getNodeType(),
JsonNodeFactory.instance.numberNode(456).getNodeType());
assertEquals(
parsedJson.get("esObjectFieldBoolean").get("key2").getNodeType(),
JsonNodeFactory.instance.booleanNode(false).getNodeType());
assertEquals(
parsedJson.get("esObjectFieldLong").get("key2").getNodeType(),
JsonNodeFactory.instance.numberNode(2L).getNodeType());
assertEquals(
parsedJson.get("esObjectFieldFloat").get("key1").getNodeType(),
JsonNodeFactory.instance.numberNode(1.0f).getNodeType());
assertEquals(
parsedJson.get("esObjectFieldDouble").get("key2").getNodeType(),
JsonNodeFactory.instance.numberNode(2.4).getNodeType());
assertEquals(
parsedJson.get("esObjectFieldInteger").get("key1").getNodeType(),
JsonNodeFactory.instance.numberNode(123).getNodeType());
}

@Test
Expand Down

0 comments on commit 343944b

Please sign in to comment.