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,16 @@ 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+ IndexVersions .SYNTHETIC_SOURCE_STORE_ARRAYS_NATIVELY_IP
205+ );
187206 return new IpFieldMapper (
188207 leafName (),
189208 new IpFieldType (
@@ -198,15 +217,16 @@ public IpFieldMapper build(MapperBuilderContext context) {
198217 ),
199218 builderParams (this , context ),
200219 context .isSourceSynthetic (),
201- this
220+ this ,
221+ offsetsFieldName
202222 );
203223 }
204224
205225 }
206226
207227 public static final TypeParser PARSER = createTypeParserWithLegacySupport ((n , c ) -> {
208228 boolean ignoreMalformedByDefault = IGNORE_MALFORMED_SETTING .get (c .getSettings ());
209- return new Builder (n , c .scriptCompiler (), ignoreMalformedByDefault , c .indexVersionCreated ());
229+ return new Builder (n , c .scriptCompiler (), ignoreMalformedByDefault , c .indexVersionCreated (), c . getIndexSettings (). sourceKeepMode () );
210230 });
211231
212232 public static final class IpFieldType extends SimpleMappedFieldType {
@@ -501,13 +521,16 @@ public TermsEnum getTerms(IndexReader reader, String prefix, boolean caseInsensi
501521 private final Script script ;
502522 private final FieldValues <InetAddress > scriptValues ;
503523 private final ScriptCompiler scriptCompiler ;
524+ private final SourceKeepMode indexSourceKeepMode ;
525+ private final String offsetsFieldName ;
504526
505527 private IpFieldMapper (
506528 String simpleName ,
507529 MappedFieldType mappedFieldType ,
508530 BuilderParams builderParams ,
509531 boolean storeIgnored ,
510- Builder builder
532+ Builder builder ,
533+ String offsetsFieldName
511534 ) {
512535 super (simpleName , mappedFieldType , builderParams );
513536 this .ignoreMalformedByDefault = builder .ignoreMalformedByDefault ;
@@ -523,6 +546,8 @@ private IpFieldMapper(
523546 this .scriptCompiler = builder .scriptCompiler ;
524547 this .dimension = builder .dimension .getValue ();
525548 this .storeIgnored = storeIgnored ;
549+ this .indexSourceKeepMode = builder .indexSourceKeepMode ;
550+ this .offsetsFieldName = offsetsFieldName ;
526551 }
527552
528553 @ Override
@@ -561,6 +586,14 @@ protected void parseCreateField(DocumentParserContext context) throws IOExceptio
561586 if (address != null ) {
562587 indexValue (context , address );
563588 }
589+ if (offsetsFieldName != null && context .isImmediateParentAnArray () && context .canAddIgnoredField ()) {
590+ if (address != null ) {
591+ BytesRef sortableValue = new BytesRef (InetAddressPoint .encode (address ));
592+ context .getOffSetContext ().recordOffset (offsetsFieldName , sortableValue );
593+ } else {
594+ context .getOffSetContext ().recordNull (offsetsFieldName );
595+ }
596+ }
564597 }
565598
566599 private void indexValue (DocumentParserContext context , InetAddress address ) {
@@ -593,7 +626,9 @@ protected void indexScriptValues(
593626
594627 @ Override
595628 public FieldMapper .Builder getMergeBuilder () {
596- return new Builder (leafName (), scriptCompiler , ignoreMalformedByDefault , indexCreatedVersion ).dimension (dimension ).init (this );
629+ return new Builder (leafName (), scriptCompiler , ignoreMalformedByDefault , indexCreatedVersion , indexSourceKeepMode ).dimension (
630+ dimension
631+ ).init (this );
597632 }
598633
599634 @ Override
@@ -610,19 +645,24 @@ protected SyntheticSourceSupport syntheticSourceSupport() {
610645 if (hasDocValues ) {
611646 return new SyntheticSourceSupport .Native (() -> {
612647 var layers = new ArrayList <CompositeSyntheticFieldLoader .Layer >();
613- layers .add (new SortedSetDocValuesSyntheticFieldLoaderLayer (fullPath ()) {
614- @ Override
615- protected BytesRef convert (BytesRef value ) {
616- byte [] bytes = Arrays .copyOfRange (value .bytes , value .offset , value .offset + value .length );
617- return new BytesRef (NetworkAddress .format (InetAddressPoint .decode (bytes )));
618- }
619-
620- @ Override
621- protected BytesRef preserve (BytesRef value ) {
622- // No need to copy because convert has made a deep copy
623- return value ;
624- }
625- });
648+ if (offsetsFieldName != null ) {
649+ layers .add (
650+ new SortedSetWithOffsetsDocValuesSyntheticFieldLoaderLayer (fullPath (), offsetsFieldName , IpFieldMapper ::convert )
651+ );
652+ } else {
653+ layers .add (new SortedSetDocValuesSyntheticFieldLoaderLayer (fullPath ()) {
654+ @ Override
655+ protected BytesRef convert (BytesRef value ) {
656+ return IpFieldMapper .convert (value );
657+ }
658+
659+ @ Override
660+ protected BytesRef preserve (BytesRef value ) {
661+ // No need to copy because convert has made a deep copy
662+ return value ;
663+ }
664+ });
665+ }
626666
627667 if (ignoreMalformed ) {
628668 layers .add (new CompositeSyntheticFieldLoader .MalformedValuesLayer (fullPath ()));
@@ -633,4 +673,14 @@ protected BytesRef preserve(BytesRef value) {
633673
634674 return super .syntheticSourceSupport ();
635675 }
676+
677+ static BytesRef convert (BytesRef value ) {
678+ byte [] bytes = Arrays .copyOfRange (value .bytes , value .offset , value .offset + value .length );
679+ return new BytesRef (NetworkAddress .format (InetAddressPoint .decode (bytes )));
680+ }
681+
682+ @ Override
683+ public String getOffsetFieldName () {
684+ return offsetsFieldName ;
685+ }
636686}
0 commit comments