@@ -345,79 +345,60 @@ String buildFieldDataString(
345
345
}
346
346
347
347
String getFieldType (String fieldName , Map <String , Object > propertiesAsMap , Index index ) {
348
+ // Attempt to get field type from cache
348
349
String fieldType = getFieldTypeFromCache (fieldName , index );
349
- if (fieldType != null ) {
350
- if (fieldType .equals (NO_FIELD_TYPE_VALUE )) {
351
- return null ;
352
- } else {
353
- return fieldType ;
354
- }
355
- }
356
350
357
- fieldType = getFieldTypeFromMapping (index , fieldName , propertiesAsMap );
358
351
if (fieldType != null ) {
359
- String finalFieldType = fieldType ;
360
- fieldTypeMap .computeIfAbsent (index , k -> new ConcurrentHashMap <>()).computeIfAbsent (fieldName , k -> finalFieldType );
361
352
return fieldType ;
362
353
}
363
- return null ;
364
- }
365
354
366
- String getFieldTypeFromMapping (Index index , String fieldName , Map <String , Object > propertiesAsMap ) {
367
- // Recursively find the field type from properties
368
- if (propertiesAsMap != null ) {
369
- String fieldType = findFieldTypeInProperties (propertiesAsMap , fieldName .split ("\\ ." ), 0 );
355
+ // Retrieve field type from mapping and cache it if found
356
+ fieldType = getFieldTypeFromProperties (fieldName , propertiesAsMap );
370
357
371
- // Cache the NO_FIELD_TYPE_VALUE result if no field type is found in this index
372
- if (fieldType == null ) {
373
- fieldTypeMap .computeIfAbsent (index , k -> new ConcurrentHashMap <>()).computeIfAbsent (fieldName , k -> fieldType );
374
- }
375
- return fieldType ;
376
- }
377
- return null ;
358
+ // Cache field type or NO_FIELD_TYPE_VALUE if not found
359
+ fieldTypeMap .computeIfAbsent (index , k -> new ConcurrentHashMap <>())
360
+ .putIfAbsent (fieldName , fieldType != null ? fieldType : NO_FIELD_TYPE_VALUE );
361
+
362
+ return fieldType ;
378
363
}
379
364
380
- private String findFieldTypeInProperties ( Map < String , ?> properties , String [] fieldParts , int index ) {
381
- if (index >= fieldParts . length ) {
365
+ String getFieldTypeFromProperties ( String fieldName , Map < String , Object > propertiesAsMap ) {
366
+ if (propertiesAsMap == null ) {
382
367
return null ;
383
368
}
384
369
385
- String currentField = fieldParts [ index ] ;
386
- Object currentMapping = properties . get ( currentField ) ;
370
+ String [] fieldParts = fieldName . split ( " \\ ." ) ;
371
+ Map < String , ?> currentProperties = propertiesAsMap ;
387
372
388
- // Check if current mapping is a map and contains further nested properties or a type
389
- if (currentMapping instanceof Map ) {
390
- Map <String , ?> currentMappingMap = (Map <String , ?>) currentMapping ;
373
+ for (int depth = 0 ; depth < fieldParts .length ; depth ++) {
374
+ Object currentMapping = currentProperties .get (fieldParts [depth ]);
391
375
392
- // If it has a 'properties' key, go into the nested object
393
- if (currentMappingMap .containsKey ("properties" )) {
394
- Map <String , ?> nestedProperties = (Map <String , ?>) currentMappingMap .get ("properties" );
395
- return findFieldTypeInProperties (nestedProperties , fieldParts , index + 1 );
396
- }
397
-
398
- // If it has a 'fields' key, handle multifields (e.g., title.raw) and is not the parent field (parent field case handled below)
399
- if (currentMappingMap .containsKey ("fields" ) && index + 1 < fieldParts .length ) {
400
- Map <String , ?> fieldsMap = (Map <String , ?>) currentMappingMap .get ("fields" );
401
- return findFieldTypeInProperties (fieldsMap , fieldParts , index + 1 ); // Recursively check subfield
402
- }
376
+ if (currentMapping instanceof Map ) {
377
+ Map <String , ?> currentMap = (Map <String , ?>) currentMapping ;
403
378
404
- // If it has a 'type' key, return the type. Also handles parent field case in multifields
405
- if (currentMappingMap .containsKey ("type" )) {
406
- return currentMappingMap .get ("type" ).toString ();
379
+ // Navigate into nested properties if available
380
+ if (currentMap .containsKey ("properties" )) {
381
+ currentProperties = (Map <String , ?>) currentMap .get ("properties" );
382
+ }
383
+ // Handle multifields (e.g., title.raw)
384
+ else if (currentMap .containsKey ("fields" ) && depth + 1 < fieldParts .length ) {
385
+ currentProperties = (Map <String , ?>) currentMap .get ("fields" );
386
+ }
387
+ // Return type if found
388
+ else if (currentMap .containsKey ("type" )) {
389
+ return (String ) currentMap .get ("type" );
390
+ } else {
391
+ return null ;
392
+ }
393
+ } else {
394
+ return null ;
407
395
}
408
396
}
409
397
410
398
return null ;
411
399
}
412
400
413
401
String getFieldTypeFromCache (String fieldName , Index index ) {
414
- Map <String , String > indexMap = fieldTypeMap .get (index );
415
- if (indexMap != null ) {
416
- String fieldType = indexMap .get (fieldName );
417
- if (fieldType != null ) {
418
- return fieldType ;
419
- }
420
- }
421
- return null ;
402
+ return fieldTypeMap .getOrDefault (index , new ConcurrentHashMap <>()).get (fieldName );
422
403
}
423
404
}
0 commit comments