5454import java .util .Arrays ;
5555import java .util .Collections ;
5656import java .util .List ;
57+ import java .util .function .BooleanSupplier ;
5758import java .util .function .Supplier ;
5859
5960/**
@@ -95,15 +96,18 @@ public static class Defaults {
9596 }
9697 }
9798
98- public static final TypeParser PARSER = new FixedTypeParser (c -> new IdFieldMapper ());
99+ public static final TypeParser PARSER = new FixedTypeParser (c -> {
100+ BooleanSupplier fieldDataEnabled = c .mapperService ().isIdFieldDataEnabled ();
101+ return new IdFieldMapper (fieldDataEnabled );
102+ });
99103
100104 static final class IdFieldType extends TermBasedFieldType {
105+ private final BooleanSupplier fieldDataEnabled ;
101106
102- public static final IdFieldType INSTANCE = new IdFieldType ();
103-
104- private IdFieldType () {
107+ IdFieldType (BooleanSupplier fieldDataEnabled ) {
105108 super (NAME , true , true , false , TextSearchInfo .SIMPLE_MATCH_ONLY , Collections .emptyMap ());
106109 setIndexAnalyzer (Lucene .KEYWORD_ANALYZER );
110+ this .fieldDataEnabled = fieldDataEnabled ;
107111 }
108112
109113 @ Override
@@ -143,6 +147,11 @@ public Query termsQuery(List<?> values, QueryShardContext context) {
143147
144148 @ Override
145149 public IndexFieldData .Builder fielddataBuilder (String fullyQualifiedIndexName , Supplier <SearchLookup > searchLookup ) {
150+ if (fieldDataEnabled .getAsBoolean () == false ) {
151+ throw new IllegalArgumentException ("Fielddata access on the _id field is disallowed, "
152+ + "you can re-enable it by updating the dynamic cluster setting: "
153+ + IndicesService .INDICES_ID_FIELD_DATA_ENABLED_SETTING .getKey ());
154+ }
146155 final IndexFieldData .Builder fieldDataBuilder = new PagedBytesIndexFieldData .Builder (
147156 name (),
148157 TextFieldMapper .Defaults .FIELDDATA_MIN_FREQUENCY ,
@@ -156,11 +165,6 @@ public IndexFieldData<?> build(
156165 CircuitBreakerService breakerService ,
157166 MapperService mapperService
158167 ) {
159- if (mapperService .isIdFieldDataEnabled () == false ) {
160- throw new IllegalArgumentException ("Fielddata access on the _id field is disallowed, "
161- + "you can re-enable it by updating the dynamic cluster setting: "
162- + IndicesService .INDICES_ID_FIELD_DATA_ENABLED_SETTING .getKey ());
163- }
164168 deprecationLogger .deprecate ("id_field_data" , ID_FIELD_DATA_DEPRECATION_MESSAGE );
165169 final IndexFieldData <?> fieldData = fieldDataBuilder .build (cache ,
166170 breakerService , mapperService );
@@ -251,8 +255,8 @@ public boolean advanceExact(int doc) throws IOException {
251255 };
252256 }
253257
254- private IdFieldMapper () {
255- super (new IdFieldType ());
258+ private IdFieldMapper (BooleanSupplier fieldDataEnabled ) {
259+ super (new IdFieldType (fieldDataEnabled ));
256260 }
257261
258262 @ Override
0 commit comments