Fall back to local statistics if global statistics are not availalbe for a field or term

Closes #2926
This commit is contained in:
Simon Willnauer 2013-04-23 12:08:56 +02:00
parent f372f7c109
commit c884304753
3 changed files with 57 additions and 5 deletions

View File

@ -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,7 +62,8 @@ 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;
}

View File

@ -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;

View File

@ -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;
@ -1205,4 +1210,51 @@ 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));
}
}