Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,43 @@ public void testAlias() throws Exception {
assertEquals(aliasFilteredResponse.getInt("size"), 4);
}

@Test
public void testAliasResultConsistency() throws Exception {
String indexName = Index.ONLINE.getName();
String aliasName = "alias_ONLINE_consistency";

// 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));
assertTrue(aliasResponse.getBoolean("acknowledged"));

// Query using direct index
String directQuery = String.format("SELECT * FROM %s", TEST_INDEX_ONLINE);
JSONObject directResponse = new JSONObject(executeFetchQuery(directQuery, 10, "jdbc"));

// Query using alias
String aliasQuery = String.format("SELECT * FROM %s", aliasName);
JSONObject aliasQueryResponse = new JSONObject(executeFetchQuery(aliasQuery, 10, "jdbc"));

assertEquals(directResponse.getInt("size"), aliasQueryResponse.getInt("size"));
assertTrue(
directResponse.getJSONArray("schema").similar(aliasQueryResponse.getJSONArray("schema")));

// Clean up alias
String deleteAliasQuery =
String.format(
"{ \"actions\": [ { \"remove\": { \"index\": \"%s\", \"alias\": \"%s\" } } ] }",
indexName, aliasName);
Request deleteAliasRequest = new Request("POST", "/_aliases");
deleteAliasRequest.setJsonEntity(deleteAliasQuery);
executeRequest(deleteAliasRequest);
}

private String executeFetchQuery(String query, int fetchSize, String requestType, String filter)
throws IOException {
String endpoint = "/_plugins/_sql?format=" + requestType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import java.util.stream.StreamSupport;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.action.admin.indices.alias.get.GetAliasesRequest;
import org.opensearch.action.admin.indices.alias.get.GetAliasesResponse;
import org.opensearch.action.admin.indices.mapping.get.GetFieldMappingsRequest;
import org.opensearch.action.admin.indices.mapping.get.GetFieldMappingsResponse;
import org.opensearch.common.document.DocumentField;
Expand Down Expand Up @@ -163,11 +161,6 @@ private void populateResultSetFromDefaultCursor(DefaultCursor cursor) {
private void loadFromEsState(Query query) {
String indexName = fetchIndexName(query);
String[] fieldNames = fetchFieldsAsArray(query);
GetAliasesResponse getAliasesResponse =
client.admin().indices().getAliases(new GetAliasesRequest(indexName)).actionGet();
if (getAliasesResponse != null && !getAliasesResponse.getAliases().isEmpty()) {
indexName = getAliasesResponse.getAliases().keySet().iterator().next();
}
// Reset boolean in the case of JOIN query where multiple calls to loadFromEsState() are made
selectAll = isSimpleQuerySelectAll(query) || isJoinQuerySelectAll(query, fieldNames);

Expand All @@ -180,11 +173,13 @@ private void loadFromEsState(Query query) {
client.admin().indices().getFieldMappings(request).actionGet();

Map<String, Map<String, FieldMappingMetadata>> mappings = response.mappings();
if (mappings.isEmpty() || !mappings.containsKey(indexName)) {
if (mappings.size() != 1) {
Copy link
Collaborator

@dai-chen dai-chen Jan 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you double check if multiple entires can be returned in certain case? Just want to make sure this check is not too strict and fail in valid case.

# Create test-index-1, 2, 3
curl -X PUT "localhost:9200/test-index-1" -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "properties": {
      "name": { "type": "text" },
      "age": { "type": "integer" },
      "timestamp": { "type": "date" }
    }
  }
}'

...

curl -X POST "localhost:9200/_aliases" -H 'Content-Type: application/json' -d'
{
  "actions": [
    { "add": { "index": "test-index-*", "alias": "wildcard-alias" } }
  ]
}'

curl -X GET "localhost:9200/wildcard-alias/_mapping?pretty"
{
  "test-index-1" : {
    ...
  },
  "test-index-2" : {
    ...
  },
  "test-index-3" : {
    ...
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @aparajita31pandey, could you take a look at this? thanks!

throw new IllegalArgumentException(
String.format("Index type %s does not exist", query.getFrom()));
String.format(
"Expected exactly one index mapping for %s, but found %d",
indexName, mappings.size()));
}
Map<String, FieldMappingMetadata> typeMappings = mappings.get(indexName);
Map<String, FieldMappingMetadata> typeMappings = mappings.values().iterator().next();

this.indexName = this.indexName == null ? indexName : (this.indexName + "|" + indexName);
this.columns.addAll(
Expand Down
Loading