From 6fd74fa39e1e77d2ac7e4bc2a7a2272b1d2df8ba Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Sat, 22 Jun 2013 19:41:02 +0200 Subject: [PATCH] Terms Filter Lookup: Failure when no mappings for the terms field exists (no data indexed) closes #3216 --- .../org/elasticsearch/index/query/TermsFilterParser.java | 8 +++++++- .../test/integration/search/query/SimpleQueryTests.java | 9 +++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/elasticsearch/index/query/TermsFilterParser.java b/src/main/java/org/elasticsearch/index/query/TermsFilterParser.java index 94264cdfd2d..4917f64dfaa 100644 --- a/src/main/java/org/elasticsearch/index/query/TermsFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/TermsFilterParser.java @@ -155,8 +155,14 @@ public class TermsFilterParser implements FilterParser { previousTypes = QueryParseContext.setTypesWithPrevious(new String[]{smartNameFieldMappers.docMapper().type()}); } } - + if (lookupId != null) { + // if there are no mappings, then nothing has been indexing yet against this shard, so we can return + // no match (but not cached!), since the Terms Lookup relies on the fact that there are mappings... + if (fieldMapper == null) { + return Queries.MATCH_NO_FILTER; + } + // external lookup, use it TermsLookup termsLookup = new TermsLookup(fieldMapper, lookupIndex, lookupType, lookupId, lookupPath, parseContext); if (cacheKey == null) { diff --git a/src/test/java/org/elasticsearch/test/integration/search/query/SimpleQueryTests.java b/src/test/java/org/elasticsearch/test/integration/search/query/SimpleQueryTests.java index 85c7e7802c2..2270a9811ef 100644 --- a/src/test/java/org/elasticsearch/test/integration/search/query/SimpleQueryTests.java +++ b/src/test/java/org/elasticsearch/test/integration/search/query/SimpleQueryTests.java @@ -31,7 +31,6 @@ import org.elasticsearch.index.query.MatchQueryBuilder.Type; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.facet.FacetBuilders; import org.elasticsearch.test.integration.AbstractSharedClusterTest; -import org.elasticsearch.common.xcontent.XContentBuilder; import org.testng.annotations.Test; import java.io.IOException; @@ -57,7 +56,7 @@ public class SimpleQueryTests extends AbstractSharedClusterTest { public int numberOfNodes() { return 4; } - + @Test // see https://github.com/elasticsearch/elasticsearch/issues/3177 public void testIssue3177() { run(prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1))); @@ -933,6 +932,12 @@ public class SimpleQueryTests extends AbstractSharedClusterTest { assertThat(searchResponse.getHits().getTotalHits(), equalTo(2l)); assertThat(searchResponse.getHits().getHits()[0].getId(), anyOf(equalTo("2"), equalTo("4"))); assertThat(searchResponse.getHits().getHits()[1].getId(), anyOf(equalTo("2"), equalTo("4"))); + + searchResponse = client().prepareSearch("test") + .setQuery(filteredQuery(matchAllQuery(), termsLookupFilter("not_exists").lookupIndex("lookup2").lookupType("type").lookupId("3").lookupPath("arr.term")) + ).execute().actionGet(); + assertThat("Failures " + Arrays.toString(searchResponse.getShardFailures()), searchResponse.getShardFailures().length, equalTo(0)); + assertThat(searchResponse.getHits().getTotalHits(), equalTo(0l)); } @Test