@@ -434,23 +434,26 @@ private void putNLPResultToSourceMapForMapType(
434
434
if (sourceValue instanceof Map ) {
435
435
for (Map .Entry <String , Object > inputNestedMapEntry : ((Map <String , Object >) sourceValue ).entrySet ()) {
436
436
if (sourceAndMetadataMap .get (processorKey ) instanceof List ) {
437
- // build nlp output for list of nested objects
438
- Iterator <Object > inputNestedMapValueIt = ((List <Object >) inputNestedMapEntry .getValue ()).iterator ();
439
- for (Map <String , Object > nestedElement : (List <Map <String , Object >>) sourceAndMetadataMap .get (processorKey )) {
440
- // Only fill in when value is not null
441
- if (inputNestedMapValueIt .hasNext () && inputNestedMapValueIt .next () != null ) {
442
- nestedElement .put (inputNestedMapEntry .getKey (), results .get (indexWrapper .index ++));
443
- }
437
+ if (inputNestedMapEntry .getValue () instanceof List ) {
438
+ processMapEntryValue (
439
+ results ,
440
+ indexWrapper ,
441
+ (List <Map <String , Object >>) sourceAndMetadataMap .get (processorKey ),
442
+ inputNestedMapEntry .getKey (),
443
+ (List <Object >) inputNestedMapEntry .getValue ()
444
+ );
445
+ } else if (inputNestedMapEntry .getValue () instanceof Map ) {
446
+ processMapEntryValue (
447
+ results ,
448
+ indexWrapper ,
449
+ (List <Map <String , Object >>) sourceAndMetadataMap .get (processorKey ),
450
+ inputNestedMapEntry .getKey (),
451
+ inputNestedMapEntry .getValue ()
452
+ );
444
453
}
445
454
} else {
446
455
Pair <String , Object > processedNestedKey = processNestedKey (inputNestedMapEntry );
447
- Map <String , Object > sourceMap ;
448
- if (sourceAndMetadataMap .get (processorKey ) == null ) {
449
- sourceMap = new HashMap <>();
450
- sourceAndMetadataMap .put (processorKey , sourceMap );
451
- } else {
452
- sourceMap = (Map <String , Object >) sourceAndMetadataMap .get (processorKey );
453
- }
456
+ Map <String , Object > sourceMap = getSourceMapBySourceAndMetadataMap (processorKey , sourceAndMetadataMap );
454
457
putNLPResultToSourceMapForMapType (
455
458
processedNestedKey .getKey (),
456
459
processedNestedKey .getValue (),
@@ -471,6 +474,97 @@ private void putNLPResultToSourceMapForMapType(
471
474
}
472
475
}
473
476
477
+ private void processMapEntryValue (
478
+ List <?> results ,
479
+ IndexWrapper indexWrapper ,
480
+ List <Map <String , Object >> sourceAndMetadataMapValueInList ,
481
+ String inputNestedMapEntryKey ,
482
+ List <Object > inputNestedMapEntryValue
483
+ ) {
484
+ // build nlp output for object in sourceValue which is list type
485
+ Iterator <Object > inputNestedMapValueIt = inputNestedMapEntryValue .iterator ();
486
+ for (Map <String , Object > nestedElement : sourceAndMetadataMapValueInList ) {
487
+ // Only fill in when value is not null
488
+ if (inputNestedMapValueIt .hasNext () && inputNestedMapValueIt .next () != null ) {
489
+ nestedElement .put (inputNestedMapEntryKey , results .get (indexWrapper .index ++));
490
+ }
491
+ }
492
+ }
493
+
494
+ private void processMapEntryValue (
495
+ List <?> results ,
496
+ IndexWrapper indexWrapper ,
497
+ List <Map <String , Object >> sourceAndMetadataMapValueInList ,
498
+ String inputNestedMapEntryKey ,
499
+ Object inputNestedMapEntryValue
500
+ ) {
501
+ // build nlp output for object in sourceValue which is map type
502
+ Iterator <Map <String , Object >> iterator = sourceAndMetadataMapValueInList .iterator ();
503
+ IntStream .range (0 , sourceAndMetadataMapValueInList .size ()).forEach (index -> {
504
+ Map <String , Object > nestedElement = iterator .next ();
505
+ putNLPResultToSingleSourceMapInList (
506
+ inputNestedMapEntryKey ,
507
+ inputNestedMapEntryValue ,
508
+ results ,
509
+ indexWrapper ,
510
+ nestedElement ,
511
+ index
512
+ );
513
+ });
514
+ }
515
+
516
+ /**
517
+ * Put nlp result to single source element, which is in a list field of source document
518
+ * Such source element is in map type
519
+ *
520
+ * @param processorKey
521
+ * @param sourceValue
522
+ * @param results
523
+ * @param indexWrapper
524
+ * @param sourceAndMetadataMap
525
+ * @param nestedElementIndex index of the element in the list field of source document
526
+ */
527
+ @ SuppressWarnings ("unchecked" )
528
+ private void putNLPResultToSingleSourceMapInList (
529
+ String processorKey ,
530
+ Object sourceValue ,
531
+ List <?> results ,
532
+ IndexWrapper indexWrapper ,
533
+ Map <String , Object > sourceAndMetadataMap ,
534
+ int nestedElementIndex
535
+ ) {
536
+ if (processorKey == null || sourceAndMetadataMap == null || sourceValue == null ) return ;
537
+ if (sourceValue instanceof Map ) {
538
+ for (Map .Entry <String , Object > inputNestedMapEntry : ((Map <String , Object >) sourceValue ).entrySet ()) {
539
+ Pair <String , Object > processedNestedKey = processNestedKey (inputNestedMapEntry );
540
+ Map <String , Object > sourceMap = getSourceMapBySourceAndMetadataMap (processorKey , sourceAndMetadataMap );
541
+ putNLPResultToSingleSourceMapInList (
542
+ processedNestedKey .getKey (),
543
+ processedNestedKey .getValue (),
544
+ results ,
545
+ indexWrapper ,
546
+ sourceMap ,
547
+ nestedElementIndex
548
+ );
549
+ }
550
+ } else {
551
+ if (sourceValue instanceof List && ((List <Object >) sourceValue ).get (nestedElementIndex ) != null ) {
552
+ sourceAndMetadataMap .merge (processorKey , results .get (indexWrapper .index ++), REMAPPING_FUNCTION );
553
+ }
554
+ }
555
+ }
556
+
557
+ @ SuppressWarnings ("unchecked" )
558
+ private Map <String , Object > getSourceMapBySourceAndMetadataMap (String processorKey , Map <String , Object > sourceAndMetadataMap ) {
559
+ Map <String , Object > sourceMap = new HashMap <>();
560
+ if (sourceAndMetadataMap .get (processorKey ) == null ) {
561
+ sourceAndMetadataMap .put (processorKey , sourceMap );
562
+ } else {
563
+ sourceMap = (Map <String , Object >) sourceAndMetadataMap .get (processorKey );
564
+ }
565
+ return sourceMap ;
566
+ }
567
+
474
568
private List <Map <String , Object >> buildNLPResultForListType (List <String > sourceValue , List <?> results , IndexWrapper indexWrapper ) {
475
569
List <Map <String , Object >> keyToResult = new ArrayList <>();
476
570
IntStream .range (0 , sourceValue .size ())
0 commit comments