1313import org .apache .http .entity .InputStreamEntity ;
1414import org .elasticsearch .client .Request ;
1515import org .elasticsearch .cluster .metadata .IndexMetadata ;
16+ import org .elasticsearch .common .Strings ;
1617import org .elasticsearch .common .settings .Settings ;
17- import org .elasticsearch .core .Strings ;
1818import org .elasticsearch .index .IndexSettings ;
1919import org .elasticsearch .index .mapper .MapperService ;
2020import org .elasticsearch .test .XContentTestUtils ;
4242import static org .elasticsearch .test .rest .ObjectPath .createFromResponse ;
4343import static org .hamcrest .Matchers .allOf ;
4444import static org .hamcrest .Matchers .equalTo ;
45+ import static org .hamcrest .Matchers .greaterThanOrEqualTo ;
4546import static org .hamcrest .Matchers .is ;
47+ import static org .hamcrest .Matchers .lessThanOrEqualTo ;
4648import static org .hamcrest .Matchers .notNullValue ;
4749
4850public abstract class AbstractIndexCompatibilityTestCase extends ESRestTestCase {
@@ -156,8 +158,16 @@ protected static Version indexVersion(String indexName) throws Exception {
156158 return new Version ((byte ) ((id / 1000000 ) % 100 ), (byte ) ((id / 10000 ) % 100 ), (byte ) ((id / 100 ) % 100 ));
157159 }
158160
161+ protected static int getNumberOfReplicas (String indexName ) throws Exception {
162+ var indexSettings = (Map <?, ?>) ((Map <?, ?>) getIndexSettings (indexName ).get (indexName )).get ("settings" );
163+ var numberOfReplicas = Integer .parseInt ((String ) indexSettings .get (IndexMetadata .SETTING_NUMBER_OF_REPLICAS ));
164+ assertThat (numberOfReplicas , allOf (greaterThanOrEqualTo (0 ), lessThanOrEqualTo (NODES - 1 )));
165+ return numberOfReplicas ;
166+ }
167+
159168 protected static void indexDocs (String indexName , int numDocs ) throws Exception {
160169 var request = new Request ("POST" , "/_bulk" );
170+ request .addParameter ("refresh" , "true" );
161171 var docs = new StringBuilder ();
162172 IntStream .range (0 , numDocs ).forEach (n -> docs .append (Strings .format ("""
163173 {"index":{"_index":"%s"}}
@@ -185,19 +195,30 @@ protected static void mountIndex(String repository, String snapshot, String inde
185195 }
186196
187197 protected static void restoreIndex (String repository , String snapshot , String indexName , String renamedIndexName ) throws Exception {
198+ restoreIndex (repository , snapshot , indexName , renamedIndexName , Settings .EMPTY );
199+ }
200+
201+ protected static void restoreIndex (
202+ String repository ,
203+ String snapshot ,
204+ String indexName ,
205+ String renamedIndexName ,
206+ Settings indexSettings
207+ ) throws Exception {
188208 var request = new Request ("POST" , "/_snapshot/" + repository + "/" + snapshot + "/_restore" );
189209 request .addParameter ("wait_for_completion" , "true" );
190- request .setJsonEntity (org . elasticsearch . common . Strings .format ("""
210+ request .setJsonEntity (Strings .format ("""
191211 {
192212 "indices": "%s",
193213 "include_global_state": false,
194214 "rename_pattern": "(.+)",
195215 "rename_replacement": "%s",
196- "include_aliases": false
197- }""" , indexName , renamedIndexName ));
216+ "include_aliases": false,
217+ "index_settings": %s
218+ }""" , indexName , renamedIndexName , Strings .toString (indexSettings )));
198219 var responseBody = createFromResponse (client ().performRequest (request ));
199- assertThat (responseBody .evaluate ("snapshot.shards.total" ), equalTo ((int ) responseBody .evaluate ("snapshot.shards.failed " )));
200- assertThat (responseBody .evaluate ("snapshot.shards.successful " ), equalTo (0 ));
220+ assertThat (responseBody .evaluate ("snapshot.shards.total" ), equalTo ((int ) responseBody .evaluate ("snapshot.shards.successful " )));
221+ assertThat (responseBody .evaluate ("snapshot.shards.failed " ), equalTo (0 ));
201222 }
202223
203224 protected static void updateRandomIndexSettings (String indexName ) throws IOException {
@@ -215,20 +236,19 @@ protected static void updateRandomIndexSettings(String indexName) throws IOExcep
215236 updateIndexSettings (indexName , settings );
216237 }
217238
218- protected static void updateRandomMappings (String indexName ) throws IOException {
239+ protected static void updateRandomMappings (String indexName ) throws Exception {
219240 final var runtime = new HashMap <>();
220241 runtime .put ("field_" + randomInt (2 ), Map .of ("type" , "keyword" ));
221242 final var properties = new HashMap <>();
222243 properties .put (randomIdentifier (), Map .of ("type" , "long" ));
223- var body = XContentTestUtils .convertToXContent (Map .of ("runtime" , runtime , "properties" , properties ), XContentType .JSON );
244+ updateMappings (indexName , Map .of ("runtime" , runtime , "properties" , properties ));
245+ }
246+
247+ protected static void updateMappings (String indexName , Map <String , ?> mappings ) throws Exception {
248+ var body = XContentTestUtils .convertToXContent (mappings , XContentType .JSON );
224249 var request = new Request ("PUT" , indexName + "/_mappings" );
225250 request .setEntity (
226- new InputStreamEntity (
227- body .streamInput (),
228- body .length (),
229-
230- ContentType .create (XContentType .JSON .mediaTypeWithoutParameters ())
231- )
251+ new InputStreamEntity (body .streamInput (), body .length (), ContentType .create (XContentType .JSON .mediaTypeWithoutParameters ()))
232252 );
233253 assertOK (client ().performRequest (request ));
234254 }
@@ -238,4 +258,14 @@ protected static boolean isIndexClosed(String indexName) throws Exception {
238258 var state = responseBody .evaluate ("metadata.indices." + indexName + ".state" );
239259 return IndexMetadata .State .fromString ((String ) state ) == IndexMetadata .State .CLOSE ;
240260 }
261+
262+ protected static void addIndexWriteBlock (String indexName ) throws Exception {
263+ assertAcknowledged (client ().performRequest (new Request ("PUT" , Strings .format ("/%s/_block/write" , indexName ))));
264+ }
265+
266+ protected static void forceMerge (String indexName , int maxNumSegments ) throws Exception {
267+ var request = new Request ("POST" , '/' + indexName + "/_forcemerge" );
268+ request .addParameter ("max_num_segments" , String .valueOf (maxNumSegments ));
269+ assertOK (client ().performRequest (request ));
270+ }
241271}
0 commit comments