Skip to content

Commit bdb4f8b

Browse files
committed
Make sure that IdFieldType#isAggregatable is accurate.
Before, it always returned 'true' even when the setting "indices.id_field_data.enabled" was false.
1 parent 19c19f2 commit bdb4f8b

File tree

4 files changed

+29
-15
lines changed

4 files changed

+29
-15
lines changed

server/src/main/java/org/elasticsearch/index/mapper/IdFieldMapper.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import java.util.Arrays;
5555
import java.util.Collections;
5656
import java.util.List;
57+
import java.util.function.BooleanSupplier;
5758
import 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

server/src/main/java/org/elasticsearch/index/mapper/MapperService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,8 @@ public Analyzer searchQuoteAnalyzer() {
464464
/**
465465
* Returns <code>true</code> if fielddata is enabled for the {@link IdFieldMapper} field, <code>false</code> otherwise.
466466
*/
467-
public boolean isIdFieldDataEnabled() {
468-
return idFieldDataEnabled.getAsBoolean();
467+
public BooleanSupplier isIdFieldDataEnabled() {
468+
return idFieldDataEnabled;
469469
}
470470

471471
@Override

server/src/test/java/org/elasticsearch/index/mapper/IdFieldMapperTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public void testEnableFieldData() throws IOException {
8888
throw new UnsupportedOperationException();
8989
}).build(null, null, mapperService));
9090
assertThat(exc.getMessage(), containsString(IndicesService.INDICES_ID_FIELD_DATA_ENABLED_SETTING.getKey()));
91+
assertFalse(ft.isAggregatable());
9192

9293
client().admin().cluster().prepareUpdateSettings()
9394
.setTransientSettings(Settings.builder().put(IndicesService.INDICES_ID_FIELD_DATA_ENABLED_SETTING.getKey(), true))
@@ -97,6 +98,7 @@ public void testEnableFieldData() throws IOException {
9798
throw new UnsupportedOperationException();
9899
}).build(null, null, mapperService);
99100
assertWarnings(ID_FIELD_DATA_DEPRECATION_MESSAGE);
101+
assertTrue(ft.isAggregatable());
100102
} finally {
101103
// unset cluster setting
102104
client().admin().cluster().prepareUpdateSettings()

server/src/test/java/org/elasticsearch/index/mapper/IdFieldTypeTests.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
public class IdFieldTypeTests extends ESTestCase {
3333

3434
public void testRangeQuery() {
35-
MappedFieldType ft = IdFieldMapper.IdFieldType.INSTANCE;
35+
MappedFieldType ft = new IdFieldMapper.IdFieldType(() -> false);
3636
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
3737
() -> ft.rangeQuery(null, null, randomBoolean(), randomBoolean(), null, null, null, null));
3838
assertEquals("Field [_id] of type [_id] does not support range queries", e.getMessage());
@@ -53,8 +53,16 @@ public void testTermsQuery() throws Exception {
5353
MapperService mapperService = Mockito.mock(MapperService.class);
5454
Mockito.when(context.getMapperService()).thenReturn(mapperService);
5555

56-
MappedFieldType ft = IdFieldMapper.IdFieldType.INSTANCE;
56+
MappedFieldType ft = new IdFieldMapper.IdFieldType(() -> false);
5757
Query query = ft.termQuery("id", context);
5858
assertEquals(new TermInSetQuery("_id", Uid.encodeId("id")), query);
5959
}
60+
61+
public void testIsAggregatable() {
62+
MappedFieldType ft = new IdFieldMapper.IdFieldType(() -> false);
63+
assertFalse(ft.isAggregatable());
64+
65+
ft = new IdFieldMapper.IdFieldType(() -> true);
66+
assertTrue(ft.isAggregatable());
67+
}
6068
}

0 commit comments

Comments
 (0)