Skip to content

Commit

Permalink
SpotlessCheck Signed-off-by: Aparajita Pandey <aparajita31pandey@gmai…
Browse files Browse the repository at this point in the history
…l.com>

Signed-off-by: Aparajita Pandey <[email protected]>
  • Loading branch information
aparajita31pandey committed Oct 4, 2024
1 parent 7e89567 commit 303f4b5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,9 @@ protected String makeRequest(String query, int fetch_size) {
}

protected String makeRequest(String query, int fetch_size, String filterQuery) {
return String.format("{ \"fetch_size\": \"%s\", \"query\": \"%s\", \"filter\" : %s }", fetch_size, query, filterQuery);
return String.format(
"{ \"fetch_size\": \"%s\", \"query\": \"%s\", \"filter\" : %s }",
fetch_size, query, filterQuery);
}

protected String makeFetchLessRequest(String query) {
Expand Down
32 changes: 17 additions & 15 deletions integ-test/src/test/java/org/opensearch/sql/sql/PaginationIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import org.junit.Ignore;
import org.junit.Test;
import org.opensearch.client.Request;
import org.opensearch.client.Response;
import org.opensearch.client.RequestOptions;
import org.opensearch.client.Response;
import org.opensearch.client.ResponseException;
import org.opensearch.sql.common.setting.Settings;
import org.opensearch.sql.legacy.SQLIntegTestCase;
Expand Down Expand Up @@ -217,42 +217,44 @@ public void testQueryWithoutFrom() {
assertEquals(1, response.getInt("total"));
assertEquals(1, response.getJSONArray("datarows").getJSONArray(0).getInt(0));
}

@Test
public void testAlias() throws Exception {
String indexName = Index.ONLINE.getName();
String aliasName = "alias_ONLINE";
String filterQuery = "{\n" +
" \"term\": {\n" +
" \"107\": 72 \n" +
" }\n" +
"}";
String filterQuery = "{\n" + " \"term\": {\n" + " \"107\": 72 \n" + " }\n" + "}";

//Execute the SQL query with filter
// Execute the SQL query with filter
String selectQuery = "SELECT * FROM " + TEST_INDEX_ONLINE;
JSONObject initialResponse = new JSONObject(executeFetchQuery(selectQuery, 10, "jdbc", filterQuery));
JSONObject initialResponse =
new JSONObject(executeFetchQuery(selectQuery, 10, "jdbc", filterQuery));
assertEquals(initialResponse.getInt("size"), 10);

//Create an alias
String createAliasQuery = String.format("{ \"actions\": [ { \"add\": { \"index\": \"%s\", \"alias\": \"%s\" } } ] }", indexName, aliasName);
// Create an alias
String createAliasQuery =
String.format(
"{ \"actions\": [ { \"add\": { \"index\": \"%s\", \"alias\": \"%s\" } } ] }",
indexName, aliasName);
Request createAliasRequest = new Request("POST", "/_aliases");
createAliasRequest.setJsonEntity(createAliasQuery);
JSONObject aliasResponse = new JSONObject(executeRequest(createAliasRequest));

// Assert that alias creation was acknowledged
assertTrue(aliasResponse.getBoolean("acknowledged"));

//Query using the alias
// Query using the alias
String aliasSelectQuery = String.format("SELECT * FROM %s", aliasName);
JSONObject aliasQueryResponse = new JSONObject(executeFetchQuery(aliasSelectQuery, 4, "jdbc"));
assertEquals(4, aliasQueryResponse.getInt("size"));

//Query using the alias with filter
JSONObject aliasFilteredResponse = new JSONObject(executeFetchQuery(aliasSelectQuery, 4, "jdbc", filterQuery));
// Query using the alias with filter
JSONObject aliasFilteredResponse =
new JSONObject(executeFetchQuery(aliasSelectQuery, 4, "jdbc", filterQuery));
assertEquals(aliasFilteredResponse.getInt("size"), 4);
}


private String executeFetchQuery(String query, int fetchSize, String requestType, String filter) throws IOException{
private String executeFetchQuery(String query, int fetchSize, String requestType, String filter)
throws IOException {
String endpoint = "/_plugins/_sql?format=" + requestType;
String requestBody = makeRequest(query, fetchSize, filter);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private void loadFromEsState(Query query) {
String indexName = fetchIndexName(query);
String[] fieldNames = fetchFieldsAsArray(query);
GetAliasesResponse getAliasesResponse =
client.admin().indices().getAliases(new GetAliasesRequest(indexName)).actionGet();
client.admin().indices().getAliases(new GetAliasesRequest(indexName)).actionGet();
if (getAliasesResponse != null && !getAliasesResponse.getAliases().isEmpty()) {
indexName = getAliasesResponse.getAliases().keySet().iterator().next();
}
Expand Down

0 comments on commit 303f4b5

Please sign in to comment.