5555import java .util .Objects ;
5656import java .util .function .BiFunction ;
5757
58+ import static org .elasticsearch .index .mapper .FieldArrayContext .getOffsetsFieldName ;
5859import static org .elasticsearch .index .mapper .IpPrefixAutomatonUtil .buildIpPrefixAutomaton ;
5960
6061/**
@@ -92,8 +93,15 @@ public static final class Builder extends FieldMapper.DimensionBuilder {
9293 private final boolean ignoreMalformedByDefault ;
9394 private final IndexVersion indexCreatedVersion ;
9495 private final ScriptCompiler scriptCompiler ;
96+ private final SourceKeepMode indexSourceKeepMode ;
9597
96- public Builder (String name , ScriptCompiler scriptCompiler , boolean ignoreMalformedByDefault , IndexVersion indexCreatedVersion ) {
98+ public Builder (
99+ String name ,
100+ ScriptCompiler scriptCompiler ,
101+ boolean ignoreMalformedByDefault ,
102+ IndexVersion indexCreatedVersion ,
103+ SourceKeepMode indexSourceKeepMode
104+ ) {
97105 super (name );
98106 this .scriptCompiler = Objects .requireNonNull (scriptCompiler );
99107 this .ignoreMalformedByDefault = ignoreMalformedByDefault ;
@@ -114,6 +122,7 @@ public Builder(String name, ScriptCompiler scriptCompiler, boolean ignoreMalform
114122 );
115123 }
116124 });
125+ this .indexSourceKeepMode = indexSourceKeepMode ;
117126 }
118127
119128 Builder nullValue (String nullValue ) {
@@ -184,6 +193,15 @@ public IpFieldMapper build(MapperBuilderContext context) {
184193 }
185194 hasScript = script .get () != null ;
186195 onScriptError = onScriptErrorParam .getValue ();
196+
197+ String offsetsFieldName = getOffsetsFieldName (
198+ context ,
199+ indexSourceKeepMode ,
200+ hasDocValues .getValue (),
201+ stored .getValue (),
202+ this ,
203+ indexCreatedVersion
204+ );
187205 return new IpFieldMapper (
188206 leafName (),
189207 new IpFieldType (
@@ -198,7 +216,8 @@ public IpFieldMapper build(MapperBuilderContext context) {
198216 ),
199217 builderParams (this , context ),
200218 context .isSourceSynthetic (),
201- this
219+ this ,
220+ offsetsFieldName
202221 );
203222 }
204223
@@ -208,7 +227,7 @@ public IpFieldMapper build(MapperBuilderContext context) {
208227
209228 public static final TypeParser PARSER = new TypeParser ((n , c ) -> {
210229 boolean ignoreMalformedByDefault = IGNORE_MALFORMED_SETTING .get (c .getSettings ());
211- return new Builder (n , c .scriptCompiler (), ignoreMalformedByDefault , c .indexVersionCreated ());
230+ return new Builder (n , c .scriptCompiler (), ignoreMalformedByDefault , c .indexVersionCreated (), c . getIndexSettings (). sourceKeepMode () );
212231 }, MINIMUM_COMPATIBILITY_VERSION );
213232
214233 public static final class IpFieldType extends SimpleMappedFieldType {
@@ -503,13 +522,16 @@ public TermsEnum getTerms(IndexReader reader, String prefix, boolean caseInsensi
503522 private final Script script ;
504523 private final FieldValues <InetAddress > scriptValues ;
505524 private final ScriptCompiler scriptCompiler ;
525+ private final SourceKeepMode indexSourceKeepMode ;
526+ private final String offsetsFieldName ;
506527
507528 private IpFieldMapper (
508529 String simpleName ,
509530 MappedFieldType mappedFieldType ,
510531 BuilderParams builderParams ,
511532 boolean storeIgnored ,
512- Builder builder
533+ Builder builder ,
534+ String offsetsFieldName
513535 ) {
514536 super (simpleName , mappedFieldType , builderParams );
515537 this .ignoreMalformedByDefault = builder .ignoreMalformedByDefault ;
@@ -525,6 +547,8 @@ private IpFieldMapper(
525547 this .scriptCompiler = builder .scriptCompiler ;
526548 this .dimension = builder .dimension .getValue ();
527549 this .storeIgnored = storeIgnored ;
550+ this .indexSourceKeepMode = builder .indexSourceKeepMode ;
551+ this .offsetsFieldName = offsetsFieldName ;
528552 }
529553
530554 @ Override
@@ -563,6 +587,14 @@ protected void parseCreateField(DocumentParserContext context) throws IOExceptio
563587 if (address != null ) {
564588 indexValue (context , address );
565589 }
590+ if (offsetsFieldName != null && context .isImmediateParentAnArray () && context .canAddIgnoredField ()) {
591+ if (address != null ) {
592+ BytesRef sortableValue = new BytesRef (InetAddressPoint .encode (address ));
593+ context .getOffSetContext ().recordOffset (offsetsFieldName , sortableValue );
594+ } else {
595+ context .getOffSetContext ().recordNull (offsetsFieldName );
596+ }
597+ }
566598 }
567599
568600 private void indexValue (DocumentParserContext context , InetAddress address ) {
@@ -595,7 +627,9 @@ protected void indexScriptValues(
595627
596628 @ Override
597629 public FieldMapper .Builder getMergeBuilder () {
598- return new Builder (leafName (), scriptCompiler , ignoreMalformedByDefault , indexCreatedVersion ).dimension (dimension ).init (this );
630+ return new Builder (leafName (), scriptCompiler , ignoreMalformedByDefault , indexCreatedVersion , indexSourceKeepMode ).dimension (
631+ dimension
632+ ).init (this );
599633 }
600634
601635 @ Override
@@ -612,19 +646,24 @@ protected SyntheticSourceSupport syntheticSourceSupport() {
612646 if (hasDocValues ) {
613647 return new SyntheticSourceSupport .Native (() -> {
614648 var layers = new ArrayList <CompositeSyntheticFieldLoader .Layer >();
615- layers .add (new SortedSetDocValuesSyntheticFieldLoaderLayer (fullPath ()) {
616- @ Override
617- protected BytesRef convert (BytesRef value ) {
618- byte [] bytes = Arrays .copyOfRange (value .bytes , value .offset , value .offset + value .length );
619- return new BytesRef (NetworkAddress .format (InetAddressPoint .decode (bytes )));
620- }
621-
622- @ Override
623- protected BytesRef preserve (BytesRef value ) {
624- // No need to copy because convert has made a deep copy
625- return value ;
626- }
627- });
649+ if (offsetsFieldName != null ) {
650+ layers .add (
651+ new SortedSetWithOffsetsDocValuesSyntheticFieldLoaderLayer (fullPath (), offsetsFieldName , IpFieldMapper ::convert )
652+ );
653+ } else {
654+ layers .add (new SortedSetDocValuesSyntheticFieldLoaderLayer (fullPath ()) {
655+ @ Override
656+ protected BytesRef convert (BytesRef value ) {
657+ return IpFieldMapper .convert (value );
658+ }
659+
660+ @ Override
661+ protected BytesRef preserve (BytesRef value ) {
662+ // No need to copy because convert has made a deep copy
663+ return value ;
664+ }
665+ });
666+ }
628667
629668 if (ignoreMalformed ) {
630669 layers .add (new CompositeSyntheticFieldLoader .MalformedValuesLayer (fullPath ()));
@@ -635,4 +674,14 @@ protected BytesRef preserve(BytesRef value) {
635674
636675 return super .syntheticSourceSupport ();
637676 }
677+
678+ static BytesRef convert (BytesRef value ) {
679+ byte [] bytes = Arrays .copyOfRange (value .bytes , value .offset , value .offset + value .length );
680+ return new BytesRef (NetworkAddress .format (InetAddressPoint .decode (bytes )));
681+ }
682+
683+ @ Override
684+ public String getOffsetFieldName () {
685+ return offsetsFieldName ;
686+ }
638687}
0 commit comments