20
20
package org .elasticsearch .index .mapper ;
21
21
22
22
import org .apache .lucene .index .IndexOptions ;
23
+ import org .elasticsearch .Version ;
24
+ import org .elasticsearch .cluster .metadata .IndexMetaData ;
23
25
import org .elasticsearch .common .Strings ;
24
26
import org .elasticsearch .common .bytes .BytesReference ;
25
27
import org .elasticsearch .common .compress .CompressedXContent ;
28
+ import org .elasticsearch .common .settings .Settings ;
26
29
import org .elasticsearch .common .xcontent .XContentFactory ;
27
30
import org .elasticsearch .common .xcontent .XContentType ;
28
31
import org .elasticsearch .test .ESSingleNodeTestCase ;
32
+ import org .elasticsearch .test .VersionUtils ;
29
33
30
34
import java .util .Arrays ;
31
35
import java .util .Collections ;
@@ -95,33 +99,33 @@ public void testInjectIntoDocDuringParsing() throws Exception {
95
99
assertFieldNames (Collections .emptySet (), doc );
96
100
}
97
101
98
- public void testExplicitEnabled () throws Exception {
102
+ public void testUsingEnabledSettingThrows () throws Exception {
99
103
String mapping = Strings .toString (XContentFactory .jsonBuilder ().startObject ().startObject ("type" )
100
104
.startObject ("_field_names" ).field ("enabled" , true ).endObject ()
101
105
.startObject ("properties" )
102
106
.startObject ("field" ).field ("type" , "keyword" ).field ("doc_values" , false ).endObject ()
103
107
.endObject ().endObject ().endObject ());
104
- DocumentMapper docMapper = createIndex ("test" ).mapperService ().documentMapperParser ()
105
- .parse ("type" , new CompressedXContent (mapping ));
106
- FieldNamesFieldMapper fieldNamesMapper = docMapper .metadataMapper (FieldNamesFieldMapper .class );
107
- assertTrue (fieldNamesMapper .fieldType ().isEnabled ());
108
+ MapperParsingException ex = expectThrows (MapperParsingException .class ,
109
+ () -> createIndex ("test" ).mapperService ().documentMapperParser ().parse ("type" , new CompressedXContent (mapping )));
108
110
109
- ParsedDocument doc = docMapper .parse (new SourceToParse ("test" , "type" , "1" ,
110
- BytesReference .bytes (XContentFactory .jsonBuilder ()
111
- .startObject ()
112
- .field ("field" , "value" )
113
- .endObject ()),
114
- XContentType .JSON ));
115
-
116
- assertFieldNames (set ("field" ), doc );
117
- assertWarnings (FieldNamesFieldMapper .TypeParser .ENABLED_DEPRECATION_MESSAGE .replace ("{}" , "test" ));
111
+ assertEquals ("The `enabled` setting for the `_field_names` field has been deprecated and removed but is still used in index [{}]. "
112
+ + "Please remove it from your mappings and templates." , ex .getMessage ());
118
113
}
119
114
120
- public void testDisabled () throws Exception {
115
+ /**
116
+ * disabling the _field_names should still work for indices before 8.0
117
+ */
118
+ public void testUsingEnabledBefore8 () throws Exception {
121
119
String mapping = Strings .toString (XContentFactory .jsonBuilder ().startObject ().startObject ("type" )
122
120
.startObject ("_field_names" ).field ("enabled" , false ).endObject ()
123
121
.endObject ().endObject ());
124
- DocumentMapper docMapper = createIndex ("test" ).mapperService ().documentMapperParser ()
122
+
123
+ DocumentMapper docMapper = createIndex ("test" ,
124
+ Settings .builder ()
125
+ .put (IndexMetaData .SETTING_INDEX_VERSION_CREATED .getKey (),
126
+ VersionUtils .randomPreviousCompatibleVersion (random (), Version .V_8_0_0 ))
127
+ .build ()).mapperService ()
128
+ .documentMapperParser ()
125
129
.parse ("type" , new CompressedXContent (mapping ));
126
130
FieldNamesFieldMapper fieldNamesMapper = docMapper .metadataMapper (FieldNamesFieldMapper .class );
127
131
assertFalse (fieldNamesMapper .fieldType ().isEnabled ());
@@ -137,14 +141,20 @@ public void testDisabled() throws Exception {
137
141
assertWarnings (FieldNamesFieldMapper .TypeParser .ENABLED_DEPRECATION_MESSAGE .replace ("{}" , "test" ));
138
142
}
139
143
140
- public void testMergingMappings () throws Exception {
144
+ /**
145
+ * Merging the "_field_names" enabled setting is forbidden in 8.0, but we still want to tests the behavior on pre-8 indices
146
+ */
147
+ public void testMergingMappingsBefore8 () throws Exception {
141
148
String enabledMapping = Strings .toString (XContentFactory .jsonBuilder ().startObject ().startObject ("type" )
142
149
.startObject ("_field_names" ).field ("enabled" , true ).endObject ()
143
150
.endObject ().endObject ());
144
151
String disabledMapping = Strings .toString (XContentFactory .jsonBuilder ().startObject ().startObject ("type" )
145
152
.startObject ("_field_names" ).field ("enabled" , false ).endObject ()
146
153
.endObject ().endObject ());
147
- MapperService mapperService = createIndex ("test" ).mapperService ();
154
+ MapperService mapperService = createIndex ("test" , Settings .builder ()
155
+ .put (IndexMetaData .SETTING_INDEX_VERSION_CREATED .getKey (),
156
+ VersionUtils .randomPreviousCompatibleVersion (random (), Version .V_8_0_0 ))
157
+ .build ()).mapperService ();
148
158
149
159
DocumentMapper mapperEnabled = mapperService .merge ("type" , new CompressedXContent (enabledMapping ),
150
160
MapperService .MergeReason .MAPPING_UPDATE );
@@ -156,4 +166,12 @@ public void testMergingMappings() throws Exception {
156
166
assertTrue (mapperEnabled .metadataMapper (FieldNamesFieldMapper .class ).fieldType ().isEnabled ());
157
167
assertWarnings (FieldNamesFieldMapper .TypeParser .ENABLED_DEPRECATION_MESSAGE .replace ("{}" , "test" ));
158
168
}
169
+
170
+ @ Override
171
+ protected boolean forbidPrivateIndexSettings () {
172
+ /**
173
+ * This is needed to force the index version with {@link IndexMetaData.SETTING_INDEX_VERSION_CREATED}.
174
+ */
175
+ return false ;
176
+ }
159
177
}
0 commit comments