2020package org .elasticsearch .index .mapper ;
2121
2222import org .apache .lucene .index .IndexOptions ;
23+ import org .elasticsearch .Version ;
24+ import org .elasticsearch .cluster .metadata .IndexMetaData ;
2325import org .elasticsearch .common .Strings ;
2426import org .elasticsearch .common .bytes .BytesReference ;
2527import org .elasticsearch .common .compress .CompressedXContent ;
28+ import org .elasticsearch .common .settings .Settings ;
2629import org .elasticsearch .common .xcontent .XContentFactory ;
2730import org .elasticsearch .common .xcontent .XContentType ;
2831import org .elasticsearch .test .ESSingleNodeTestCase ;
32+ import org .elasticsearch .test .VersionUtils ;
2933
3034import java .util .Arrays ;
3135import java .util .Collections ;
@@ -95,33 +99,33 @@ public void testInjectIntoDocDuringParsing() throws Exception {
9599 assertFieldNames (Collections .emptySet (), doc );
96100 }
97101
98- public void testExplicitEnabled () throws Exception {
102+ public void testUsingEnabledSettingThrows () throws Exception {
99103 String mapping = Strings .toString (XContentFactory .jsonBuilder ().startObject ().startObject ("type" )
100104 .startObject ("_field_names" ).field ("enabled" , true ).endObject ()
101105 .startObject ("properties" )
102106 .startObject ("field" ).field ("type" , "keyword" ).field ("doc_values" , false ).endObject ()
103107 .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 )));
108110
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 ());
118113 }
119114
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 {
121119 String mapping = Strings .toString (XContentFactory .jsonBuilder ().startObject ().startObject ("type" )
122120 .startObject ("_field_names" ).field ("enabled" , false ).endObject ()
123121 .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 ()
125129 .parse ("type" , new CompressedXContent (mapping ));
126130 FieldNamesFieldMapper fieldNamesMapper = docMapper .metadataMapper (FieldNamesFieldMapper .class );
127131 assertFalse (fieldNamesMapper .fieldType ().isEnabled ());
@@ -137,14 +141,20 @@ public void testDisabled() throws Exception {
137141 assertWarnings (FieldNamesFieldMapper .TypeParser .ENABLED_DEPRECATION_MESSAGE .replace ("{}" , "test" ));
138142 }
139143
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 {
141148 String enabledMapping = Strings .toString (XContentFactory .jsonBuilder ().startObject ().startObject ("type" )
142149 .startObject ("_field_names" ).field ("enabled" , true ).endObject ()
143150 .endObject ().endObject ());
144151 String disabledMapping = Strings .toString (XContentFactory .jsonBuilder ().startObject ().startObject ("type" )
145152 .startObject ("_field_names" ).field ("enabled" , false ).endObject ()
146153 .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 ();
148158
149159 DocumentMapper mapperEnabled = mapperService .merge ("type" , new CompressedXContent (enabledMapping ),
150160 MapperService .MergeReason .MAPPING_UPDATE );
@@ -156,4 +166,12 @@ public void testMergingMappings() throws Exception {
156166 assertTrue (mapperEnabled .metadataMapper (FieldNamesFieldMapper .class ).fieldType ().isEnabled ());
157167 assertWarnings (FieldNamesFieldMapper .TypeParser .ENABLED_DEPRECATION_MESSAGE .replace ("{}" , "test" ));
158168 }
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+ }
159177}
0 commit comments