use of org.opensearch.cluster.metadata.AliasMetadata in project OpenSearch by opensearch-project.
the class GetAliasesResponseTests method mutateAliasMetadata.
public static AliasMetadata mutateAliasMetadata(AliasMetadata alias) {
boolean changeAlias = randomBoolean();
AliasMetadata.Builder builder = AliasMetadata.builder(changeAlias ? randomAlphaOfLengthBetween(2, 5) : alias.getAlias());
builder.searchRouting(alias.searchRouting());
builder.indexRouting(alias.indexRouting());
builder.filter(alias.filter());
if (false == changeAlias) {
if (randomBoolean()) {
builder.searchRouting(alias.searchRouting() + randomAlphaOfLengthBetween(1, 3));
} else {
builder.indexRouting(alias.indexRouting() + randomAlphaOfLengthBetween(1, 3));
}
}
return builder.build();
}
use of org.opensearch.cluster.metadata.AliasMetadata in project OpenSearch by opensearch-project.
the class SimpleIndexTemplateIT method testMultipleAliasesPrecedence.
public void testMultipleAliasesPrecedence() throws Exception {
client().admin().indices().preparePutTemplate("template1").setPatterns(Collections.singletonList("*")).setOrder(0).addAlias(new Alias("alias1")).addAlias(new Alias("{index}-alias")).addAlias(new Alias("alias3").filter(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("test")))).addAlias(new Alias("alias4")).get();
client().admin().indices().preparePutTemplate("template2").setPatterns(Collections.singletonList("te*")).setOrder(1).addAlias(new Alias("alias1").routing("test")).addAlias(new Alias("alias3")).get();
assertAcked(prepareCreate("test").addAlias(new Alias("test-alias").searchRouting("test-routing")));
ensureGreen();
GetAliasesResponse getAliasesResponse = client().admin().indices().prepareGetAliases().addIndices("test").get();
assertThat(getAliasesResponse.getAliases().get("test").size(), equalTo(4));
for (AliasMetadata aliasMetadata : getAliasesResponse.getAliases().get("test")) {
assertThat(aliasMetadata.alias(), anyOf(equalTo("alias1"), equalTo("test-alias"), equalTo("alias3"), equalTo("alias4")));
if ("alias1".equals(aliasMetadata.alias())) {
assertThat(aliasMetadata.indexRouting(), equalTo("test"));
assertThat(aliasMetadata.searchRouting(), equalTo("test"));
} else if ("alias3".equals(aliasMetadata.alias())) {
assertThat(aliasMetadata.filter(), nullValue());
} else if ("test-alias".equals(aliasMetadata.alias())) {
assertThat(aliasMetadata.indexRouting(), nullValue());
assertThat(aliasMetadata.searchRouting(), equalTo("test-routing"));
}
}
}
use of org.opensearch.cluster.metadata.AliasMetadata in project OpenSearch by opensearch-project.
the class TransportSimulateIndexTemplateAction method resolveTemplate.
/**
* Take a template and index name as well as state where the template exists, and return a final
* {@link Template} that represents all the resolved Settings, Mappings, and Aliases
*/
public static Template resolveTemplate(final String matchingTemplate, final String indexName, final ClusterState simulatedState, final NamedXContentRegistry xContentRegistry, final IndicesService indicesService, final AliasValidator aliasValidator) throws Exception {
Settings settings = resolveSettings(simulatedState.metadata(), matchingTemplate);
List<Map<String, AliasMetadata>> resolvedAliases = MetadataIndexTemplateService.resolveAliases(simulatedState.metadata(), matchingTemplate);
// create the index with dummy settings in the cluster state so we can parse and validate the aliases
Settings dummySettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(settings).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetadata.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()).build();
final IndexMetadata indexMetadata = IndexMetadata.builder(indexName).settings(dummySettings).build();
final ClusterState tempClusterState = ClusterState.builder(simulatedState).metadata(Metadata.builder(simulatedState.metadata()).put(indexMetadata, true).build()).build();
List<AliasMetadata> aliases = indicesService.withTempIndexService(indexMetadata, tempIndexService -> MetadataCreateIndexService.resolveAndValidateAliases(indexName, Collections.emptySet(), resolvedAliases, tempClusterState.metadata(), aliasValidator, xContentRegistry, // shard id and the current timestamp
tempIndexService.newQueryShardContext(0, null, () -> 0L, null)));
Map<String, AliasMetadata> aliasesByName = aliases.stream().collect(Collectors.toMap(AliasMetadata::getAlias, Function.identity()));
// empty request mapping as the user can't specify any explicit mappings via the simulate api
List<Map<String, Object>> mappings = MetadataCreateIndexService.collectV2Mappings("{}", simulatedState, matchingTemplate, xContentRegistry, indexName);
CompressedXContent mergedMapping = indicesService.<CompressedXContent, Exception>withTempIndexService(indexMetadata, tempIndexService -> {
MapperService mapperService = tempIndexService.mapperService();
for (Map<String, Object> mapping : mappings) {
if (mapping.isEmpty() == false) {
mapperService.merge(MapperService.SINGLE_MAPPING_NAME, mapping, MapperService.MergeReason.INDEX_TEMPLATE);
}
}
DocumentMapper documentMapper = mapperService.documentMapper();
return documentMapper != null ? documentMapper.mappingSource() : null;
});
return new Template(settings, mergedMapping, aliasesByName);
}
use of org.opensearch.cluster.metadata.AliasMetadata in project OpenSearch by opensearch-project.
the class IndicesClientIT method testGetAliasesNonExistentIndexOrAlias.
public void testGetAliasesNonExistentIndexOrAlias() throws IOException {
/*
* This test is quite extensive as this is the only way we can check that we haven't slid out of sync with the server
* because the server renders the xcontent in a spot that is difficult for us to access in a unit test.
*/
String alias = "alias";
String index = "index";
{
GetAliasesRequest getAliasesRequest = new GetAliasesRequest().indices(index);
GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias, highLevelClient().indices()::getAliasAsync);
assertThat(getAliasesResponse.status(), equalTo(RestStatus.NOT_FOUND));
assertThat(getAliasesResponse.getException().getMessage(), equalTo("OpenSearch exception [type=index_not_found_exception, reason=no such index [index]]"));
}
{
GetAliasesRequest getAliasesRequest = new GetAliasesRequest(alias);
GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias, highLevelClient().indices()::getAliasAsync);
assertThat(getAliasesResponse.status(), equalTo(RestStatus.NOT_FOUND));
assertThat(getAliasesResponse.getError(), equalTo("alias [" + alias + "] missing"));
assertThat(getAliasesResponse.getException(), nullValue());
}
createIndex(index, Settings.EMPTY);
client().performRequest(new Request(HttpPut.METHOD_NAME, index + "/_alias/" + alias));
{
GetAliasesRequest getAliasesRequest = new GetAliasesRequest().indices(index, "non_existent_index");
GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias, highLevelClient().indices()::getAliasAsync);
assertThat(getAliasesResponse.getAliases().size(), equalTo(0));
assertThat(getAliasesResponse.status(), equalTo(RestStatus.NOT_FOUND));
assertThat(getAliasesResponse.getError(), nullValue());
assertThat(getAliasesResponse.getException().getMessage(), equalTo("OpenSearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]"));
}
{
GetAliasesRequest getAliasesRequest = new GetAliasesRequest().indices(index, "non_existent_index").aliases(alias);
GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias, highLevelClient().indices()::getAliasAsync);
assertThat(getAliasesResponse.getAliases().size(), equalTo(0));
assertThat(getAliasesResponse.status(), equalTo(RestStatus.NOT_FOUND));
assertThat(getAliasesResponse.getException().getMessage(), equalTo("OpenSearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]"));
}
{
GetAliasesRequest getAliasesRequest = new GetAliasesRequest().indices("non_existent_index*");
GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias, highLevelClient().indices()::getAliasAsync);
assertThat(getAliasesResponse.status(), equalTo(RestStatus.OK));
assertThat(getAliasesResponse.getAliases().size(), equalTo(0));
assertThat(getAliasesResponse.getException(), nullValue());
assertThat(getAliasesResponse.getError(), nullValue());
}
{
GetAliasesRequest getAliasesRequest = new GetAliasesRequest().indices(index).aliases(alias, "non_existent_alias");
GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias, highLevelClient().indices()::getAliasAsync);
assertThat(getAliasesResponse.status(), equalTo(RestStatus.NOT_FOUND));
assertThat(getAliasesResponse.getError(), equalTo("alias [non_existent_alias] missing"));
assertThat(getAliasesResponse.getAliases().size(), equalTo(1));
assertThat(getAliasesResponse.getAliases().get(index).size(), equalTo(1));
AliasMetadata aliasMetadata = getAliasesResponse.getAliases().get(index).iterator().next();
assertThat(aliasMetadata, notNullValue());
assertThat(aliasMetadata.alias(), equalTo(alias));
/*
This is the above response in json format:
{
"error": "alias [something] missing",
"status": 404,
"index": {
"aliases": {
"alias": {}
}
}
}
*/
}
{
GetAliasesRequest getAliasesRequest = new GetAliasesRequest().aliases("non_existent_alias*");
GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias, highLevelClient().indices()::getAliasAsync);
assertThat(getAliasesResponse.status(), equalTo(RestStatus.OK));
assertThat(getAliasesResponse.getAliases().size(), equalTo(0));
}
}
use of org.opensearch.cluster.metadata.AliasMetadata in project OpenSearch by opensearch-project.
the class ClusterClientIT method testComponentTemplates.
public void testComponentTemplates() throws Exception {
String templateName = "my-template";
Settings settings = Settings.builder().put("index.number_of_shards", 1).build();
CompressedXContent mappings = new CompressedXContent("{\"properties\":{\"host_name\":{\"type\":\"keyword\"}}}");
AliasMetadata alias = AliasMetadata.builder("alias").writeIndex(true).build();
Template template = new Template(settings, mappings, Collections.singletonMap("alias", alias));
ComponentTemplate componentTemplate = new ComponentTemplate(template, 1L, new HashMap<>());
PutComponentTemplateRequest putComponentTemplateRequest = new PutComponentTemplateRequest().name(templateName).create(true).componentTemplate(componentTemplate);
AcknowledgedResponse response = execute(putComponentTemplateRequest, highLevelClient().cluster()::putComponentTemplate, highLevelClient().cluster()::putComponentTemplateAsync);
assertThat(response.isAcknowledged(), equalTo(true));
ComponentTemplatesExistRequest componentTemplatesExistRequest = new ComponentTemplatesExistRequest(templateName);
boolean exist = execute(componentTemplatesExistRequest, highLevelClient().cluster()::existsComponentTemplate, highLevelClient().cluster()::existsComponentTemplateAsync);
assertTrue(exist);
GetComponentTemplatesRequest getComponentTemplatesRequest = new GetComponentTemplatesRequest(templateName);
GetComponentTemplatesResponse getResponse = execute(getComponentTemplatesRequest, highLevelClient().cluster()::getComponentTemplate, highLevelClient().cluster()::getComponentTemplateAsync);
assertThat(getResponse.getComponentTemplates().size(), equalTo(1));
assertThat(getResponse.getComponentTemplates().containsKey(templateName), equalTo(true));
assertThat(getResponse.getComponentTemplates().get(templateName), equalTo(componentTemplate));
DeleteComponentTemplateRequest deleteComponentTemplateRequest = new DeleteComponentTemplateRequest(templateName);
response = execute(deleteComponentTemplateRequest, highLevelClient().cluster()::deleteComponentTemplate, highLevelClient().cluster()::deleteComponentTemplateAsync);
assertThat(response.isAcknowledged(), equalTo(true));
OpenSearchStatusException statusException = expectThrows(OpenSearchStatusException.class, () -> execute(getComponentTemplatesRequest, highLevelClient().cluster()::getComponentTemplate, highLevelClient().cluster()::getComponentTemplateAsync));
assertThat(statusException.status(), equalTo(RestStatus.NOT_FOUND));
exist = execute(componentTemplatesExistRequest, highLevelClient().cluster()::existsComponentTemplate, highLevelClient().cluster()::existsComponentTemplateAsync);
assertFalse(exist);
}
Aggregations