3838import org .elasticsearch .search .lookup .Source ;
3939import org .elasticsearch .search .vectors .SparseVectorQueryWrapper ;
4040import org .elasticsearch .test .index .IndexVersionUtils ;
41+ import org .elasticsearch .xcontent .ToXContent ;
4142import org .elasticsearch .xcontent .XContentBuilder ;
4243import org .elasticsearch .xcontent .XContentParseException ;
4344import org .elasticsearch .xcontent .json .JsonXContent ;
@@ -88,6 +89,9 @@ protected void minimalFieldMappingPreviousIndexVersion(XContentBuilder b) throws
8889 b .startObject ("meta" );
8990 b .endObject ();
9091
92+ // note that internally, this will have a `index_options: null` field,
93+ // but when serialized back to the client, this field will be pruned
94+ // the YAML Rest tests checks for this
9195 b .field ("index_options" , (Object ) null );
9296 }
9397
@@ -256,7 +260,7 @@ private void buildDocForSparseVectorFieldMapping(XContentBuilder b, CheckedConsu
256260 b .endObject ();
257261 };
258262
259- public void testDefaultsWithIncludeDefaults () throws Exception {
263+ public void testDefaultsWithAndWithoutIncludeDefaults () throws Exception {
260264 XContentBuilder orig = JsonXContent .contentBuilder ().startObject ();
261265 createMapperService (fieldMapping (this ::minimalMapping )).documentMapper ().mapping ().toXContent (orig , INCLUDE_DEFAULTS );
262266 orig .endObject ();
@@ -266,9 +270,16 @@ public void testDefaultsWithIncludeDefaults() throws Exception {
266270 withDefaults .endObject ();
267271
268272 assertEquals (Strings .toString (withDefaults ), Strings .toString (orig ));
273+
274+ XContentBuilder origWithoutDefaults = JsonXContent .contentBuilder ().startObject ();
275+ createMapperService (fieldMapping (this ::minimalMapping ))
276+ .documentMapper ().mapping ().toXContent (origWithoutDefaults , ToXContent .EMPTY_PARAMS );
277+ origWithoutDefaults .endObject ();
278+
279+ assertEquals (Strings .toString (fieldMapping (this ::minimalMapping )), Strings .toString (origWithoutDefaults ));
269280 }
270281
271- public void testDefaultsWithIncludeDefaultsOlderIndexVersion () throws Exception {
282+ public void testDefaultsWithAndWithoutIncludeDefaultsOlderIndexVersion () throws Exception {
272283 IndexVersion indexVersion = IndexVersionUtils .randomVersionBetween (
273284 random (),
274285 UPGRADE_TO_LUCENE_10_0_0 ,
@@ -284,19 +295,17 @@ public void testDefaultsWithIncludeDefaultsOlderIndexVersion() throws Exception
284295 withDefaults .endObject ();
285296
286297 assertEquals (Strings .toString (withDefaults ), Strings .toString (orig ));
287- }
288298
289- public void testMappingWithoutIndexOptionsUsesDefaults () throws Exception {
290- DocumentMapper mapper = createDocumentMapper (fieldMapping (this ::minimalMapping ));
291- assertEquals (Strings .toString (fieldMapping (this ::minimalMapping )), mapper .mappingSource ().toString ());
299+ XContentBuilder origWithoutDefaults = JsonXContent .contentBuilder ().startObject ();
300+ createMapperService (indexVersion , fieldMapping (this ::minimalMapping ))
301+ .documentMapper ().mapping ().toXContent (origWithoutDefaults , INCLUDE_DEFAULTS );
302+ origWithoutDefaults .endObject ();
292303
293- IndexVersion preIndexOptionsVersion = IndexVersionUtils .randomVersionBetween (
294- random (),
295- UPGRADE_TO_LUCENE_10_0_0 ,
296- IndexVersionUtils .getPreviousVersion (SPARSE_VECTOR_PRUNING_INDEX_OPTIONS_VERSION )
297- );
298- DocumentMapper previousMapper = createDocumentMapper (preIndexOptionsVersion , fieldMapping (this ::minimalMapping ));
299- assertEquals (Strings .toString (fieldMapping (this ::minimalMapping )), previousMapper .mappingSource ().toString ());
304+ XContentBuilder withoutDefaults = JsonXContent .contentBuilder ().startObject ();
305+ buildDocForSparseVectorFieldMapping (withoutDefaults , this ::minimalFieldMappingPreviousIndexVersion );
306+ withoutDefaults .endObject ();
307+
308+ assertEquals (Strings .toString (withoutDefaults ), Strings .toString (origWithoutDefaults ));
300309 }
301310
302311 public void testMappingWithExplicitIndexOptions () throws Exception {
@@ -694,7 +703,7 @@ private void withSearchExecutionContext(MapperService mapperService, CheckedCons
694703 iw .close ();
695704
696705 try (DirectoryReader reader = wrapInMockESDirectoryReader (DirectoryReader .open (directory ))) {
697- var searchContext = createSearchExecutionContext (mapperService , new IndexSearcher (reader ));
706+ var searchContext = createSearchExecutionContext (mapperService , newSearcher (reader ));
698707 consumer .accept (searchContext );
699708 }
700709 }
0 commit comments