From 8f7e3c8b5398ca1a59c8f09d1d04c07d8610b962 Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Mon, 16 Sep 2013 23:32:17 +0200 Subject: [PATCH] Better handling of /_all/_search when no indices exist closes #3710 --- .../search/type/TransportSearchTypeAction.java | 12 ++++++------ .../indicesboost/SimpleIndicesBoostSearchTests.java | 12 +++--------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/elasticsearch/action/search/type/TransportSearchTypeAction.java b/src/main/java/org/elasticsearch/action/search/type/TransportSearchTypeAction.java index e4d81f3ed17..976b422e4e8 100644 --- a/src/main/java/org/elasticsearch/action/search/type/TransportSearchTypeAction.java +++ b/src/main/java/org/elasticsearch/action/search/type/TransportSearchTypeAction.java @@ -42,11 +42,11 @@ import org.elasticsearch.search.SearchShardTarget; import org.elasticsearch.search.action.SearchServiceListener; import org.elasticsearch.search.action.SearchServiceTransportAction; import org.elasticsearch.search.controller.SearchPhaseController; +import org.elasticsearch.search.internal.InternalSearchResponse; import org.elasticsearch.search.internal.ShardSearchRequest; import org.elasticsearch.search.query.QuerySearchResultProvider; import org.elasticsearch.threadpool.ThreadPool; -import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Set; @@ -119,15 +119,15 @@ public abstract class TransportSearchTypeAction extends TransportAction(shardsIts.size()); } public void start() { + if (expectedSuccessfulOps == 0) { + // no search shards to search on, bail with empty response (it happens with search across _all with no indices around and consistent with broadcast operations) + listener.onResponse(new SearchResponse(InternalSearchResponse.EMPTY, null, 0, 0, System.currentTimeMillis() - startTime, ShardSearchFailure.EMPTY_ARRAY)); + return; + } request.beforeStart(); // count the local operations, and perform the non local ones int localOperations = 0; diff --git a/src/test/java/org/elasticsearch/search/indicesboost/SimpleIndicesBoostSearchTests.java b/src/test/java/org/elasticsearch/search/indicesboost/SimpleIndicesBoostSearchTests.java index 0a873991937..c378f92b6f8 100644 --- a/src/test/java/org/elasticsearch/search/indicesboost/SimpleIndicesBoostSearchTests.java +++ b/src/test/java/org/elasticsearch/search/indicesboost/SimpleIndicesBoostSearchTests.java @@ -19,19 +19,19 @@ package org.elasticsearch.search.indicesboost; +import org.elasticsearch.AbstractSharedClusterTest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchType; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.AbstractSharedClusterTest; +import org.elasticsearch.test.hamcrest.ElasticsearchAssertions; import org.junit.Test; import static org.elasticsearch.client.Requests.*; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.query.QueryBuilders.termQuery; import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource; -import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; /** @@ -46,13 +46,7 @@ public class SimpleIndicesBoostSearchTests extends AbstractSharedClusterTest { @Test public void testIndicesBoost() throws Exception { - // execute a search before we create an index - try { - client().prepareSearch().setQuery(termQuery("test", "value")).execute().actionGet(); - assert false : "should fail"; - } catch (Exception e) { - // ignore, no indices - } + ElasticsearchAssertions.assertHitCount(client().prepareSearch().setQuery(termQuery("test", "value")).get(), 0); try { client().prepareSearch("test").setQuery(termQuery("test", "value")).execute().actionGet();