@@ -46,12 +46,29 @@ public class TestUtils {
4646 */
4747 public static void createIndexByRestClient (RestClient client , String indexName , String mapping ) {
4848 Request request = new Request ("PUT" , "/" + indexName );
49- if (! isNullOrEmpty (mapping )) {
50- request . setJsonEntity ( mapping );
51- }
49+ JSONObject jsonObject = isNullOrEmpty (mapping ) ? new JSONObject () : new JSONObject ( mapping );
50+ setZeroReplicas ( jsonObject );
51+ request . setJsonEntity ( jsonObject . toString ());
5252 performRequest (client , request );
5353 }
5454
55+ /**
56+ * Sets number_of_replicas to 0 in the index settings. This makes multi-node behavior consistent
57+ * (<a href="https://github.com/opensearch-project/sql/issues/4261">4261</a>) and prevents tests
58+ * from hanging on single-node clusters when using wait_for_active_shards=all.
59+ *
60+ * @param jsonObject the index creation JSON object to modify
61+ */
62+ private static void setZeroReplicas (JSONObject jsonObject ) {
63+ JSONObject settings =
64+ jsonObject .has ("settings" ) ? jsonObject .getJSONObject ("settings" ) : new JSONObject ();
65+ JSONObject indexSettings =
66+ settings .has ("index" ) ? settings .getJSONObject ("index" ) : new JSONObject ();
67+ indexSettings .put ("number_of_replicas" , 0 );
68+ settings .put ("index" , indexSettings );
69+ jsonObject .put ("settings" , settings );
70+ }
71+
5572 /**
5673 * https://github.com/elastic/elasticsearch/pull/49959<br>
5774 * Deprecate creation of dot-prefixed index names except for hidden and system indices. Create
@@ -99,7 +116,8 @@ public static boolean isIndexExist(RestClient client, String indexName) {
99116 public static void loadDataByRestClient (
100117 RestClient client , String indexName , String dataSetFilePath ) throws IOException {
101118 Path path = Paths .get (getResourceFilePath (dataSetFilePath ));
102- Request request = new Request ("POST" , "/" + indexName + "/_bulk?refresh=true" );
119+ Request request =
120+ new Request ("POST" , "/" + indexName + "/_bulk?refresh=wait_for&wait_for_active_shards=all" );
103121 request .setJsonEntity (new String (Files .readAllBytes (path )));
104122 performRequest (client , request );
105123 }
0 commit comments