22
22
import java .util .ArrayList ;
23
23
import java .util .Collection ;
24
24
import java .util .Collections ;
25
+ import java .util .HashMap ;
25
26
import java .util .HashSet ;
26
- import java .util .LinkedHashMap ;
27
27
import java .util .List ;
28
28
import java .util .Map ;
29
29
import java .util .Set ;
@@ -84,9 +84,9 @@ protected void addDoc(LuceneDocument doc) {
84
84
private final MappingParserContext mappingParserContext ;
85
85
private final SourceToParse sourceToParse ;
86
86
private final Set <String > ignoredFields ;
87
- private final Map <String , List <Mapper . Builder >> dynamicMappers ;
87
+ private final Map <String , List <Mapper >> dynamicMappers ;
88
88
private final Set <String > newFieldsSeen ;
89
- private final Map <String , ObjectMapper . Builder > dynamicObjectMappers ;
89
+ private final Map <String , ObjectMapper > dynamicObjectMappers ;
90
90
private final List <RuntimeField > dynamicRuntimeFields ;
91
91
private final DocumentDimensions dimensions ;
92
92
private final ObjectMapper parent ;
@@ -102,9 +102,9 @@ private DocumentParserContext(
102
102
MappingParserContext mappingParserContext ,
103
103
SourceToParse sourceToParse ,
104
104
Set <String > ignoreFields ,
105
- Map <String , List <Mapper . Builder >> dynamicMappers ,
105
+ Map <String , List <Mapper >> dynamicMappers ,
106
106
Set <String > newFieldsSeen ,
107
- Map <String , ObjectMapper . Builder > dynamicObjectMappers ,
107
+ Map <String , ObjectMapper > dynamicObjectMappers ,
108
108
List <RuntimeField > dynamicRuntimeFields ,
109
109
String id ,
110
110
Field version ,
@@ -166,9 +166,9 @@ protected DocumentParserContext(
166
166
mappingParserContext ,
167
167
source ,
168
168
new HashSet <>(),
169
- new LinkedHashMap <>(),
169
+ new HashMap <>(),
170
170
new HashSet <>(),
171
- new LinkedHashMap <>(),
171
+ new HashMap <>(),
172
172
new ArrayList <>(),
173
173
null ,
174
174
null ,
@@ -304,29 +304,29 @@ public boolean isCopyToField(String name) {
304
304
/**
305
305
* Add a new mapper dynamically created while parsing.
306
306
*/
307
- public final void addDynamicMapper (String fullName , Mapper . Builder builder ) {
307
+ public final void addDynamicMapper (Mapper mapper ) {
308
308
// eagerly check object depth limit here to avoid stack overflow errors
309
- if (builder instanceof ObjectMapper . Builder ) {
310
- MappingLookup .checkObjectDepthLimit (indexSettings ().getMappingDepthLimit (), fullName );
309
+ if (mapper instanceof ObjectMapper ) {
310
+ MappingLookup .checkObjectDepthLimit (indexSettings ().getMappingDepthLimit (), mapper . name () );
311
311
}
312
312
313
313
// eagerly check field name limit here to avoid OOM errors
314
314
// only check fields that are not already mapped or tracked in order to avoid hitting field limit too early via double-counting
315
315
// note that existing fields can also receive dynamic mapping updates (e.g. constant_keyword to fix the value)
316
- if (mappingLookup .getMapper (fullName ) == null
317
- && mappingLookup .objectMappers ().containsKey (fullName ) == false
318
- && newFieldsSeen .add (fullName )) {
316
+ if (mappingLookup .getMapper (mapper . name () ) == null
317
+ && mappingLookup .objectMappers ().containsKey (mapper . name () ) == false
318
+ && newFieldsSeen .add (mapper . name () )) {
319
319
mappingLookup .checkFieldLimit (indexSettings ().getMappingTotalFieldsLimit (), newFieldsSeen .size ());
320
320
}
321
- if (builder instanceof ObjectMapper . Builder objectMapper ) {
322
- dynamicObjectMappers .put (fullName , objectMapper );
321
+ if (mapper instanceof ObjectMapper objectMapper ) {
322
+ dynamicObjectMappers .put (objectMapper . name () , objectMapper );
323
323
// dynamic object mappers may have been obtained from applying a dynamic template, in which case their definition may contain
324
324
// sub-fields as well as sub-objects that need to be added to the mappings
325
- for (Mapper . Builder submapper : objectMapper .subBuilders ()) {
325
+ for (Mapper submapper : objectMapper .mappers . values ()) {
326
326
// we could potentially skip the step of adding these to the dynamic mappers, because their parent is already added to
327
327
// that list, and what is important is that all of the intermediate objects are added to the dynamic object mappers so that
328
328
// they can be looked up once sub-fields need to be added to them. For simplicity, we treat these like any other object
329
- addDynamicMapper (fullName + "." + submapper . name , submapper );
329
+ addDynamicMapper (submapper );
330
330
}
331
331
}
332
332
@@ -336,7 +336,7 @@ public final void addDynamicMapper(String fullName, Mapper.Builder builder) {
336
336
// dynamically mapped objects when the incoming document defines no sub-fields in them:
337
337
// 1) by default, they would be empty containers in the mappings, is it then important to map them?
338
338
// 2) they can be the result of applying a dynamic template which may define sub-fields or set dynamic, enabled or subobjects.
339
- dynamicMappers .computeIfAbsent (fullName , k -> new ArrayList <>()).add (builder );
339
+ dynamicMappers .computeIfAbsent (mapper . name () , k -> new ArrayList <>()).add (mapper );
340
340
}
341
341
342
342
/**
@@ -345,8 +345,8 @@ public final void addDynamicMapper(String fullName, Mapper.Builder builder) {
345
345
* Consists of a all {@link Mapper}s that will need to be added to their respective parent {@link ObjectMapper}s in order
346
346
* to become part of the resulting dynamic mapping update.
347
347
*/
348
- public final Map < String , List <Mapper . Builder > > getDynamicMappers () {
349
- return dynamicMappers ;
348
+ public final List <Mapper > getDynamicMappers () {
349
+ return dynamicMappers . values (). stream (). flatMap ( List :: stream ). toList () ;
350
350
}
351
351
352
352
/**
@@ -355,13 +355,13 @@ public final Map<String, List<Mapper.Builder>> getDynamicMappers() {
355
355
* @param fieldName Full field name with dot-notation.
356
356
* @return List of Mappers or null
357
357
*/
358
- public final List <Mapper . Builder > getDynamicMappers (String fieldName ) {
358
+ public final List <Mapper > getDynamicMappers (String fieldName ) {
359
359
return dynamicMappers .get (fieldName );
360
360
}
361
361
362
- public void updateDynamicMappers (String name , Mapper . Builder mapper ) {
362
+ public void updateDynamicMappers (String name , List < Mapper > mappers ) {
363
363
dynamicMappers .remove (name );
364
- dynamicMappers . put ( name , List . of ( mapper ) );
364
+ mappers . forEach ( this :: addDynamicMapper );
365
365
}
366
366
367
367
/**
@@ -371,7 +371,7 @@ public void updateDynamicMappers(String name, Mapper.Builder mapper) {
371
371
* Holds a flat set of object mappers, meaning that an object field named <code>foo.bar</code> can be looked up directly with its
372
372
* dotted name.
373
373
*/
374
- final ObjectMapper . Builder getDynamicObjectMapper (String name ) {
374
+ final ObjectMapper getDynamicObjectMapper (String name ) {
375
375
return dynamicObjectMappers .get (name );
376
376
}
377
377
0 commit comments