diff --git a/src/main/java/org/elasticsearch/search/dfs/CachedDfSource.java b/src/main/java/org/elasticsearch/search/dfs/CachedDfSource.java index e28ff78268c..11b8ec77202 100644 --- a/src/main/java/org/elasticsearch/search/dfs/CachedDfSource.java +++ b/src/main/java/org/elasticsearch/search/dfs/CachedDfSource.java @@ -23,7 +23,6 @@ import org.apache.lucene.document.Document; import org.apache.lucene.index.*; import org.apache.lucene.search.*; import org.apache.lucene.search.similarities.Similarity; -import org.elasticsearch.ElasticSearchIllegalArgumentException; import java.io.IOException; import java.util.List; @@ -53,7 +52,8 @@ public class CachedDfSource extends IndexSearcher { public TermStatistics termStatistics(Term term, TermContext context) throws IOException { TermStatistics termStatistics = aggregatedDfs.termStatistics().get(term); if (termStatistics == null) { - throw new ElasticSearchIllegalArgumentException("Not distributed term statistics for term: " + term); + // we don't have stats for this - this might be a must_not clauses etc. that doesn't allow extract terms on the query + return super.termStatistics(term, context); } return termStatistics; } @@ -62,11 +62,12 @@ public class CachedDfSource extends IndexSearcher { public CollectionStatistics collectionStatistics(String field) throws IOException { CollectionStatistics collectionStatistics = aggregatedDfs.fieldStatistics().get(field); if (collectionStatistics == null) { - throw new ElasticSearchIllegalArgumentException("Not distributed collection statistics for field: " + field); + // we don't have stats for this - this might be a must_not clauses etc. that doesn't allow extract terms on the query + return super.collectionStatistics(field); } return collectionStatistics; } - + public int maxDoc() { return this.maxDoc; } diff --git a/src/main/java/org/elasticsearch/search/query/QueryPhase.java b/src/main/java/org/elasticsearch/search/query/QueryPhase.java index 3d39695ccc1..dbc6379685c 100644 --- a/src/main/java/org/elasticsearch/search/query/QueryPhase.java +++ b/src/main/java/org/elasticsearch/search/query/QueryPhase.java @@ -32,7 +32,6 @@ import org.elasticsearch.search.facet.FacetPhase; import org.elasticsearch.search.internal.ContextIndexSearcher; import org.elasticsearch.search.internal.SearchContext; import org.elasticsearch.search.rescore.RescorePhase; -import org.elasticsearch.search.rescore.RescoreSearchContext; import org.elasticsearch.search.sort.SortParseElement; import org.elasticsearch.search.sort.TrackScoresParseElement; import org.elasticsearch.search.suggest.SuggestPhase; 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 6ec0e866752..7c742bf7c1d 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 @@ -19,14 +19,18 @@ package org.elasticsearch.test.integration.search.query; +import org.elasticsearch.ElasticSearchException; +import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.action.search.SearchPhaseExecutionException; import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.action.search.SearchType; import org.elasticsearch.client.Client; import org.elasticsearch.common.Priority; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.index.query.*; import org.elasticsearch.index.query.CommonTermsQueryBuilder.Operator; +import org.elasticsearch.index.query.MatchQueryBuilder.Type; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.facet.FacetBuilders; import org.elasticsearch.test.integration.AbstractNodesTests; @@ -34,6 +38,7 @@ import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; +import java.io.IOException; import java.util.Arrays; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; @@ -1204,5 +1209,52 @@ public class SimpleQueryTests extends AbstractNodesTests { assertThat(response.getHits().totalHits(), equalTo(4l)); } + + @Test // see #2926 + public void testMustNot() throws ElasticSearchException, IOException { + client.admin().indices().prepareDelete().execute().actionGet(); + client.admin().indices().prepareCreate("test").setSettings( + ImmutableSettings.settingsBuilder() + .put("index.number_of_shards", 2) + .put("index.number_of_replicas", 0) + ) + .execute().actionGet(); + + client.prepareIndex("test", "test", "1").setSource(jsonBuilder().startObject() + .field("description", "foo other anything bar") + .endObject()) + .execute().actionGet(); + + client.prepareIndex("test", "test", "2").setSource(jsonBuilder().startObject() + .field("description", "foo other anything") + .endObject()) + .execute().actionGet(); + + client.prepareIndex("test", "test", "3").setSource(jsonBuilder().startObject() + .field("description", "foo other") + .endObject()) + .execute().actionGet(); + + client.prepareIndex("test", "test", "4").setSource(jsonBuilder().startObject() + .field("description", "foo") + .endObject()) + .execute().actionGet(); + + client.admin().indices().prepareRefresh().execute().actionGet(); + + SearchResponse response = client.prepareSearch("test") + .setQuery(QueryBuilders.matchAllQuery()) + .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) + .execute().actionGet(); + assertThat(response.getShardFailures().length, equalTo(0)); + assertThat(response.getHits().totalHits(), equalTo(4l)); + + response = client.prepareSearch("test").setQuery( + QueryBuilders.boolQuery() + .mustNot(QueryBuilders.matchQuery("description", "anything").type(Type.BOOLEAN)) + ).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).execute().actionGet(); + assertThat(response.getShardFailures().length, equalTo(0)); + assertThat(response.getHits().totalHits(), equalTo(2l)); + } }