2020package org .elasticsearch .index .mapper .source ;
2121
2222import org .apache .lucene .index .IndexableField ;
23+ import org .elasticsearch .Version ;
24+ import org .elasticsearch .cluster .metadata .IndexMetaData ;
2325import org .elasticsearch .common .bytes .BytesArray ;
2426import org .elasticsearch .common .compress .CompressedString ;
2527import org .elasticsearch .common .compress .CompressorFactory ;
2628import org .elasticsearch .common .settings .ImmutableSettings ;
29+ import org .elasticsearch .common .settings .Settings ;
2730import org .elasticsearch .common .xcontent .XContentBuilder ;
2831import org .elasticsearch .common .xcontent .XContentFactory ;
2932import org .elasticsearch .common .xcontent .XContentType ;
3841
3942import static org .hamcrest .Matchers .*;
4043
41- /**
42- *
43- */
4444public class DefaultSourceMappingTests extends ElasticsearchSingleNodeTest {
4545
46- @ Test
4746 public void testNoFormat () throws Exception {
4847 String mapping = XContentFactory .jsonBuilder ().startObject ().startObject ("type" )
4948 .startObject ("_source" ).endObject ()
@@ -65,7 +64,6 @@ public void testNoFormat() throws Exception {
6564 assertThat (XContentFactory .xContentType (doc .source ()), equalTo (XContentType .SMILE ));
6665 }
6766
68- @ Test
6967 public void testJsonFormat () throws Exception {
7068 String mapping = XContentFactory .jsonBuilder ().startObject ().startObject ("type" )
7169 .startObject ("_source" ).field ("format" , "json" ).endObject ()
@@ -87,7 +85,6 @@ public void testJsonFormat() throws Exception {
8785 assertThat (XContentFactory .xContentType (doc .source ()), equalTo (XContentType .JSON ));
8886 }
8987
90- @ Test
9188 public void testJsonFormatCompressed () throws Exception {
9289 String mapping = XContentFactory .jsonBuilder ().startObject ().startObject ("type" )
9390 .startObject ("_source" ).field ("format" , "json" ).field ("compress" , true ).endObject ()
@@ -113,26 +110,58 @@ public void testJsonFormatCompressed() throws Exception {
113110 assertThat (XContentFactory .xContentType (uncompressed ), equalTo (XContentType .JSON ));
114111 }
115112
116- @ Test
117- public void testIncludeExclude () throws Exception {
113+ public void testIncludesBackcompat () throws Exception {
118114 String mapping = XContentFactory .jsonBuilder ().startObject ().startObject ("type" )
119- .startObject ("_source" ).field ("includes" , new String []{"path1*" }).endObject ()
120- .endObject ().endObject ().string ();
115+ .startObject ("_source" ).field ("includes" , new String []{"path1*" }).endObject ()
116+ .endObject ().endObject ().string ();
117+
118+ try {
119+ createIndex ("testbad" ).mapperService ().documentMapperParser ().parse (mapping );
120+ fail ("includes should not be allowed" );
121+ } catch (MapperParsingException e ) {
122+ assertTrue (e .getMessage ().contains ("unsupported parameters" ));
123+ }
121124
122- DocumentMapper documentMapper = createIndex ("test" ).mapperService ().documentMapperParser ().parse (mapping );
125+ Settings settings = ImmutableSettings .builder ().put (IndexMetaData .SETTING_VERSION_CREATED , Version .V_1_4_2 .id ).build ();
126+ DocumentMapper documentMapper = createIndex ("test" , settings ).mapperService ().documentMapperParser ().parse (mapping );
123127
124128 ParsedDocument doc = documentMapper .parse ("type" , "1" , XContentFactory .jsonBuilder ().startObject ()
125- .startObject ("path1" ).field ("field1" , "value1" ).endObject ()
126- .startObject ("path2" ).field ("field2" , "value2" ).endObject ()
127- .endObject ().bytes ());
129+ .startObject ("path1" ).field ("field1" , "value1" ).endObject ()
130+ .startObject ("path2" ).field ("field2" , "value2" ).endObject ()
131+ .endObject ().bytes ());
128132
129133 IndexableField sourceField = doc .rootDoc ().getField ("_source" );
130134 Map <String , Object > sourceAsMap = XContentFactory .xContent (XContentType .JSON ).createParser (new BytesArray (sourceField .binaryValue ())).mapAndClose ();
131135 assertThat (sourceAsMap .containsKey ("path1" ), equalTo (true ));
132136 assertThat (sourceAsMap .containsKey ("path2" ), equalTo (false ));
133137 }
134138
135- @ Test
139+ public void testExcludesBackcompat () throws Exception {
140+ String mapping = XContentFactory .jsonBuilder ().startObject ().startObject ("type" )
141+ .startObject ("_source" ).field ("excludes" , new String []{"path1*" }).endObject ()
142+ .endObject ().endObject ().string ();
143+
144+ try {
145+ createIndex ("testbad" ).mapperService ().documentMapperParser ().parse (mapping );
146+ fail ("excludes should not be allowed" );
147+ } catch (MapperParsingException e ) {
148+ assertTrue (e .getMessage ().contains ("unsupported parameters" ));
149+ }
150+
151+ Settings settings = ImmutableSettings .builder ().put (IndexMetaData .SETTING_VERSION_CREATED , Version .V_1_4_2 .id ).build ();
152+ DocumentMapper documentMapper = createIndex ("test" , settings ).mapperService ().documentMapperParser ().parse (mapping );
153+
154+ ParsedDocument doc = documentMapper .parse ("type" , "1" , XContentFactory .jsonBuilder ().startObject ()
155+ .startObject ("path1" ).field ("field1" , "value1" ).endObject ()
156+ .startObject ("path2" ).field ("field2" , "value2" ).endObject ()
157+ .endObject ().bytes ());
158+
159+ IndexableField sourceField = doc .rootDoc ().getField ("_source" );
160+ Map <String , Object > sourceAsMap = XContentFactory .xContent (XContentType .JSON ).createParser (new BytesArray (sourceField .binaryValue ())).mapAndClose ();
161+ assertThat (sourceAsMap .containsKey ("path1" ), equalTo (false ));
162+ assertThat (sourceAsMap .containsKey ("path2" ), equalTo (true ));
163+ }
164+
136165 public void testDefaultMappingAndNoMapping () throws Exception {
137166 String defaultMapping = XContentFactory .jsonBuilder ().startObject ().startObject (MapperService .DEFAULT_MAPPING )
138167 .startObject ("_source" ).field ("enabled" , false ).endObject ()
@@ -161,7 +190,6 @@ public void testDefaultMappingAndNoMapping() throws Exception {
161190 }
162191 }
163192
164- @ Test
165193 public void testDefaultMappingAndWithMappingOverride () throws Exception {
166194 String defaultMapping = XContentFactory .jsonBuilder ().startObject ().startObject (MapperService .DEFAULT_MAPPING )
167195 .startObject ("_source" ).field ("enabled" , false ).endObject ()
@@ -176,7 +204,6 @@ public void testDefaultMappingAndWithMappingOverride() throws Exception {
176204 assertThat (mapper .sourceMapper ().enabled (), equalTo (true ));
177205 }
178206
179- @ Test
180207 public void testDefaultMappingAndNoMappingWithMapperService () throws Exception {
181208 String defaultMapping = XContentFactory .jsonBuilder ().startObject ().startObject (MapperService .DEFAULT_MAPPING )
182209 .startObject ("_source" ).field ("enabled" , false ).endObject ()
@@ -190,7 +217,6 @@ public void testDefaultMappingAndNoMappingWithMapperService() throws Exception {
190217 assertThat (mapper .sourceMapper ().enabled (), equalTo (false ));
191218 }
192219
193- @ Test
194220 public void testDefaultMappingAndWithMappingOverrideWithMapperService () throws Exception {
195221 String defaultMapping = XContentFactory .jsonBuilder ().startObject ().startObject (MapperService .DEFAULT_MAPPING )
196222 .startObject ("_source" ).field ("enabled" , false ).endObject ()
@@ -208,66 +234,4 @@ public void testDefaultMappingAndWithMappingOverrideWithMapperService() throws E
208234 assertThat (mapper .type (), equalTo ("my_type" ));
209235 assertThat (mapper .sourceMapper ().enabled (), equalTo (true ));
210236 }
211-
212- @ Test
213- public void testParsingWithDefaultAppliedAndNotApplied () throws Exception {
214- String defaultMapping = XContentFactory .jsonBuilder ().startObject ().startObject (MapperService .DEFAULT_MAPPING )
215- .startObject ("_source" ).array ("includes" , "default_field_path." ).endObject ()
216- .endObject ().endObject ().string ();
217-
218- MapperService mapperService = createIndex ("test" ).mapperService ();
219- mapperService .merge (MapperService .DEFAULT_MAPPING , new CompressedString (defaultMapping ), true );
220-
221- String mapping = XContentFactory .jsonBuilder ().startObject ().startObject ("my_type" )
222- .startObject ("_source" ).array ("includes" , "custom_field_path." ).endObject ()
223- .endObject ().endObject ().string ();
224- mapperService .merge ("my_type" , new CompressedString (mapping ), true );
225- DocumentMapper mapper = mapperService .documentMapper ("my_type" );
226- assertThat (mapper .type (), equalTo ("my_type" ));
227- assertThat (mapper .sourceMapper ().includes ().length , equalTo (2 ));
228- assertThat (mapper .sourceMapper ().includes (), hasItemInArray ("default_field_path." ));
229- assertThat (mapper .sourceMapper ().includes (), hasItemInArray ("custom_field_path." ));
230-
231- mapping = XContentFactory .jsonBuilder ().startObject ().startObject ("my_type" )
232- .startObject ("properties" ).startObject ("text" ).field ("type" , "string" ).endObject ().endObject ()
233- .endObject ().endObject ().string ();
234- mapperService .merge ("my_type" , new CompressedString (mapping ), false );
235- mapper = mapperService .documentMapper ("my_type" );
236- assertThat (mapper .type (), equalTo ("my_type" ));
237- assertThat (mapper .sourceMapper ().includes (), hasItemInArray ("default_field_path." ));
238- assertThat (mapper .sourceMapper ().includes (), hasItemInArray ("custom_field_path." ));
239- assertThat (mapper .sourceMapper ().includes ().length , equalTo (2 ));
240- }
241-
242- public void testDefaultNotAppliedOnUpdate () throws Exception {
243- XContentBuilder defaultMapping = XContentFactory .jsonBuilder ().startObject ().startObject (MapperService .DEFAULT_MAPPING )
244- .startObject ("_source" ).array ("includes" , "default_field_path." ).endObject ()
245- .endObject ().endObject ();
246-
247- IndexService indexService = createIndex ("test" , ImmutableSettings .EMPTY , MapperService .DEFAULT_MAPPING , defaultMapping );
248-
249- String mapping = XContentFactory .jsonBuilder ().startObject ().startObject ("my_type" )
250- .startObject ("_source" ).array ("includes" , "custom_field_path." ).endObject ()
251- .endObject ().endObject ().string ();
252- client ().admin ().indices ().preparePutMapping ("test" ).setType ("my_type" ).setSource (mapping ).get ();
253-
254- DocumentMapper mapper = indexService .mapperService ().documentMapper ("my_type" );
255- assertThat (mapper .type (), equalTo ("my_type" ));
256- assertThat (mapper .sourceMapper ().includes ().length , equalTo (2 ));
257- List <String > includes = Arrays .asList (mapper .sourceMapper ().includes ());
258- assertThat ("default_field_path." , isIn (includes ));
259- assertThat ("custom_field_path." , isIn (includes ));
260-
261- mapping = XContentFactory .jsonBuilder ().startObject ().startObject ("my_type" )
262- .startObject ("properties" ).startObject ("text" ).field ("type" , "string" ).endObject ().endObject ()
263- .endObject ().endObject ().string ();
264- client ().admin ().indices ().preparePutMapping ("test" ).setType ("my_type" ).setSource (mapping ).get ();
265-
266- mapper = indexService .mapperService ().documentMapper ("my_type" );
267- assertThat (mapper .type (), equalTo ("my_type" ));
268- includes = Arrays .asList (mapper .sourceMapper ().includes ());
269- assertThat ("default_field_path." , isIn (includes ));
270- assertThat ("custom_field_path." , isIn (includes ));
271- assertThat (mapper .sourceMapper ().includes ().length , equalTo (2 ));
272- }
273237}
0 commit comments