Permalink
Browse files

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

  • Loading branch information...
1 parent 768c6d2 commit 4bbe1b111af4adef6482258bd33744d22ae471d0 @kimchy kimchy committed Oct 6, 2011
@@ -22,7 +22,6 @@
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 CountRequest(String... indices) {
@Override public ActionRequestValidationException validate() {
ActionRequestValidationException validationException = super.validate();
- if (querySource == null) {
- validationException = Actions.addValidationError("query is missing", validationException);
- }
return validationException;
}
@@ -35,6 +35,7 @@
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 InternalIndexShard start(String reason) throws IndexShardStartedException
@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)));
@@ -231,6 +231,13 @@ protected Client getClient2() {
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");

0 comments on commit 4bbe1b1

Please sign in to comment.