2121import org .apache .lucene .store .Directory ;
2222import org .apache .lucene .tests .index .RandomIndexWriter ;
2323import org .elasticsearch .common .Strings ;
24+ import org .elasticsearch .common .bytes .BytesReference ;
2425import org .elasticsearch .common .compress .CompressedXContent ;
2526import org .elasticsearch .core .CheckedConsumer ;
2627import org .elasticsearch .core .Nullable ;
4142import org .elasticsearch .xcontent .ToXContent ;
4243import org .elasticsearch .xcontent .XContentBuilder ;
4344import org .elasticsearch .xcontent .XContentParseException ;
45+ import org .elasticsearch .xcontent .XContentType ;
4446import org .elasticsearch .xcontent .json .JsonXContent ;
4547import org .hamcrest .Matchers ;
4648import org .junit .AssumptionViolatedException ;
5759import static org .elasticsearch .index .mapper .vectors .SparseVectorFieldMapper .NEW_SPARSE_VECTOR_INDEX_VERSION ;
5860import static org .elasticsearch .index .mapper .vectors .SparseVectorFieldMapper .PREVIOUS_SPARSE_VECTOR_INDEX_VERSION ;
5961import static org .elasticsearch .index .mapper .vectors .SparseVectorFieldMapper .SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_VERSION ;
62+ import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertToXContentEquivalent ;
6063import static org .elasticsearch .xcontent .XContentFactory .jsonBuilder ;
6164import static org .hamcrest .Matchers .containsString ;
6265import static org .hamcrest .Matchers .equalTo ;
@@ -230,54 +233,31 @@ public void testDefaults() throws Exception {
230233 assertTrue (freq1 < freq2 );
231234 }
232235
233- private void buildDocForSparseVectorFieldMapping (XContentBuilder b , CheckedConsumer <XContentBuilder , IOException > supplier )
234- throws IOException {
235- b .startObject ("_doc" );
236- {
237- b .startArray ("dynamic_date_formats" );
238- {
239- b .value ("strict_date_optional_time||epoch_millis" );
240- b .value ("yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis" );
241- }
242- b .endArray ();
243-
244- b .startArray ("dynamic_templates" );
245- b .endArray ();
246-
247- b .field ("date_detection" , true );
248- b .field ("numeric_detection" , false );
249-
250- b .startObject ("properties" );
251- {
252- b .startObject ("field" );
253-
254- supplier .accept (b );
255-
256- b .endObject ();
257- }
258- b .endObject ();
259- }
260- b .endObject ();
261- };
262-
263236 public void testDefaultsWithAndWithoutIncludeDefaults () throws Exception {
264237 XContentBuilder orig = JsonXContent .contentBuilder ().startObject ();
265- createMapperService (fieldMapping (this ::minimalMapping )).documentMapper ().mapping ( ).toXContent (orig , INCLUDE_DEFAULTS );
238+ createMapperService (fieldMapping (this ::minimalMapping )).mappingLookup ().getMapper ( "field" ).toXContent (orig , INCLUDE_DEFAULTS );
266239 orig .endObject ();
267240
268241 XContentBuilder withDefaults = JsonXContent .contentBuilder ().startObject ();
269- buildDocForSparseVectorFieldMapping (withDefaults , this ::minimalMappingWithExplicitDefaults );
242+ withDefaults .startObject ("field" );
243+ minimalMappingWithExplicitDefaults (withDefaults );
244+ withDefaults .endObject ();
270245 withDefaults .endObject ();
271246
272- assertEquals ( Strings . toString (withDefaults ), Strings . toString (orig ));
247+ assertToXContentEquivalent ( BytesReference . bytes (withDefaults ), BytesReference . bytes (orig ), XContentType . JSON );
273248
274249 XContentBuilder origWithoutDefaults = JsonXContent .contentBuilder ().startObject ();
275- createMapperService (fieldMapping (this ::minimalMapping )).documentMapper ()
276- .mapping ()
250+ createMapperService (fieldMapping (this ::minimalMapping )).mappingLookup ().getMapper ("field" )
277251 .toXContent (origWithoutDefaults , ToXContent .EMPTY_PARAMS );
278252 origWithoutDefaults .endObject ();
279253
280- assertEquals (Strings .toString (fieldMapping (this ::minimalMapping )), Strings .toString (origWithoutDefaults ));
254+ XContentBuilder withoutDefaults = JsonXContent .contentBuilder ().startObject ();
255+ withoutDefaults .startObject ("field" );
256+ minimalMapping (withoutDefaults );
257+ withoutDefaults .endObject ();
258+ withoutDefaults .endObject ();
259+
260+ assertToXContentEquivalent (BytesReference .bytes (withoutDefaults ), BytesReference .bytes (origWithoutDefaults ), XContentType .JSON );
281261 }
282262
283263 public void testDefaultsWithAndWithoutIncludeDefaultsOlderIndexVersion () throws Exception {
@@ -288,26 +268,30 @@ public void testDefaultsWithAndWithoutIncludeDefaultsOlderIndexVersion() throws
288268 );
289269
290270 XContentBuilder orig = JsonXContent .contentBuilder ().startObject ();
291- createMapperService (indexVersion , fieldMapping (this ::minimalMapping )).documentMapper ().mapping ().toXContent (orig , INCLUDE_DEFAULTS );
271+ createMapperService (indexVersion , fieldMapping (this ::minimalMapping )).mappingLookup ().getMapper ("field" )
272+ .toXContent (orig , INCLUDE_DEFAULTS );
292273 orig .endObject ();
293274
294275 XContentBuilder withDefaults = JsonXContent .contentBuilder ().startObject ();
295- buildDocForSparseVectorFieldMapping (withDefaults , this ::minimalFieldMappingPreviousIndexVersion );
276+ withDefaults .startObject ("field" );
277+ minimalFieldMappingPreviousIndexVersion (withDefaults );
278+ withDefaults .endObject ();
296279 withDefaults .endObject ();
297280
298- assertEquals ( Strings . toString (withDefaults ), Strings . toString (orig ));
281+ assertToXContentEquivalent ( BytesReference . bytes (withDefaults ), BytesReference . bytes (orig ), XContentType . JSON );
299282
300283 XContentBuilder origWithoutDefaults = JsonXContent .contentBuilder ().startObject ();
301- createMapperService (indexVersion , fieldMapping (this ::minimalMapping )).documentMapper ()
302- .mapping ()
303- .toXContent (origWithoutDefaults , INCLUDE_DEFAULTS );
284+ createMapperService (indexVersion , fieldMapping (this ::minimalMapping )).mappingLookup ().getMapper ("field" )
285+ .toXContent (origWithoutDefaults , ToXContent .EMPTY_PARAMS );
304286 origWithoutDefaults .endObject ();
305287
306288 XContentBuilder withoutDefaults = JsonXContent .contentBuilder ().startObject ();
307- buildDocForSparseVectorFieldMapping (withoutDefaults , this ::minimalFieldMappingPreviousIndexVersion );
289+ withoutDefaults .startObject ("field" );
290+ minimalMapping (withoutDefaults );
291+ withoutDefaults .endObject ();
308292 withoutDefaults .endObject ();
309293
310- assertEquals ( Strings . toString (withoutDefaults ), Strings . toString (origWithoutDefaults ));
294+ assertToXContentEquivalent ( BytesReference . bytes (withoutDefaults ), BytesReference . bytes (origWithoutDefaults ), XContentType . JSON );
311295 }
312296
313297 public void testMappingWithExplicitIndexOptions () throws Exception {
0 commit comments