43
43
import org .elasticsearch .search .aggregations .support .CoreValuesSourceType ;
44
44
import org .elasticsearch .search .lookup .FieldValues ;
45
45
import org .elasticsearch .search .lookup .SearchLookup ;
46
+ import org .elasticsearch .xcontent .XContentParser ;
46
47
47
48
import java .io .IOException ;
48
49
import java .net .InetAddress ;
51
52
import java .util .Arrays ;
52
53
import java .util .Collection ;
53
54
import java .util .Collections ;
55
+ import java .util .List ;
54
56
import java .util .Map ;
55
57
import java .util .Objects ;
58
+ import java .util .Set ;
56
59
import java .util .function .BiFunction ;
57
60
58
61
import static org .elasticsearch .index .mapper .FieldArrayContext .getOffsetsFieldName ;
@@ -213,7 +216,8 @@ public IpFieldMapper build(MapperBuilderContext context) {
213
216
parseNullValue (),
214
217
scriptValues (),
215
218
meta .getValue (),
216
- dimension .getValue ()
219
+ dimension .getValue (),
220
+ context .isSourceSynthetic ()
217
221
),
218
222
builderParams (this , context ),
219
223
context .isSourceSynthetic (),
@@ -234,6 +238,7 @@ public static final class IpFieldType extends SimpleMappedFieldType {
234
238
private final InetAddress nullValue ;
235
239
private final FieldValues <InetAddress > scriptValues ;
236
240
private final boolean isDimension ;
241
+ private final boolean isSyntheticSource ;
237
242
238
243
public IpFieldType (
239
244
String name ,
@@ -243,12 +248,14 @@ public IpFieldType(
243
248
InetAddress nullValue ,
244
249
FieldValues <InetAddress > scriptValues ,
245
250
Map <String , String > meta ,
246
- boolean isDimension
251
+ boolean isDimension ,
252
+ boolean isSyntheticSource
247
253
) {
248
254
super (name , indexed , stored , hasDocValues , TextSearchInfo .SIMPLE_MATCH_WITHOUT_TERMS , meta );
249
255
this .nullValue = nullValue ;
250
256
this .scriptValues = scriptValues ;
251
257
this .isDimension = isDimension ;
258
+ this .isSyntheticSource = isSyntheticSource ;
252
259
}
253
260
254
261
public IpFieldType (String name ) {
@@ -260,7 +267,7 @@ public IpFieldType(String name, boolean isIndexed) {
260
267
}
261
268
262
269
public IpFieldType (String name , boolean isIndexed , boolean hasDocValues ) {
263
- this (name , isIndexed , false , hasDocValues , null , null , Collections .emptyMap (), false );
270
+ this (name , isIndexed , false , hasDocValues , null , null , Collections .emptyMap (), false , false );
264
271
}
265
272
266
273
@ Override
@@ -452,10 +459,79 @@ public static Query rangeQuery(
452
459
453
460
@ Override
454
461
public BlockLoader blockLoader (BlockLoaderContext blContext ) {
455
- if (hasDocValues ()) {
462
+ if (hasDocValues () && ( blContext . fieldExtractPreference () != FieldExtractPreference . STORED || isSyntheticSource ) ) {
456
463
return new BlockDocValuesReader .BytesRefsFromOrdsBlockLoader (name ());
457
464
}
458
- return null ;
465
+
466
+ if (isStored ()) {
467
+ return new BlockStoredFieldsReader .BytesFromBytesRefsBlockLoader (name ());
468
+ }
469
+
470
+ if (isSyntheticSource ) {
471
+ return blockLoaderFromFallbackSyntheticSource (blContext );
472
+ }
473
+
474
+ // see #indexValue
475
+ BlockSourceReader .LeafIteratorLookup lookup = hasDocValues () == false && isIndexed ()
476
+ ? BlockSourceReader .lookupFromFieldNames (blContext .fieldNames (), name ())
477
+ : BlockSourceReader .lookupMatchingAll ();
478
+ return new BlockSourceReader .IpsBlockLoader (sourceValueFetcher (blContext .sourcePaths (name ())), lookup );
479
+ }
480
+
481
+ private BlockLoader blockLoaderFromFallbackSyntheticSource (BlockLoaderContext blContext ) {
482
+ var reader = new FallbackSyntheticSourceBlockLoader .SingleValueReader <InetAddress >(nullValue ) {
483
+ @ Override
484
+ public void convertValue (Object value , List <InetAddress > accumulator ) {
485
+ if (value instanceof InetAddress ia ) {
486
+ accumulator .add (ia );
487
+ }
488
+
489
+ try {
490
+ var address = InetAddresses .forString (value .toString ());
491
+ accumulator .add (address );
492
+ } catch (Exception e ) {
493
+ // Malformed value, skip it
494
+ }
495
+ }
496
+
497
+ @ Override
498
+ protected void parseNonNullValue (XContentParser parser , List <InetAddress > accumulator ) throws IOException {
499
+ // aligned with #parseCreateField()
500
+ String value = parser .text ();
501
+
502
+ try {
503
+ var address = InetAddresses .forString (value );
504
+ accumulator .add (address );
505
+ } catch (Exception e ) {
506
+ // Malformed value, skip it
507
+ }
508
+ }
509
+
510
+ @ Override
511
+ public void writeToBlock (List <InetAddress > values , BlockLoader .Builder blockBuilder ) {
512
+ var bytesRefBuilder = (BlockLoader .BytesRefBuilder ) blockBuilder ;
513
+
514
+ for (var value : values ) {
515
+ bytesRefBuilder .appendBytesRef (new BytesRef (InetAddressPoint .encode (value )));
516
+ }
517
+ }
518
+ };
519
+
520
+ return new FallbackSyntheticSourceBlockLoader (reader , name ()) {
521
+ @ Override
522
+ public Builder builder (BlockFactory factory , int expectedCount ) {
523
+ return factory .bytesRefs (expectedCount );
524
+ }
525
+ };
526
+ }
527
+
528
+ private SourceValueFetcher sourceValueFetcher (Set <String > sourcePaths ) {
529
+ return new SourceValueFetcher (sourcePaths , nullValue ) {
530
+ @ Override
531
+ public InetAddress parseSourceValue (Object value ) {
532
+ return parse (value );
533
+ }
534
+ };
459
535
}
460
536
461
537
@ Override
0 commit comments