Aggregations: Forbid usage of aggregations in conjunction with search_type=SCAN.

Aggregations are collection-wide statistics, which is incompatible with the
collection mode of search_type=SCAN since it doesn't collect all matches on
calls to the search API.

Close #7429
This commit is contained in:
Adrien Grand 2014-08-28 12:20:59 +02:00
parent 203e80e650
commit 4bfad644b3
3 changed files with 23 additions and 7 deletions

View File

@ -115,6 +115,9 @@ A scanning scroll request differs from a standard scroll request in three
ways:
* Sorting is disabled. Results are returned in the order they appear in the index.
* Aggregations are not supported.
* The response of the initial `search` request will not contain any results in
the `hits` array. The first results will be returned by the first `scroll`
request.

View File

@ -27,6 +27,7 @@ import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.index.NumericDocValues;
import org.apache.lucene.search.TopDocs;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.action.search.SearchType;
@ -69,11 +70,8 @@ import org.elasticsearch.search.dfs.CachedDfSource;
import org.elasticsearch.search.dfs.DfsPhase;
import org.elasticsearch.search.dfs.DfsSearchResult;
import org.elasticsearch.search.fetch.*;
import org.elasticsearch.search.internal.DefaultSearchContext;
import org.elasticsearch.search.internal.InternalScrollSearchRequest;
import org.elasticsearch.search.internal.SearchContext;
import org.elasticsearch.search.internal.*;
import org.elasticsearch.search.internal.SearchContext.Lifetime;
import org.elasticsearch.search.internal.ShardSearchRequest;
import org.elasticsearch.search.query.*;
import org.elasticsearch.search.warmer.IndexWarmersMetaData;
import org.elasticsearch.threadpool.ThreadPool;
@ -206,10 +204,14 @@ public class SearchService extends AbstractLifecycleComponent<SearchService> {
public QuerySearchResult executeScan(ShardSearchRequest request) throws ElasticsearchException {
SearchContext context = createAndPutContext(request);
try {
if (context.aggregations() != null) {
throw new ElasticsearchIllegalArgumentException("aggregations are not supported with search_type=scan");
}
assert context.searchType() == SearchType.SCAN;
context.searchType(SearchType.COUNT); // move to COUNT, and then, when scrolling, move to SCAN
assert context.searchType() == SearchType.COUNT;
try {
if (context.scroll() == null) {
throw new ElasticsearchException("Scroll must be provided when scanning...");
}

View File

@ -21,7 +21,9 @@ package org.elasticsearch.search.aggregations;
import com.google.common.collect.Lists;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchPhaseExecutionException;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
@ -50,6 +52,15 @@ public class AggregationsIntegrationTests extends ElasticsearchIntegrationTest {
indexRandom(true, docs);
}
public void testScan() {
try {
client().prepareSearch("index").setSearchType(SearchType.SCAN).setScroll(new TimeValue(500)).addAggregation(terms("f").field("f")).get();
fail();
} catch (SearchPhaseExecutionException e) {
assertTrue(e.getMessage(), e.getMessage().contains("aggregations are not supported with search_type=scan"));
}
}
public void testScroll() {
final int size = randomIntBetween(1, 4);
SearchResponse response = client().prepareSearch("index")