Skip to content

Commit

Permalink
Merge pull request #12150 from martijnvg/aliases/remove_strict_filter…
Browse files Browse the repository at this point in the history
…_parsing

Don't require fields in alias filters to exist in the mapping
  • Loading branch information
martijnvg committed Jul 10, 2015
2 parents 6c0badd + aa22e23 commit 9eb1126
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 38 deletions.
Expand Up @@ -145,7 +145,6 @@ private void validateAliasFilter(XContentParser parser, IndexQueryParserService
QueryParseContext context = indexQueryParserService.getParseContext();
try {
context.reset(parser);
context.setAllowUnmappedFields(false);
context.parseInnerFilter();
} finally {
context.reset(null);
Expand Down
34 changes: 12 additions & 22 deletions core/src/test/java/org/elasticsearch/aliases/IndexAliasesTests.java
Expand Up @@ -29,7 +29,6 @@
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.AliasAction;
import org.elasticsearch.cluster.metadata.AliasMetaData;
Expand All @@ -39,7 +38,6 @@
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryParsingException;
import org.elasticsearch.rest.action.admin.indices.alias.delete.AliasesMissingException;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
Expand Down Expand Up @@ -909,29 +907,21 @@ public void testCreateIndexWithAliasesFilterNotValid() {
}

@Test
// Before 2.0 alias filters were parsed at alias creation time, in order
// for filters to work correctly ES required that fields mentioned in those
// filters exist in the mapping.
// From 2.0 and higher alias filters are parsed at request time and therefor
// fields mentioned in filters don't need to exist in the mapping.
public void testAddAliasWithFilterNoMapping() throws Exception {
assertAcked(prepareCreate("test"));

try {
client().admin().indices().prepareAliases()
.addAlias("test", "a", QueryBuilders.termQuery("field1", "term"))
.get();
fail();
} catch (IllegalArgumentException e) {
assertThat(e.getCause(), instanceOf(QueryParsingException.class));
}

try {
client().admin().indices().prepareAliases()
.addAlias("test", "a", QueryBuilders.rangeQuery("field2").from(0).to(1))
.get();
fail();
} catch (IllegalArgumentException e) {
assertThat(e.getCause(), instanceOf(QueryParsingException.class));
}

client().admin().indices().prepareAliases()
.addAlias("test", "a", QueryBuilders.matchAllQuery()) // <-- no fail, b/c no field mentioned
.addAlias("test", "a", QueryBuilders.termQuery("field1", "term"))
.get();
client().admin().indices().prepareAliases()
.addAlias("test", "a", QueryBuilders.rangeQuery("field2").from(0).to(1))
.get();
client().admin().indices().prepareAliases()
.addAlias("test", "a", QueryBuilders.matchAllQuery())
.get();
}

Expand Down
Expand Up @@ -20,9 +20,7 @@

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesResponse;
Expand All @@ -33,7 +31,6 @@
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.cluster.metadata.AliasMetaData;
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
Expand All @@ -44,12 +41,10 @@
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*;
import static org.hamcrest.Matchers.*;
Expand Down Expand Up @@ -659,17 +654,21 @@ public void testStrictAliasParsingInIndicesCreatedViaTemplates() throws Exceptio
assertThat(response.getItems()[0].getId(), equalTo("test"));
assertThat(response.getItems()[0].getVersion(), equalTo(1l));

try {
client().prepareIndex("d1", "test", "test").setSource("{}").get();
fail();
} catch (Exception e) {
assertThat(ExceptionsHelper.unwrapCause(e), instanceOf(IllegalArgumentException.class));
assertThat(e.getMessage(), containsString("failed to parse filter for alias [alias4]"));
}
// Before 2.0 alias filters were parsed at alias creation time, in order
// for filters to work correctly ES required that fields mentioned in those
// filters exist in the mapping.
// From 2.0 and higher alias filters are parsed at request time and therefor
// fields mentioned in filters don't need to exist in the mapping.
// So the aliases defined in the index template for this index will not fail
// even though the fields in the alias fields don't exist yet and indexing into
// an index that doesn't exist yet will succeed
client().prepareIndex("d1", "test", "test").setSource("{}").get();

response = client().prepareBulk().add(new IndexRequest("d2", "test", "test").source("{}")).get();
assertThat(response.hasFailures(), is(true));
assertThat(response.getItems()[0].isFailed(), equalTo(true));
assertThat(response.getItems()[0].getFailureMessage(), containsString("failed to parse filter for alias [alias4]"));
assertThat(response.hasFailures(), is(false));
assertThat(response.getItems()[0].isFailed(), equalTo(false));
assertThat(response.getItems()[0].getId(), equalTo("test"));
assertThat(response.getItems()[0].getVersion(), equalTo(1l));
}

}
5 changes: 5 additions & 0 deletions docs/reference/migration/migrate_2_0.asciidoc
Expand Up @@ -770,3 +770,8 @@ For the record, official plugins which can use this new simplified form are:
* elasticsearch-lang-javascript
* elasticsearch-lang-python

=== Aliases

Fields used in alias filters no longer have to exist in the mapping upon alias creation time. Alias filters are now
parsed at request time and then the fields in filters are resolved from the mapping, whereas before alias filters were
parsed at alias creation time and the parsed form was kept around in memory.

0 comments on commit 9eb1126

Please sign in to comment.