Skip to content

Commit baa3fd8

Browse files
committed
Mappings: Make _type use doc values
_type should have got doc values with the change to default doc values. However, due to how metadata fields have separate builders and special constructors, it was not picking it up. This change updates the field type for _type to have doc values. closes #14781
1 parent 3cc1d27 commit baa3fd8

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

core/src/main/java/org/elasticsearch/index/mapper/internal/TypeFieldMapper.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import org.apache.lucene.util.BytesRef;
3131
import org.elasticsearch.Version;
3232
import org.elasticsearch.common.Nullable;
33+
import org.elasticsearch.common.logging.ESLogger;
34+
import org.elasticsearch.common.logging.ESLoggerFactory;
3335
import org.elasticsearch.common.lucene.BytesRefs;
3436
import org.elasticsearch.common.lucene.Lucene;
3537
import org.elasticsearch.common.settings.Settings;
@@ -86,7 +88,7 @@ public Builder(MappedFieldType existing) {
8688

8789
@Override
8890
public TypeFieldMapper build(BuilderContext context) {
89-
fieldType.setNames(new MappedFieldType.Names(indexName, indexName, name));
91+
fieldType.setNames(buildNames(context));
9092
return new TypeFieldMapper(fieldType, context.indexSettings());
9193
}
9294
}
@@ -146,12 +148,21 @@ public Query termQuery(Object value, @Nullable QueryShardContext context) {
146148
}
147149

148150
public TypeFieldMapper(Settings indexSettings, MappedFieldType existing) {
149-
this(existing == null ? Defaults.FIELD_TYPE.clone() : existing.clone(),
151+
this(existing == null ? defaultFieldType(indexSettings) : existing.clone(),
150152
indexSettings);
151153
}
152154

153155
public TypeFieldMapper(MappedFieldType fieldType, Settings indexSettings) {
154-
super(NAME, fieldType, Defaults.FIELD_TYPE, indexSettings);
156+
super(NAME, fieldType, defaultFieldType(indexSettings), indexSettings);
157+
}
158+
159+
private static MappedFieldType defaultFieldType(Settings indexSettings) {
160+
MappedFieldType defaultFieldType = Defaults.FIELD_TYPE.clone();
161+
Version indexCreated = Version.indexCreated(indexSettings);
162+
if (indexCreated.onOrAfter(Version.V_2_1_0)) {
163+
defaultFieldType.setHasDocValues(true);
164+
}
165+
return defaultFieldType;
155166
}
156167

157168
@Override
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.index.mapper.internal;
21+
22+
import org.elasticsearch.Version;
23+
import org.elasticsearch.cluster.metadata.IndexMetaData;
24+
import org.elasticsearch.common.settings.Settings;
25+
import org.elasticsearch.common.xcontent.XContentFactory;
26+
import org.elasticsearch.index.mapper.DocumentMapper;
27+
import org.elasticsearch.test.ESSingleNodeTestCase;
28+
29+
public class TypeFieldMapperTests extends ESSingleNodeTestCase {
30+
31+
public void testDocValues() throws Exception {
32+
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").endObject().endObject().string();
33+
34+
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping);
35+
TypeFieldMapper typeMapper = docMapper.rootMapper(TypeFieldMapper.class);
36+
assertTrue(typeMapper.fieldType().hasDocValues());
37+
}
38+
39+
public void testDocValuesPre21() throws Exception {
40+
// between 2.0 and 2.1, doc values was disabled for _type
41+
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").endObject().endObject().string();
42+
Settings bwcSettings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_2_0_0_beta1.id).build();
43+
44+
DocumentMapper docMapper = createIndex("test", bwcSettings).mapperService().documentMapperParser().parse(mapping);
45+
TypeFieldMapper typeMapper = docMapper.rootMapper(TypeFieldMapper.class);
46+
assertFalse(typeMapper.fieldType().hasDocValues());
47+
}
48+
}

0 commit comments

Comments
 (0)