Skip to content

Commit c2c723b

Browse files
committed
Return all "_attributes" as arrays. This ensures that documents with multiple matching attribute values pass all of the values to the "_attributes" field.
1 parent 114c2ca commit c2c723b

File tree

5 files changed

+40
-19
lines changed

5 files changed

+40
-19
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Once you have installed Elasticsearch, you can install zentity from a remote URL
3131

3232
Example:
3333

34-
`elasticsearch-plugin install https://zentity.io/releases/zentity-1.4.2-elasticsearch-7.3.2.zip`
34+
`elasticsearch-plugin install https://zentity.io/releases/zentity-1.5.0-beta1-elasticsearch-7.3.2.zip`
3535

3636
Read the [installation](https://zentity.io/docs/installation) docs for more details.
3737

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<zentity.author>Dave Moore</zentity.author>
1515
<zentity.classname>org.elasticsearch.plugin.zentity.ZentityPlugin</zentity.classname>
1616
<zentity.website>https://zentity.io</zentity.website>
17-
<zentity.version>1.4.2</zentity.version>
17+
<zentity.version>1.5.0-beta1</zentity.version>
1818
<!-- dependency versions -->
1919
<elasticsearch.version>7.3.2</elasticsearch.version>
2020
<jackson.core.version>2.9.9</jackson.core.version>

src/main/java/io/zentity/resolution/Job.java

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.util.Arrays;
4141
import java.util.Base64;
4242
import java.util.Collections;
43+
import java.util.Iterator;
4344
import java.util.List;
4445
import java.util.Map;
4546
import java.util.Set;
@@ -1086,32 +1087,41 @@ else if (!resolversClause.isEmpty())
10861087

10871088
// Gather attributes from the doc. Store them in the "_attributes" field of the doc,
10881089
// and include them in the attributes for subsequent queries.
1089-
TreeMap<String, JsonNode> docAttributes = new TreeMap<>();
1090+
TreeMap<String, TreeSet<Value>> docAttributes = new TreeMap<>();
1091+
TreeMap<String, JsonNode> docIndexFields = new TreeMap<>();
10901092
for (String indexFieldName : this.input.model().indices().get(indexName).fields().keySet()) {
10911093
String attributeName = this.input.model().indices().get(indexName).fields().get(indexFieldName).attribute();
10921094
if (this.input.model().attributes().get(attributeName) == null)
10931095
continue;
10941096
String attributeType = this.input.model().attributes().get(attributeName).type();
1097+
if (!docAttributes.containsKey(attributeName))
1098+
docAttributes.put(attributeName, new TreeSet<>());
10951099
if (!nextInputAttributes.containsKey(attributeName))
10961100
nextInputAttributes.put(attributeName, new Attribute(attributeName, attributeType));
10971101

1098-
// Get the attribute value from the doc.
1102+
// Get the attribute values from the doc.
10991103
if (doc.has("fields") && doc.get("fields").has(indexFieldName)) {
11001104

11011105
// Get the attribute value from the "fields" field if it exists there.
11021106
// This would include 'date' attribute types, for example.
11031107
JsonNode valueNode = doc.get("fields").get(indexFieldName);
1104-
if (valueNode.size() > 1) {
1105-
docAttributes.put(attributeName, valueNode); // Return multiple values (as an array) in "_attributes"
1106-
for (JsonNode vNode : valueNode) {
1108+
if (valueNode.isArray()) {
1109+
Iterator<JsonNode> valueNodeIterator = valueNode.elements();
1110+
while (valueNodeIterator.hasNext()) {
1111+
JsonNode vNode = valueNodeIterator.next();
11071112
Value value = Value.create(attributeType, vNode);
1113+
docAttributes.get(attributeName).add(value);
11081114
nextInputAttributes.get(attributeName).values().add(value);
11091115
}
1116+
if (valueNode.size() == 1)
1117+
docIndexFields.put(indexFieldName, valueNode.elements().next());
1118+
else
1119+
docIndexFields.put(indexFieldName, valueNode);
11101120
} else {
1111-
JsonNode vNode = valueNode.get(0); // Return single value (not as an array) in "_attributes"
1112-
docAttributes.put(attributeName, vNode);
1113-
Value value = Value.create(attributeType, vNode);
1121+
Value value = Value.create(attributeType, valueNode);
1122+
docAttributes.get(attributeName).add(value);
11141123
nextInputAttributes.get(attributeName).values().add(value);
1124+
docIndexFields.put(indexFieldName, valueNode);
11151125
}
11161126

11171127
} else {
@@ -1130,9 +1140,20 @@ else if (!resolversClause.isEmpty())
11301140
else
11311141
continue;
11321142
}
1133-
docAttributes.put(attributeName, valueNode);
1134-
Value value = Value.create(attributeType, valueNode);
1135-
nextInputAttributes.get(attributeName).values().add(value);
1143+
docIndexFields.put(indexFieldName, valueNode);
1144+
if (valueNode.isArray()) {
1145+
Iterator<JsonNode> valueNodeIterator = valueNode.elements();
1146+
while (valueNodeIterator.hasNext()) {
1147+
JsonNode vNode = valueNodeIterator.next();
1148+
Value value = Value.create(attributeType, vNode);
1149+
docAttributes.get(attributeName).add(value);
1150+
nextInputAttributes.get(attributeName).values().add(value);
1151+
}
1152+
} else {
1153+
Value value = Value.create(attributeType, valueNode);
1154+
docAttributes.get(attributeName).add(value);
1155+
nextInputAttributes.get(attributeName).values().add(value);
1156+
}
11361157
}
11371158
}
11381159

@@ -1146,8 +1167,9 @@ else if (!resolversClause.isEmpty())
11461167
if (this.includeAttributes) {
11471168
ObjectNode docAttributesObjNode = docObjNode.putObject("_attributes");
11481169
for (String attributeName : docAttributes.keySet()) {
1149-
JsonNode values = docAttributes.get(attributeName);
1150-
docAttributesObjNode.set(attributeName, values);
1170+
ArrayNode docAttributeArrNode = docAttributesObjNode.putArray(attributeName);
1171+
for (Value value : docAttributes.get(attributeName))
1172+
docAttributeArrNode.add(value.value());
11511173
}
11521174
}
11531175

@@ -1178,7 +1200,6 @@ else if (!resolversClause.isEmpty())
11781200
if (attributeType.equals("string") || attributeType.equals("date"))
11791201
attributeValueSerialized = "\"" + attributeValueSerialized + "\"";
11801202
JsonNode attributeValueNode = Json.MAPPER.readTree("{\"attribute_value\":" + attributeValueSerialized + "}").get("attribute_value");
1181-
JsonNode indexFieldValueNode = docAttributes.get(attributeName);
11821203
JsonNode matcherParamsNode;
11831204
if (input.attributes().containsKey(attributeName))
11841205
matcherParamsNode = Json.ORDERED_MAPPER.readTree(Json.ORDERED_MAPPER.writeValueAsString(input.attributes().get(attributeName).params()));
@@ -1189,7 +1210,7 @@ else if (input.model().matchers().containsKey(matcherName))
11891210
ObjectNode docExpDetailsObjNode = Json.ORDERED_MAPPER.createObjectNode();
11901211
docExpDetailsObjNode.put("attribute", attributeName);
11911212
docExpDetailsObjNode.put("target_field", indexFieldName);
1192-
docExpDetailsObjNode.put("target_value", indexFieldValueNode);
1213+
docExpDetailsObjNode.put("target_value", docIndexFields.get(indexFieldName));
11931214
docExpDetailsObjNode.put("input_value", attributeValueNode);
11941215
docExpDetailsObjNode.put("input_matcher", matcherName);
11951216
docExpDetailsObjNode.putPOJO("input_matcher_params", matcherParamsNode);

src/main/java/io/zentity/resolution/input/value/Value.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public Object type() {
5555
}
5656

5757
@Override
58-
public Object value() {
58+
public JsonNode value() {
5959
return this.value;
6060
}
6161

src/test/java/io/zentity/resolution/JobIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ private void destroyTestResources(int testResourceSet) throws IOException {
534534
destroyTestEntityModelElasticsearchError();
535535
break;
536536
case TEST_RESOURCES_ZENTITY_ERROR:
537-
destroyTestEntityModelElasticsearchError();
537+
destroyTestEntityModelZentityError();
538538
break;
539539
}
540540
}

0 commit comments

Comments
 (0)