4040import java .util .Arrays ;
4141import java .util .Base64 ;
4242import java .util .Collections ;
43+ import java .util .Iterator ;
4344import java .util .List ;
4445import java .util .Map ;
4546import 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 );
0 commit comments