use of org.opensearch.indices.InvalidAliasNameException in project OpenSearch by opensearch-project.
the class SimpleIndexTemplateIT method testAliasNameExistingIndex.
public void testAliasNameExistingIndex() throws Exception {
createIndex("index");
client().admin().indices().preparePutTemplate("template_1").setPatterns(Collections.singletonList("te*")).addAlias(new Alias("index")).get();
InvalidAliasNameException e = expectThrows(InvalidAliasNameException.class, () -> createIndex("test"));
assertThat(e.getMessage(), equalTo("Invalid alias name [index], an index exists with the same name as the alias"));
}
use of org.opensearch.indices.InvalidAliasNameException in project OpenSearch by opensearch-project.
the class ShardSearchRequest method parseAliasFilter.
/**
* Returns the filter associated with listed filtering aliases.
* <p>
* The list of filtering aliases should be obtained by calling Metadata.filteringAliases.
* Returns {@code null} if no filtering is required.</p>
*/
public static QueryBuilder parseAliasFilter(CheckedFunction<BytesReference, QueryBuilder, IOException> filterParser, IndexMetadata metadata, String... aliasNames) {
if (aliasNames == null || aliasNames.length == 0) {
return null;
}
Index index = metadata.getIndex();
ImmutableOpenMap<String, AliasMetadata> aliases = metadata.getAliases();
Function<AliasMetadata, QueryBuilder> parserFunction = (alias) -> {
if (alias.filter() == null) {
return null;
}
try {
return filterParser.apply(alias.filter().uncompressed());
} catch (IOException ex) {
throw new AliasFilterParsingException(index, alias.getAlias(), "Invalid alias filter", ex);
}
};
if (aliasNames.length == 1) {
AliasMetadata alias = aliases.get(aliasNames[0]);
if (alias == null) {
// This shouldn't happen unless alias disappeared after filteringAliases was called.
throw new InvalidAliasNameException(index, aliasNames[0], "Unknown alias name was passed to alias Filter");
}
return parserFunction.apply(alias);
} else {
// we need to bench here a bit, to see maybe it makes sense to use OrFilter
BoolQueryBuilder combined = new BoolQueryBuilder();
for (String aliasName : aliasNames) {
AliasMetadata alias = aliases.get(aliasName);
if (alias == null) {
// This shouldn't happen unless alias disappeared after filteringAliases was called.
throw new InvalidAliasNameException(index, aliasNames[0], "Unknown alias name was passed to alias Filter");
}
QueryBuilder parsedFilter = parserFunction.apply(alias);
if (parsedFilter != null) {
combined.should(parsedFilter);
} else {
// The filter might be null only if filter was removed after filteringAliases was called
return null;
}
}
return combined;
}
}
use of org.opensearch.indices.InvalidAliasNameException in project OpenSearch by opensearch-project.
the class ShardSearchRequestTests method testRemovedAliasFilter.
public void testRemovedAliasFilter() throws Exception {
IndexMetadata indexMetadata = BASE_METADATA;
indexMetadata = add(indexMetadata, "cats", filter(termQuery("animal", "cat")));
indexMetadata = remove(indexMetadata, "cats");
try {
aliasFilter(indexMetadata, "cats");
fail("Expected InvalidAliasNameException");
} catch (InvalidAliasNameException e) {
assertThat(e.getMessage(), containsString("Invalid alias name [cats]"));
}
}
Aggregations