55import com .fasterxml .jackson .databind .ObjectMapper ;
66import com .fasterxml .jackson .databind .SerializationFeature ;
77import com .fasterxml .jackson .databind .node .ObjectNode ;
8+ import io .zentity .model .Attribute ;
89import io .zentity .model .Matcher ;
910import io .zentity .model .Model ;
11+ import io .zentity .model .ValidationException ;
1012import org .elasticsearch .action .search .SearchAction ;
1113import org .elasticsearch .action .search .SearchRequestBuilder ;
1214import org .elasticsearch .action .search .SearchResponse ;
@@ -247,7 +249,7 @@ private SearchResponse search(String indexName, String query) throws IOException
247249 *
248250 * @throws IOException
249251 */
250- private void traverse () throws IOException {
252+ private void traverse () throws IOException , ValidationException {
251253
252254 // Prepare to collect attributes from the results of these queries as the inputs to subsequent queries.
253255 HashMap <String , HashSet <Object >> nextInputAttributes = new HashMap <>();
@@ -376,35 +378,21 @@ private void traverse() throws IOException {
376378
377379 // Gather attributes from the doc. Store them in the "_attributes" field of the doc,
378380 // and include them in the attributes for subsequent queries.
379- TreeMap <String , Object > docAttributes = new TreeMap <>();
381+ TreeMap <String , JsonNode > docAttributes = new TreeMap <>();
380382 for (String indexFieldName : this .model .indices ().get (indexName ).fields ().keySet ()) {
381383 String attributeName = this .model .indices ().get (indexName ).fields ().get (indexFieldName ).attribute ();
384+ String attributeType = this .model .attributes ().get (attributeName ).type ();
382385 // The index field name might not refer to the _source property.
383386 // If it's not in the _source, remove the last part of the index field name from the dot notation.
384387 // Index field names can reference multi-fields, which are not returned in the _source.
388+ if (!nextInputAttributes .containsKey (attributeName ))
389+ nextInputAttributes .put (attributeName , new HashSet <>());
385390 if (!doc .get ("_source" ).has (indexFieldName ))
386391 indexFieldName = indexFieldName .split ("\\ ." )[0 ];
387392 if (doc .get ("_source" ).has (indexFieldName )) {
388- Object value ;
389- if (doc .get ("_source" ).get (indexFieldName ).isBoolean ())
390- value = doc .get ("_source" ).get (indexFieldName ).booleanValue ();
391- else if (doc .get ("_source" ).get (indexFieldName ).isDouble ())
392- value = doc .get ("_source" ).get (indexFieldName ).doubleValue ();
393- else if (doc .get ("_source" ).get (indexFieldName ).isFloat ())
394- value = doc .get ("_source" ).get (indexFieldName ).floatValue ();
395- else if (doc .get ("_source" ).get (indexFieldName ).isInt ())
396- value = doc .get ("_source" ).get (indexFieldName ).intValue ();
397- else if (doc .get ("_source" ).get (indexFieldName ).isLong ())
398- value = doc .get ("_source" ).get (indexFieldName ).longValue ();
399- else if (doc .get ("_source" ).get (indexFieldName ).isShort ())
400- value = doc .get ("_source" ).get (indexFieldName ).shortValue ();
401- else if (doc .get ("_source" ).get (indexFieldName ).isNull ())
402- value = "" ;
403- else
404- value = doc .get ("_source" ).get (indexFieldName ).asText ();
405- docAttributes .put (attributeName , value );
406- if (!nextInputAttributes .containsKey (attributeName ))
407- nextInputAttributes .put (attributeName , new HashSet <>());
393+ JsonNode valueNode = doc .get ("_source" ).get (indexFieldName );
394+ docAttributes .put (attributeName , valueNode );
395+ Object value = Attribute .convertType (attributeType , valueNode );
408396 nextInputAttributes .get (attributeName ).add (value );
409397 }
410398 }
@@ -418,24 +406,9 @@ else if (doc.get("_source").get(indexFieldName).isNull())
418406 docObjNode .remove ("_source" );
419407 if (this .includeAttributes ) {
420408 ObjectNode docAttributesObjNode = docObjNode .putObject ("_attributes" );
421- for (String attribute : docAttributes .keySet ()) {
422- Object value = docAttributes .get (attribute );
423- if (value .getClass () == Boolean .class )
424- docAttributesObjNode .put (attribute , (Boolean ) value );
425- else if (value .getClass () == Double .class )
426- docAttributesObjNode .put (attribute , (Double ) value );
427- else if (value .getClass () == Float .class )
428- docAttributesObjNode .put (attribute , (Float ) value );
429- else if (value .getClass () == Integer .class )
430- docAttributesObjNode .put (attribute , (Integer ) value );
431- else if (value .getClass () == Long .class )
432- docAttributesObjNode .put (attribute , (Long ) value );
433- else if (value .getClass () == Short .class )
434- docAttributesObjNode .put (attribute , (Short ) value );
435- else if (value .getClass () == null )
436- docAttributesObjNode .put (attribute , "" );
437- else
438- docAttributesObjNode .put (attribute , (String ) value );
409+ for (String attributeName : docAttributes .keySet ()) {
410+ JsonNode values = docAttributes .get (attributeName );
411+ docAttributesObjNode .set (attributeName , values );
439412 }
440413 }
441414
@@ -451,12 +424,12 @@ else if (value.getClass() == null)
451424 return ;
452425
453426 // Update input attributes for the next queries.
454- for (String attribute : nextInputAttributes .keySet ()) {
455- if (!this .inputAttributes .containsKey (attribute ))
456- this .inputAttributes .put (attribute , new HashSet <>());
457- for (Object value : nextInputAttributes .get (attribute )) {
458- if (!this .inputAttributes .get (attribute ).contains (value )) {
459- this .inputAttributes .get (attribute ).add (value );
427+ for (String attributeName : nextInputAttributes .keySet ()) {
428+ if (!this .inputAttributes .containsKey (attributeName ))
429+ this .inputAttributes .put (attributeName , new HashSet <>());
430+ for (Object value : nextInputAttributes .get (attributeName )) {
431+ if (!this .inputAttributes .get (attributeName ).contains (value )) {
432+ this .inputAttributes .get (attributeName ).add (value );
460433 newHits = true ;
461434 }
462435 }
@@ -477,7 +450,7 @@ else if (value.getClass() == null)
477450 * @return A JSON string to be returned as the body of the response to a client.
478451 * @throws IOException
479452 */
480- public String run () throws IOException {
453+ public String run () throws IOException , ValidationException {
481454 try {
482455
483456 // Reset the state of the job if reusing this Job object.
0 commit comments