count doesn't work for empty query (discrepancy with search API), closes #1377.

This commit is contained in:
Shay Banon 2011-10-06 21:57:19 +02:00
parent 768c6d2922
commit 4bbe1b111a
3 changed files with 14 additions and 5 deletions

View File

@ -22,7 +22,6 @@ package org.elasticsearch.action.count;
import org.apache.lucene.util.UnicodeUtil;
import org.elasticsearch.ElasticSearchGenerationException;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.Actions;
import org.elasticsearch.action.support.broadcast.BroadcastOperationRequest;
import org.elasticsearch.action.support.broadcast.BroadcastOperationThreading;
import org.elasticsearch.client.Requests;
@ -86,9 +85,6 @@ public class CountRequest extends BroadcastOperationRequest {
@Override public ActionRequestValidationException validate() {
ActionRequestValidationException validationException = super.validate();
if (querySource == null) {
validationException = Actions.addValidationError("query is missing", validationException);
}
return validationException;
}

View File

@ -35,6 +35,7 @@ import org.elasticsearch.common.Strings;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.io.FastByteArrayOutputStream;
import org.elasticsearch.common.lucene.Lucene;
import org.elasticsearch.common.lucene.search.Queries;
import org.elasticsearch.common.metrics.MeanMetric;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
@ -367,7 +368,12 @@ public class InternalIndexShard extends AbstractIndexShardComponent implements I
@Override public long count(float minScore, byte[] querySource, int querySourceOffset, int querySourceLength,
@Nullable String[] filteringAliases, String... types) throws ElasticSearchException {
readAllowed();
Query query = queryParserService.parse(querySource, querySourceOffset, querySourceLength).query();
Query query;
if (querySourceLength == 0) {
query = Queries.MATCH_ALL_QUERY;
} else {
query = queryParserService.parse(querySource, querySourceOffset, querySourceLength).query();
}
// wrap it in filter, cache it, and constant score it
// Don't cache it, since it might be very different queries each time...
// query = new ConstantScoreQuery(filterCache.cache(new QueryWrapperFilter(query)));

View File

@ -231,6 +231,13 @@ public class DocumentActionsTests extends AbstractNodesTests {
assertThat(countResponse.count(), equalTo(0l));
assertThat(countResponse.successfulShards(), equalTo(0));
assertThat(countResponse.failedShards(), equalTo(5));
// count with no query is a match all one
countResponse = client1.prepareCount("test").execute().actionGet();
assertThat("Failures " + countResponse.shardFailures(), countResponse.shardFailures().size(), equalTo(0));
assertThat(countResponse.count(), equalTo(2l));
assertThat(countResponse.successfulShards(), equalTo(5));
assertThat(countResponse.failedShards(), equalTo(0));
}
logger.info("Delete by query");