Skip to content

Commit b2c88df

Browse files
authored
Delay _uid field data deprecation warning (#30651)
A deprecation warning is printed when creating the fieldddata builder for the `_uid` field. This change moves the deprecation logging to the building of the fielddata since otherwise APIs like `_field_caps` can emit deprecation warning when they just test the capabilities of the `_uid` field. Closes #30625
1 parent 26ca19e commit b2c88df

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ public String typeName() {
113113
@Override
114114
public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName) {
115115
if (indexOptions() == IndexOptions.NONE) {
116-
DEPRECATION_LOGGER.deprecated("Fielddata access on the _uid field is deprecated, use _id instead");
117116
return new IndexFieldData.Builder() {
118117
@Override
119118
public IndexFieldData<?> build(IndexSettings indexSettings, MappedFieldType fieldType, IndexFieldDataCache cache,
120119
CircuitBreakerService breakerService, MapperService mapperService) {
120+
DEPRECATION_LOGGER.deprecated("Fielddata access on the _uid field is deprecated, use _id instead");
121121
MappedFieldType idFieldType = mapperService.fullName(IdFieldMapper.NAME);
122122
IndexFieldData<?> idFieldData = idFieldType.fielddataBuilder(fullyQualifiedIndexName)
123123
.build(indexSettings, idFieldType, cache, breakerService, mapperService);

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,25 @@
2525
import org.elasticsearch.Version;
2626
import org.elasticsearch.cluster.metadata.IndexMetaData;
2727
import org.elasticsearch.common.UUIDs;
28+
import org.elasticsearch.common.breaker.CircuitBreaker;
29+
import org.elasticsearch.common.breaker.NoopCircuitBreaker;
2830
import org.elasticsearch.common.settings.Settings;
2931
import org.elasticsearch.index.IndexSettings;
32+
import org.elasticsearch.index.fielddata.IndexFieldData;
33+
import org.elasticsearch.index.fielddata.IndexFieldDataCache;
3034
import org.elasticsearch.index.mapper.MappedFieldType;
3135
import org.elasticsearch.index.mapper.UidFieldMapper;
3236
import org.elasticsearch.index.query.QueryShardContext;
37+
import org.elasticsearch.indices.breaker.CircuitBreakerService;
38+
import org.elasticsearch.indices.breaker.NoneCircuitBreakerService;
3339
import org.mockito.Mockito;
3440

41+
import java.io.IOException;
3542
import java.util.Collection;
3643
import java.util.Collections;
3744

45+
import static org.mockito.Matchers.any;
46+
3847
public class UidFieldTypeTests extends FieldTypeTestCase {
3948
@Override
4049
protected MappedFieldType createDefaultFieldType() {
@@ -132,4 +141,35 @@ public void testTermsQuery() throws Exception {
132141
query = ft.termQuery("type2#id", context);
133142
assertEquals(new TermInSetQuery("_id"), query);
134143
}
144+
145+
public void testIsAggregatable() {
146+
Settings indexSettings = Settings.builder()
147+
.put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT)
148+
.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)
149+
.put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1)
150+
.put(IndexMetaData.SETTING_INDEX_UUID, UUIDs.randomBase64UUID())
151+
.build();
152+
IndexMetaData indexMetaData = IndexMetaData.builder(IndexMetaData.INDEX_UUID_NA_VALUE).settings(indexSettings).build();
153+
IndexSettings mockSettings = new IndexSettings(indexMetaData, Settings.EMPTY);
154+
MappedFieldType ft = UidFieldMapper.defaultFieldType(mockSettings);
155+
assertTrue(ft.isAggregatable());
156+
}
157+
158+
public void testFieldDataDeprecation() {
159+
Settings indexSettings = Settings.builder()
160+
.put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT)
161+
.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)
162+
.put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1)
163+
.put(IndexMetaData.SETTING_INDEX_UUID, UUIDs.randomBase64UUID())
164+
.build();
165+
IndexMetaData indexMetaData = IndexMetaData.builder(IndexMetaData.INDEX_UUID_NA_VALUE).settings(indexSettings).build();
166+
IndexSettings mockSettings = new IndexSettings(indexMetaData, Settings.EMPTY);
167+
MappedFieldType ft = UidFieldMapper.defaultFieldType(mockSettings);
168+
IndexFieldData.Builder builder = ft.fielddataBuilder("");
169+
MapperService mockMapper = Mockito.mock(MapperService.class);
170+
Mockito.when(mockMapper.fullName(any())).thenReturn(new IdFieldMapper.IdFieldType());
171+
Mockito.when(mockMapper.types()).thenReturn(Collections.singleton("doc"));
172+
builder.build(mockSettings, ft, null, new NoneCircuitBreakerService(), mockMapper);
173+
assertWarnings("Fielddata access on the _uid field is deprecated, use _id instead");
174+
}
135175
}

0 commit comments

Comments
 (0)