From fe1d22af0119bb040e0fa8a259861472f5ce3c11 Mon Sep 17 00:00:00 2001 From: Luca Cavanna Date: Fri, 29 Nov 2013 20:25:42 +0100 Subject: [PATCH] trimmed down SimpleQueryTests + more assertAcked --- .../count/query/SimpleQueryTests.java | 847 +++---- .../search/query/SimpleQueryTests.java | 1969 +++++++---------- .../hamcrest/ElasticsearchAssertions.java | 6 +- .../test/hamcrest/ElasticsearchMatchers.java | 22 + 4 files changed, 1082 insertions(+), 1762 deletions(-) diff --git a/src/test/java/org/elasticsearch/count/query/SimpleQueryTests.java b/src/test/java/org/elasticsearch/count/query/SimpleQueryTests.java index 34d60914bae..bd24847d7f6 100644 --- a/src/test/java/org/elasticsearch/count/query/SimpleQueryTests.java +++ b/src/test/java/org/elasticsearch/count/query/SimpleQueryTests.java @@ -23,7 +23,6 @@ import org.elasticsearch.ElasticSearchException; import org.elasticsearch.action.count.CountResponse; import org.elasticsearch.action.search.SearchPhaseExecutionException; import org.elasticsearch.common.bytes.BytesArray; -import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.index.query.*; import org.elasticsearch.index.query.CommonTermsQueryBuilder.Operator; @@ -36,35 +35,34 @@ import org.junit.Test; import java.io.IOException; +import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS; +import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_SHARDS; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.query.FilterBuilders.*; import static org.elasticsearch.index.query.QueryBuilders.*; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*; import static org.hamcrest.Matchers.*; -/** - * - */ public class SimpleQueryTests extends ElasticsearchIntegrationTest { @Test public void passQueryAsStringTest() throws Exception { - client().admin().indices().prepareCreate("test").setSettings("index.number_of_shards", 1).execute().actionGet(); + assertAcked(prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1)); - client().prepareIndex("test", "type1", "1").setSource("field1", "value1_1", "field2", "value2_1").setRefresh(true).execute().actionGet(); + client().prepareIndex("test", "type1", "1").setSource("field1", "value1_1", "field2", "value2_1").setRefresh(true).get(); - CountResponse countResponse = client().prepareCount().setQuery(new BytesArray("{ \"term\" : { \"field1\" : \"value1_1\" }}").array()).execute().actionGet(); + CountResponse countResponse = client().prepareCount().setQuery(new BytesArray("{ \"term\" : { \"field1\" : \"value1_1\" }}").array()).get(); assertHitCount(countResponse, 1l); } @Test public void testIndexOptions() throws Exception { - client().admin().indices().prepareCreate("test") + assertAcked(prepareCreate("test") .addMapping("type1", "field1", "type=string,index_options=docs") - .setSettings("index.number_of_shards", 1).get(); + .setSettings("index.number_of_shards", 1)); - client().prepareIndex("test", "type1", "1").setSource("field1", "quick brown fox", "field2", "quick brown fox").execute().actionGet(); - client().prepareIndex("test", "type1", "2").setSource("field1", "quick lazy huge brown fox", "field2", "quick lazy huge brown fox").setRefresh(true).execute().actionGet(); + client().prepareIndex("test", "type1", "1").setSource("field1", "quick brown fox", "field2", "quick brown fox").get(); + client().prepareIndex("test", "type1", "2").setSource("field1", "quick lazy huge brown fox", "field2", "quick lazy huge brown fox").setRefresh(true).get(); CountResponse countResponse = client().prepareCount().setQuery(QueryBuilders.matchQuery("field2", "quick brown").type(Type.PHRASE).slop(0)).get(); assertHitCount(countResponse, 1l); @@ -77,75 +75,75 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest { @Test public void testCommonTermsQuery() throws Exception { - client().admin().indices().prepareCreate("test") + assertAcked(prepareCreate("test") .addMapping("type1", "field1", "type=string,analyzer=whitespace") - .setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet(); - indexRandom(true, client().prepareIndex("test", "type1", "3").setSource("field1", "quick lazy huge brown pidgin", "field2", "the quick lazy huge brown fox jumps over the tree"), + .setSettings(SETTING_NUMBER_OF_SHARDS, 1)); + + indexRandom(true, + client().prepareIndex("test", "type1", "3").setSource("field1", "quick lazy huge brown pidgin", "field2", "the quick lazy huge brown fox jumps over the tree"), client().prepareIndex("test", "type1", "1").setSource("field1", "the quick brown fox"), - client().prepareIndex("test", "type1", "2").setSource("field1", "the quick lazy huge brown fox jumps over the tree") - ); + client().prepareIndex("test", "type1", "2").setSource("field1", "the quick lazy huge brown fox jumps over the tree") ); - CountResponse countResponse = client().prepareCount().setQuery(QueryBuilders.commonTerms("field1", "the quick brown").cutoffFrequency(3).lowFreqOperator(Operator.OR)).execute().actionGet(); + CountResponse countResponse = client().prepareCount().setQuery(QueryBuilders.commonTerms("field1", "the quick brown").cutoffFrequency(3).lowFreqOperator(Operator.OR)).get(); assertHitCount(countResponse, 3l); - countResponse = client().prepareCount().setQuery(QueryBuilders.commonTerms("field1", "the quick brown").cutoffFrequency(3).lowFreqOperator(Operator.AND)).execute().actionGet(); + countResponse = client().prepareCount().setQuery(QueryBuilders.commonTerms("field1", "the quick brown").cutoffFrequency(3).lowFreqOperator(Operator.AND)).get(); assertHitCount(countResponse, 2l); // Default - countResponse = client().prepareCount().setQuery(QueryBuilders.commonTerms("field1", "the quick brown").cutoffFrequency(3)).execute().actionGet(); + countResponse = client().prepareCount().setQuery(QueryBuilders.commonTerms("field1", "the quick brown").cutoffFrequency(3)).get(); assertHitCount(countResponse, 3l); - countResponse = client().prepareCount().setQuery(QueryBuilders.commonTerms("field1", "the huge fox").lowFreqMinimumShouldMatch("2")).execute().actionGet(); + countResponse = client().prepareCount().setQuery(QueryBuilders.commonTerms("field1", "the huge fox").lowFreqMinimumShouldMatch("2")).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount().setQuery(QueryBuilders.commonTerms("field1", "the lazy fox brown").cutoffFrequency(1).highFreqMinimumShouldMatch("3")).execute().actionGet(); + countResponse = client().prepareCount().setQuery(QueryBuilders.commonTerms("field1", "the lazy fox brown").cutoffFrequency(1).highFreqMinimumShouldMatch("3")).get(); assertHitCount(countResponse, 2l); - countResponse = client().prepareCount().setQuery(QueryBuilders.commonTerms("field1", "the lazy fox brown").cutoffFrequency(1).highFreqMinimumShouldMatch("4")).execute().actionGet(); + countResponse = client().prepareCount().setQuery(QueryBuilders.commonTerms("field1", "the lazy fox brown").cutoffFrequency(1).highFreqMinimumShouldMatch("4")).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount().setQuery(new BytesArray("{ \"common\" : { \"field1\" : { \"query\" : \"the lazy fox brown\", \"cutoff_frequency\" : 1, \"minimum_should_match\" : { \"high_freq\" : 4 } } } }").array()).execute().actionGet(); + countResponse = client().prepareCount().setQuery(new BytesArray("{ \"common\" : { \"field1\" : { \"query\" : \"the lazy fox brown\", \"cutoff_frequency\" : 1, \"minimum_should_match\" : { \"high_freq\" : 4 } } } }").array()).get(); assertHitCount(countResponse, 1l); // Default - countResponse = client().prepareCount().setQuery(QueryBuilders.commonTerms("field1", "the lazy fox brown").cutoffFrequency(1)).execute().actionGet(); + countResponse = client().prepareCount().setQuery(QueryBuilders.commonTerms("field1", "the lazy fox brown").cutoffFrequency(1)).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount().setQuery(QueryBuilders.commonTerms("field1", "the quick brown").cutoffFrequency(3).analyzer("standard")).execute().actionGet(); + countResponse = client().prepareCount().setQuery(QueryBuilders.commonTerms("field1", "the quick brown").cutoffFrequency(3).analyzer("standard")).get(); assertHitCount(countResponse, 3l); // standard drops "the" since its a stopword // try the same with match query - countResponse = client().prepareCount().setQuery(QueryBuilders.matchQuery("field1", "the quick brown").cutoffFrequency(3).operator(MatchQueryBuilder.Operator.AND)).execute().actionGet(); + countResponse = client().prepareCount().setQuery(QueryBuilders.matchQuery("field1", "the quick brown").cutoffFrequency(3).operator(MatchQueryBuilder.Operator.AND)).get(); assertHitCount(countResponse, 2l); - countResponse = client().prepareCount().setQuery(QueryBuilders.matchQuery("field1", "the quick brown").cutoffFrequency(3).operator(MatchQueryBuilder.Operator.OR)).execute().actionGet(); + countResponse = client().prepareCount().setQuery(QueryBuilders.matchQuery("field1", "the quick brown").cutoffFrequency(3).operator(MatchQueryBuilder.Operator.OR)).get(); assertHitCount(countResponse, 3l); - countResponse = client().prepareCount().setQuery(QueryBuilders.matchQuery("field1", "the quick brown").cutoffFrequency(3).operator(MatchQueryBuilder.Operator.AND).analyzer("stop")).execute().actionGet(); + countResponse = client().prepareCount().setQuery(QueryBuilders.matchQuery("field1", "the quick brown").cutoffFrequency(3).operator(MatchQueryBuilder.Operator.AND).analyzer("stop")).get(); assertHitCount(countResponse, 3l); // standard drops "the" since its a stopword // try the same with multi match query - countResponse = client().prepareCount().setQuery(QueryBuilders.multiMatchQuery("the quick brown", "field1", "field2").cutoffFrequency(3).operator(MatchQueryBuilder.Operator.AND)).execute().actionGet(); + countResponse = client().prepareCount().setQuery(QueryBuilders.multiMatchQuery("the quick brown", "field1", "field2").cutoffFrequency(3).operator(MatchQueryBuilder.Operator.AND)).get(); assertHitCount(countResponse, 3l); } @Test public void testOmitTermFreqsAndPositions() throws Exception { // backwards compat test! - client().admin().indices().prepareCreate("test") + assertAcked(prepareCreate("test") .addMapping("type1", "field1", "type=string,omit_term_freq_and_positions=true") - .setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).get(); + .setSettings(SETTING_NUMBER_OF_SHARDS, 1)); indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("field1", "quick brown fox", "field2", "quick brown fox"), client().prepareIndex("test", "type1", "2").setSource("field1", "quick lazy huge brown fox", "field2", "quick lazy huge brown fox")); - - CountResponse countResponse = client().prepareCount().setQuery(QueryBuilders.matchQuery("field2", "quick brown").type(Type.PHRASE).slop(0)).execute().actionGet(); + CountResponse countResponse = client().prepareCount().setQuery(QueryBuilders.matchQuery("field2", "quick brown").type(Type.PHRASE).slop(0)).get(); assertHitCount(countResponse, 1l); try { - client().prepareCount().setQuery(QueryBuilders.matchQuery("field1", "quick brown").type(Type.PHRASE).slop(0)).execute().actionGet(); + client().prepareCount().setQuery(QueryBuilders.matchQuery("field1", "quick brown").type(Type.PHRASE).slop(0)).get(); } catch (SearchPhaseExecutionException e) { assertTrue(e.getMessage().endsWith("IllegalStateException[field \"field1\" was indexed without position data; cannot run PhraseQuery (term=quick)]; }")); } @@ -153,68 +151,65 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest { @Test public void queryStringAnalyzedWildcard() throws Exception { - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet(); + assertAcked(prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1)); - client().prepareIndex("test", "type1", "1").setSource("field1", "value_1", "field2", "value_2").execute().actionGet(); + client().prepareIndex("test", "type1", "1").setSource("field1", "value_1", "field2", "value_2").get(); + refresh(); - client().admin().indices().prepareRefresh().execute().actionGet(); - - CountResponse countResponse = client().prepareCount().setQuery(queryString("value*").analyzeWildcard(true)).execute().actionGet(); + CountResponse countResponse = client().prepareCount().setQuery(queryString("value*").analyzeWildcard(true)).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount().setQuery(queryString("*ue*").analyzeWildcard(true)).execute().actionGet(); + countResponse = client().prepareCount().setQuery(queryString("*ue*").analyzeWildcard(true)).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount().setQuery(queryString("*ue_1").analyzeWildcard(true)).execute().actionGet(); + countResponse = client().prepareCount().setQuery(queryString("*ue_1").analyzeWildcard(true)).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount().setQuery(queryString("val*e_1").analyzeWildcard(true)).execute().actionGet(); + countResponse = client().prepareCount().setQuery(queryString("val*e_1").analyzeWildcard(true)).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount().setQuery(queryString("v?l*e?1").analyzeWildcard(true)).execute().actionGet(); + countResponse = client().prepareCount().setQuery(queryString("v?l*e?1").analyzeWildcard(true)).get(); assertHitCount(countResponse, 1l); } @Test public void testLowercaseExpandedTerms() { - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet(); + assertAcked(prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1)); - client().prepareIndex("test", "type1", "1").setSource("field1", "value_1", "field2", "value_2").execute().actionGet(); + client().prepareIndex("test", "type1", "1").setSource("field1", "value_1", "field2", "value_2").get(); + refresh(); - client().admin().indices().prepareRefresh().execute().actionGet(); - - CountResponse countResponse = client().prepareCount().setQuery(queryString("VALUE_3~1").lowercaseExpandedTerms(true)).execute().actionGet(); + CountResponse countResponse = client().prepareCount().setQuery(queryString("VALUE_3~1").lowercaseExpandedTerms(true)).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount().setQuery(queryString("VALUE_3~1").lowercaseExpandedTerms(false)).execute().actionGet(); + countResponse = client().prepareCount().setQuery(queryString("VALUE_3~1").lowercaseExpandedTerms(false)).get(); assertHitCount(countResponse, 0l); - countResponse = client().prepareCount().setQuery(queryString("ValUE_*").lowercaseExpandedTerms(true)).execute().actionGet(); + countResponse = client().prepareCount().setQuery(queryString("ValUE_*").lowercaseExpandedTerms(true)).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount().setQuery(queryString("vAl*E_1")).execute().actionGet(); + countResponse = client().prepareCount().setQuery(queryString("vAl*E_1")).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount().setQuery(queryString("[VALUE_1 TO VALUE_3]")).execute().actionGet(); + countResponse = client().prepareCount().setQuery(queryString("[VALUE_1 TO VALUE_3]")).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount().setQuery(queryString("[VALUE_1 TO VALUE_3]").lowercaseExpandedTerms(false)).execute().actionGet(); + countResponse = client().prepareCount().setQuery(queryString("[VALUE_1 TO VALUE_3]").lowercaseExpandedTerms(false)).get(); assertHitCount(countResponse, 0l); } @Test public void testDateRangeInQueryString() { - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet(); + assertAcked(prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1)); String aMonthAgo = ISODateTimeFormat.yearMonthDay().print(new DateTime(DateTimeZone.UTC).minusMonths(1)); String aMonthFromNow = ISODateTimeFormat.yearMonthDay().print(new DateTime(DateTimeZone.UTC).plusMonths(1)); - client().prepareIndex("test", "type", "1").setSource("past", aMonthAgo, "future", aMonthFromNow).execute().actionGet(); - + client().prepareIndex("test", "type", "1").setSource("past", aMonthAgo, "future", aMonthFromNow).get(); refresh(); - CountResponse countResponse = client().prepareCount().setQuery(queryString("past:[now-2M/d TO now/d]")).execute().actionGet(); + CountResponse countResponse = client().prepareCount().setQuery(queryString("past:[now-2M/d TO now/d]")).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount().setQuery(queryString("future:[now/d TO now+2M/d]").lowercaseExpandedTerms(false)).execute().actionGet(); + countResponse = client().prepareCount().setQuery(queryString("future:[now/d TO now+2M/d]").lowercaseExpandedTerms(false)).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount().setQuery(queryString("future:[now/D TO now+2M/d]").lowercaseExpandedTerms(false)).execute().actionGet(); + countResponse = client().prepareCount().setQuery(queryString("future:[now/D TO now+2M/d]").lowercaseExpandedTerms(false)).get(); //D is an unsupported unit in date math assertThat(countResponse.getSuccessfulShards(), equalTo(0)); assertThat(countResponse.getFailedShards(), equalTo(1)); @@ -233,14 +228,13 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest { } private void typeFilterTests(String index) throws Exception { - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)) + assertAcked(prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1) .addMapping("type1", jsonBuilder().startObject().startObject("type1") .startObject("_type").field("index", index).endObject() .endObject().endObject()) .addMapping("type2", jsonBuilder().startObject().startObject("type2") .startObject("_type").field("index", index).endObject() - .endObject().endObject()) - .execute().actionGet(); + .endObject().endObject())); indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("field1", "value1"), client().prepareIndex("test", "type2", "1").setSource("field1", "value1"), client().prepareIndex("test", "type1", "2").setSource("field1", "value1"), @@ -250,10 +244,10 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest { assertHitCount(client().prepareCount().setQuery(filteredQuery(matchAllQuery(), typeFilter("type1"))).get(), 2l); assertHitCount(client().prepareCount().setQuery(filteredQuery(matchAllQuery(), typeFilter("type2"))).get(), 3l); - assertHitCount(client().prepareCount().setTypes("type1").setQuery(matchAllQuery()).execute().actionGet(), 2l); - assertHitCount(client().prepareCount().setTypes("type2").setQuery(matchAllQuery()).execute().actionGet(), 3l); + assertHitCount(client().prepareCount().setTypes("type1").setQuery(matchAllQuery()).get(), 2l); + assertHitCount(client().prepareCount().setTypes("type2").setQuery(matchAllQuery()).get(), 3l); - assertHitCount(client().prepareCount().setTypes("type1", "type2").setQuery(matchAllQuery()).execute().actionGet(), 5l); + assertHitCount(client().prepareCount().setTypes("type1", "type2").setQuery(matchAllQuery()).get(), 5l); } @Test @@ -267,547 +261,423 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest { } private void idsFilterTests(String index) throws Exception { - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)) + assertAcked(prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1) .addMapping("type1", jsonBuilder().startObject().startObject("type1") .startObject("_id").field("index", index).endObject() - .endObject().endObject()) - .execute().actionGet(); + .endObject().endObject())); - client().prepareIndex("test", "type1", "1").setSource("field1", "value1").execute().actionGet(); - client().admin().indices().prepareFlush().execute().actionGet(); - client().prepareIndex("test", "type1", "2").setSource("field1", "value2").execute().actionGet(); - client().prepareIndex("test", "type1", "3").setSource("field1", "value3").execute().actionGet(); + indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("field1", "value1"), + client().prepareIndex("test", "type1", "2").setSource("field1", "value2"), + client().prepareIndex("test", "type1", "3").setSource("field1", "value3")); - client().admin().indices().prepareRefresh().execute().actionGet(); - - CountResponse countResponse = client().prepareCount().setQuery(constantScoreQuery(idsFilter("type1").ids("1", "3"))).execute().actionGet(); + CountResponse countResponse = client().prepareCount().setQuery(constantScoreQuery(idsFilter("type1").ids("1", "3"))).get(); assertHitCount(countResponse, 2l); // no type - countResponse = client().prepareCount().setQuery(constantScoreQuery(idsFilter().ids("1", "3"))).execute().actionGet(); + countResponse = client().prepareCount().setQuery(constantScoreQuery(idsFilter().ids("1", "3"))).get(); assertHitCount(countResponse, 2l); - countResponse = client().prepareCount().setQuery(idsQuery("type1").ids("1", "3")).execute().actionGet(); + countResponse = client().prepareCount().setQuery(idsQuery("type1").ids("1", "3")).get(); assertHitCount(countResponse, 2l); // no type - countResponse = client().prepareCount().setQuery(idsQuery().ids("1", "3")).execute().actionGet(); + countResponse = client().prepareCount().setQuery(idsQuery().ids("1", "3")).get(); assertHitCount(countResponse, 2l); - countResponse = client().prepareCount().setQuery(idsQuery("type1").ids("7", "10")).execute().actionGet(); + countResponse = client().prepareCount().setQuery(idsQuery("type1").ids("7", "10")).get(); assertHitCount(countResponse, 0l); // repeat..., with terms - countResponse = client().prepareCount().setTypes("type1").setQuery(constantScoreQuery(termsFilter("_id", "1", "3"))).execute().actionGet(); + countResponse = client().prepareCount().setTypes("type1").setQuery(constantScoreQuery(termsFilter("_id", "1", "3"))).get(); assertHitCount(countResponse, 2l); } @Test public void testLimitFilter() throws Exception { - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet(); + assertAcked(prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1)); - client().prepareIndex("test", "type1", "1").setSource("field1", "value1_1").execute().actionGet(); - client().prepareIndex("test", "type1", "2").setSource("field1", "value1_2").execute().actionGet(); - client().prepareIndex("test", "type1", "3").setSource("field2", "value2_3").execute().actionGet(); - client().prepareIndex("test", "type1", "4").setSource("field3", "value3_4").execute().actionGet(); + indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("field1", "value1_1"), + client().prepareIndex("test", "type1", "2").setSource("field1", "value1_2"), + client().prepareIndex("test", "type1", "3").setSource("field2", "value2_3"), + client().prepareIndex("test", "type1", "4").setSource("field3", "value3_4")); - client().admin().indices().prepareRefresh().execute().actionGet(); - - CountResponse countResponse = client().prepareCount().setQuery(filteredQuery(matchAllQuery(), limitFilter(2))).execute().actionGet(); + CountResponse countResponse = client().prepareCount().setQuery(filteredQuery(matchAllQuery(), limitFilter(2))).get(); assertHitCount(countResponse, 2l); } @Test public void filterExistsMissingTests() throws Exception { - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet(); + assertAcked(prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1)); - client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject().startObject("obj1").field("obj1_val", "1").endObject().field("x1", "x_1").field("field1", "value1_1").field("field2", "value2_1").endObject()).execute().actionGet(); - client().prepareIndex("test", "type1", "2").setSource(jsonBuilder().startObject().startObject("obj1").field("obj1_val", "1").endObject().field("x2", "x_2").field("field1", "value1_2").endObject()).execute().actionGet(); - client().prepareIndex("test", "type1", "3").setSource(jsonBuilder().startObject().startObject("obj2").field("obj2_val", "1").endObject().field("y1", "y_1").field("field2", "value2_3").endObject()).execute().actionGet(); - client().prepareIndex("test", "type1", "4").setSource(jsonBuilder().startObject().startObject("obj2").field("obj2_val", "1").endObject().field("y2", "y_2").field("field3", "value3_4").endObject()).execute().actionGet(); + indexRandom(true, + client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject().startObject("obj1").field("obj1_val", "1").endObject().field("x1", "x_1").field("field1", "value1_1").field("field2", "value2_1").endObject()), + client().prepareIndex("test", "type1", "2").setSource(jsonBuilder().startObject().startObject("obj1").field("obj1_val", "1").endObject().field("x2", "x_2").field("field1", "value1_2").endObject()), + client().prepareIndex("test", "type1", "3").setSource(jsonBuilder().startObject().startObject("obj2").field("obj2_val", "1").endObject().field("y1", "y_1").field("field2", "value2_3").endObject()), + client().prepareIndex("test", "type1", "4").setSource(jsonBuilder().startObject().startObject("obj2").field("obj2_val", "1").endObject().field("y2", "y_2").field("field3", "value3_4").endObject())); - client().admin().indices().prepareRefresh().execute().actionGet(); - - CountResponse countResponse = client().prepareCount().setQuery(filteredQuery(matchAllQuery(), existsFilter("field1"))).execute().actionGet(); + CountResponse countResponse = client().prepareCount().setQuery(filteredQuery(matchAllQuery(), existsFilter("field1"))).get(); assertHitCount(countResponse, 2l); - countResponse = client().prepareCount().setQuery(constantScoreQuery(existsFilter("field1"))).execute().actionGet(); + countResponse = client().prepareCount().setQuery(constantScoreQuery(existsFilter("field1"))).get(); assertHitCount(countResponse, 2l); - countResponse = client().prepareCount().setQuery(queryString("_exists_:field1")).execute().actionGet(); + countResponse = client().prepareCount().setQuery(queryString("_exists_:field1")).get(); assertHitCount(countResponse, 2l); - countResponse = client().prepareCount().setQuery(filteredQuery(matchAllQuery(), existsFilter("field2"))).execute().actionGet(); + countResponse = client().prepareCount().setQuery(filteredQuery(matchAllQuery(), existsFilter("field2"))).get(); assertHitCount(countResponse, 2l); - countResponse = client().prepareCount().setQuery(filteredQuery(matchAllQuery(), existsFilter("field3"))).execute().actionGet(); + countResponse = client().prepareCount().setQuery(filteredQuery(matchAllQuery(), existsFilter("field3"))).get(); assertHitCount(countResponse, 1l); // wildcard check - countResponse = client().prepareCount().setQuery(filteredQuery(matchAllQuery(), existsFilter("x*"))).execute().actionGet(); + countResponse = client().prepareCount().setQuery(filteredQuery(matchAllQuery(), existsFilter("x*"))).get(); assertHitCount(countResponse, 2l); // object check - countResponse = client().prepareCount().setQuery(filteredQuery(matchAllQuery(), existsFilter("obj1"))).execute().actionGet(); + countResponse = client().prepareCount().setQuery(filteredQuery(matchAllQuery(), existsFilter("obj1"))).get(); assertHitCount(countResponse, 2l); - countResponse = client().prepareCount().setQuery(filteredQuery(matchAllQuery(), missingFilter("field1"))).execute().actionGet(); + countResponse = client().prepareCount().setQuery(filteredQuery(matchAllQuery(), missingFilter("field1"))).get(); assertHitCount(countResponse, 2l); - countResponse = client().prepareCount().setQuery(filteredQuery(matchAllQuery(), missingFilter("field1"))).execute().actionGet(); + countResponse = client().prepareCount().setQuery(filteredQuery(matchAllQuery(), missingFilter("field1"))).get(); assertHitCount(countResponse, 2l); - countResponse = client().prepareCount().setQuery(constantScoreQuery(missingFilter("field1"))).execute().actionGet(); + countResponse = client().prepareCount().setQuery(constantScoreQuery(missingFilter("field1"))).get(); assertHitCount(countResponse, 2l); - countResponse = client().prepareCount().setQuery(queryString("_missing_:field1")).execute().actionGet(); + countResponse = client().prepareCount().setQuery(queryString("_missing_:field1")).get(); assertHitCount(countResponse, 2l); // wildcard check - countResponse = client().prepareCount().setQuery(filteredQuery(matchAllQuery(), missingFilter("x*"))).execute().actionGet(); + countResponse = client().prepareCount().setQuery(filteredQuery(matchAllQuery(), missingFilter("x*"))).get(); assertHitCount(countResponse, 2l); // object check - countResponse = client().prepareCount().setQuery(filteredQuery(matchAllQuery(), missingFilter("obj1"))).execute().actionGet(); + countResponse = client().prepareCount().setQuery(filteredQuery(matchAllQuery(), missingFilter("obj1"))).get(); assertHitCount(countResponse, 2l); } @Test public void passQueryAsJSONStringTest() throws Exception { - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet(); + assertAcked(prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1)); - client().prepareIndex("test", "type1", "1").setSource("field1", "value1_1", "field2", "value2_1").setRefresh(true).execute().actionGet(); + client().prepareIndex("test", "type1", "1").setSource("field1", "value1_1", "field2", "value2_1").setRefresh(true).get(); WrapperQueryBuilder wrapper = new WrapperQueryBuilder("{ \"term\" : { \"field1\" : \"value1_1\" } }"); - CountResponse countResponse = client().prepareCount().setQuery(wrapper).execute().actionGet(); - assertHitCount(countResponse, 1l); + assertHitCount(client().prepareCount().setQuery(wrapper).get(), 1l); - BoolQueryBuilder bool = new BoolQueryBuilder(); - bool.must(wrapper); - bool.must(new TermQueryBuilder("field2", "value2_1")); - - countResponse = client().prepareCount().setQuery(wrapper).execute().actionGet(); - assertHitCount(countResponse, 1l); + BoolQueryBuilder bool = boolQuery().must(wrapper).must(new TermQueryBuilder("field2", "value2_1")); + assertHitCount(client().prepareCount().setQuery(bool).get(), 1l); } @Test public void testFiltersWithCustomCacheKey() throws Exception { createIndex("test"); ensureGreen(); - client().prepareIndex("test", "type1", "1").setSource("field1", "value1").execute().actionGet(); + client().prepareIndex("test", "type1", "1").setSource("field1", "value1").get(); refresh(); - CountResponse countResponse = client().prepareCount("test").setQuery(constantScoreQuery(termsFilter("field1", "value1").cacheKey("test1"))).execute().actionGet(); - assertNoFailures(countResponse); + + CountResponse countResponse = client().prepareCount("test").setQuery(constantScoreQuery(termsFilter("field1", "value1").cacheKey("test1"))).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount("test").setQuery(constantScoreQuery(termsFilter("field1", "value1").cacheKey("test1"))).execute().actionGet(); - assertNoFailures(countResponse); + countResponse = client().prepareCount("test").setQuery(constantScoreQuery(termsFilter("field1", "value1").cacheKey("test1"))).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount("test").setQuery(constantScoreQuery(termsFilter("field1", "value1"))).execute().actionGet(); - assertNoFailures(countResponse); + countResponse = client().prepareCount("test").setQuery(constantScoreQuery(termsFilter("field1", "value1"))).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount("test").setQuery(constantScoreQuery(termsFilter("field1", "value1"))).execute().actionGet(); - assertNoFailures(countResponse); + countResponse = client().prepareCount("test").setQuery(constantScoreQuery(termsFilter("field1", "value1"))).get(); assertHitCount(countResponse, 1l); } @Test public void testMatchQueryNumeric() throws Exception { - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet(); + assertAcked(prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1)); - client().prepareIndex("test", "type1", "1").setSource("long", 1l, "double", 1.0d).execute().actionGet(); - client().prepareIndex("test", "type1", "2").setSource("long", 2l, "double", 2.0d).execute().actionGet(); - client().prepareIndex("test", "type1", "3").setSource("long", 3l, "double", 3.0d).execute().actionGet(); - client().admin().indices().prepareRefresh("test").execute().actionGet(); - CountResponse countResponse = client().prepareCount().setQuery(matchQuery("long", "1")).execute().actionGet(); + client().prepareIndex("test", "type1", "1").setSource("long", 1l, "double", 1.0d).get(); + client().prepareIndex("test", "type1", "2").setSource("long", 2l, "double", 2.0d).get(); + client().prepareIndex("test", "type1", "3").setSource("long", 3l, "double", 3.0d).get(); + refresh(); + CountResponse countResponse = client().prepareCount().setQuery(matchQuery("long", "1")).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount().setQuery(matchQuery("double", "2")).execute().actionGet(); + countResponse = client().prepareCount().setQuery(matchQuery("double", "2")).get(); assertHitCount(countResponse, 1l); } @Test public void testMultiMatchQuery() throws Exception { - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet(); + assertAcked(prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1)); - client().prepareIndex("test", "type1", "1").setSource("field1", "value1", "field2", "value4", "field3", "value3").execute().actionGet(); - client().prepareIndex("test", "type1", "2").setSource("field1", "value2", "field2", "value5", "field3", "value2").execute().actionGet(); - client().prepareIndex("test", "type1", "3").setSource("field1", "value3", "field2", "value6", "field3", "value1").execute().actionGet(); - client().admin().indices().prepareRefresh("test").execute().actionGet(); + client().prepareIndex("test", "type1", "1").setSource("field1", "value1", "field2", "value4", "field3", "value3").get(); + client().prepareIndex("test", "type1", "2").setSource("field1", "value2", "field2", "value5", "field3", "value2").get(); + client().prepareIndex("test", "type1", "3").setSource("field1", "value3", "field2", "value6", "field3", "value1").get(); + refresh(); MultiMatchQueryBuilder builder = QueryBuilders.multiMatchQuery("value1 value2 value4", "field1", "field2"); - CountResponse countResponse = client().prepareCount() - .setQuery(builder) - .execute().actionGet(); - + CountResponse countResponse = client().prepareCount().setQuery(builder).get(); assertHitCount(countResponse, 2l); - client().admin().indices().prepareRefresh("test").execute().actionGet(); + refresh(); builder = QueryBuilders.multiMatchQuery("value1", "field1", "field2") .operator(MatchQueryBuilder.Operator.AND); // Operator only applies on terms inside a field! Fields are always OR-ed together. - countResponse = client().prepareCount() - .setQuery(builder) - .execute().actionGet(); + countResponse = client().prepareCount().setQuery(builder).get(); assertHitCount(countResponse, 1l); - client().admin().indices().prepareRefresh("test").execute().actionGet(); + refresh(); builder = QueryBuilders.multiMatchQuery("value1", "field1", "field3^1.5") .operator(MatchQueryBuilder.Operator.AND); // Operator only applies on terms inside a field! Fields are always OR-ed together. - countResponse = client().prepareCount() - .setQuery(builder) - .execute().actionGet(); + countResponse = client().prepareCount().setQuery(builder).get(); assertHitCount(countResponse, 2l); - client().admin().indices().prepareRefresh("test").execute().actionGet(); + refresh(); builder = QueryBuilders.multiMatchQuery("value1").field("field1").field("field3", 1.5f) .operator(MatchQueryBuilder.Operator.AND); // Operator only applies on terms inside a field! Fields are always OR-ed together. - countResponse = client().prepareCount() - .setQuery(builder) - .execute().actionGet(); + countResponse = client().prepareCount().setQuery(builder).get(); assertHitCount(countResponse, 2l); // Test lenient - client().prepareIndex("test", "type1", "3").setSource("field1", "value7", "field2", "value8", "field4", 5).execute().actionGet(); - client().admin().indices().prepareRefresh("test").execute().actionGet(); + client().prepareIndex("test", "type1", "3").setSource("field1", "value7", "field2", "value8", "field4", 5).get(); + refresh(); builder = QueryBuilders.multiMatchQuery("value1", "field1", "field2", "field4"); builder.lenient(true); - countResponse = client().prepareCount().setQuery(builder).execute().actionGet(); + countResponse = client().prepareCount().setQuery(builder).get(); assertHitCount(countResponse, 1l); } @Test - public void testMatchQueryZeroTermsQuery() throws IOException { - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)) - .addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties") - .startObject("field1").field("type", "string").field("analyzer", "classic").endObject() - .startObject("field2").field("type", "string").field("analyzer", "classic").endObject() - .endObject().endObject().endObject()) - .execute().actionGet(); - client().prepareIndex("test", "type1", "1").setSource("field1", "value1").execute().actionGet(); - client().prepareIndex("test", "type1", "2").setSource("field1", "value2").execute().actionGet(); - client().admin().indices().prepareRefresh("test").execute().actionGet(); + public void testMatchQueryZeroTermsQuery() { + assertAcked(prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1) + .addMapping("type1", "field1", "type=string,analyzer=classic", "field2", "type=string,analyzer=classic")); + client().prepareIndex("test", "type1", "1").setSource("field1", "value1").get(); + client().prepareIndex("test", "type1", "2").setSource("field1", "value2").get(); + refresh(); BoolQueryBuilder boolQuery = boolQuery() .must(matchQuery("field1", "a").zeroTermsQuery(MatchQueryBuilder.ZeroTermsQuery.NONE)) .must(matchQuery("field1", "value1").zeroTermsQuery(MatchQueryBuilder.ZeroTermsQuery.NONE)); - CountResponse countResponse = client().prepareCount() - .setQuery(boolQuery) - .execute().actionGet(); + CountResponse countResponse = client().prepareCount().setQuery(boolQuery).get(); assertHitCount(countResponse, 0l); boolQuery = boolQuery() .must(matchQuery("field1", "a").zeroTermsQuery(MatchQueryBuilder.ZeroTermsQuery.ALL)) .must(matchQuery("field1", "value1").zeroTermsQuery(MatchQueryBuilder.ZeroTermsQuery.ALL)); - countResponse = client().prepareCount() - .setQuery(boolQuery) - .execute().actionGet(); + countResponse = client().prepareCount().setQuery(boolQuery).get(); assertHitCount(countResponse, 1l); - boolQuery = boolQuery() - .must(matchQuery("field1", "a").zeroTermsQuery(MatchQueryBuilder.ZeroTermsQuery.ALL)); - countResponse = client().prepareCount() - .setQuery(boolQuery) - .execute().actionGet(); + boolQuery = boolQuery().must(matchQuery("field1", "a").zeroTermsQuery(MatchQueryBuilder.ZeroTermsQuery.ALL)); + countResponse = client().prepareCount().setQuery(boolQuery).get(); assertHitCount(countResponse, 2l); } @Test - public void testMultiMatchQueryZeroTermsQuery() throws IOException { - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)) - .addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties") - .startObject("field1").field("type", "string").field("analyzer", "classic").endObject() - .startObject("field2").field("type", "string").field("analyzer", "classic").endObject() - .endObject().endObject().endObject()) - .execute().actionGet(); - client().prepareIndex("test", "type1", "1").setSource("field1", "value1", "field2", "value2").execute().actionGet(); - client().prepareIndex("test", "type1", "2").setSource("field1", "value3", "field2", "value4").execute().actionGet(); - client().admin().indices().prepareRefresh("test").execute().actionGet(); + public void testMultiMatchQueryZeroTermsQuery() { + assertAcked(client().admin().indices().prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1) + .addMapping("type1", "field1", "type=string,analyzer=classic", "field2", "type=string,analyzer=classic")); + client().prepareIndex("test", "type1", "1").setSource("field1", "value1", "field2", "value2").get(); + client().prepareIndex("test", "type1", "2").setSource("field1", "value3", "field2", "value4").get(); + refresh(); BoolQueryBuilder boolQuery = boolQuery() .must(multiMatchQuery("a", "field1", "field2").zeroTermsQuery(MatchQueryBuilder.ZeroTermsQuery.NONE)) .must(multiMatchQuery("value1", "field1", "field2").zeroTermsQuery(MatchQueryBuilder.ZeroTermsQuery.NONE)); // Fields are ORed together - CountResponse countResponse = client().prepareCount() - .setQuery(boolQuery) - .execute().actionGet(); + CountResponse countResponse = client().prepareCount().setQuery(boolQuery).get(); assertHitCount(countResponse, 0l); boolQuery = boolQuery() .must(multiMatchQuery("a", "field1", "field2").zeroTermsQuery(MatchQueryBuilder.ZeroTermsQuery.ALL)) .must(multiMatchQuery("value4", "field1", "field2").zeroTermsQuery(MatchQueryBuilder.ZeroTermsQuery.ALL)); - countResponse = client().prepareCount() - .setQuery(boolQuery) - .execute().actionGet(); + countResponse = client().prepareCount().setQuery(boolQuery).get(); assertHitCount(countResponse, 1l); - boolQuery = boolQuery() - .must(multiMatchQuery("a", "field1").zeroTermsQuery(MatchQueryBuilder.ZeroTermsQuery.ALL)); - countResponse = client().prepareCount() - .setQuery(boolQuery) - .execute().actionGet(); + boolQuery = boolQuery().must(multiMatchQuery("a", "field1").zeroTermsQuery(MatchQueryBuilder.ZeroTermsQuery.ALL)); + countResponse = client().prepareCount().setQuery(boolQuery).get(); assertHitCount(countResponse, 2l); } @Test public void testMultiMatchQueryMinShouldMatch() { - - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet(); - client().prepareIndex("test", "type1", "1").setSource("field1", new String[]{"value1", "value2", "value3"}).execute().actionGet(); - client().prepareIndex("test", "type1", "2").setSource("field2", "value1").execute().actionGet(); - client().admin().indices().prepareRefresh("test").execute().actionGet(); + assertAcked(prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1)); + client().prepareIndex("test", "type1", "1").setSource("field1", new String[]{"value1", "value2", "value3"}).get(); + client().prepareIndex("test", "type1", "2").setSource("field2", "value1").get(); + refresh(); MultiMatchQueryBuilder multiMatchQuery = multiMatchQuery("value1 value2 foo", "field1", "field2"); multiMatchQuery.useDisMax(true); multiMatchQuery.minimumShouldMatch("70%"); - CountResponse countResponse = client().prepareCount() - .setQuery(multiMatchQuery) - .execute().actionGet(); + CountResponse countResponse = client().prepareCount().setQuery(multiMatchQuery).get(); assertHitCount(countResponse, 1l); multiMatchQuery.minimumShouldMatch("30%"); - countResponse = client().prepareCount() - .setQuery(multiMatchQuery) - .execute().actionGet(); + countResponse = client().prepareCount().setQuery(multiMatchQuery).get(); assertHitCount(countResponse, 2l); multiMatchQuery.useDisMax(false); multiMatchQuery.minimumShouldMatch("70%"); - countResponse = client().prepareCount() - .setQuery(multiMatchQuery) - .execute().actionGet(); + countResponse = client().prepareCount().setQuery(multiMatchQuery).get(); assertHitCount(countResponse, 1l); multiMatchQuery.minimumShouldMatch("30%"); - countResponse = client().prepareCount() - .setQuery(multiMatchQuery) - .execute().actionGet(); + countResponse = client().prepareCount().setQuery(multiMatchQuery).get(); assertHitCount(countResponse, 2l); multiMatchQuery = multiMatchQuery("value1 value2 bar", "field1"); multiMatchQuery.minimumShouldMatch("100%"); - countResponse = client().prepareCount() - .setQuery(multiMatchQuery) - .execute().actionGet(); + countResponse = client().prepareCount().setQuery(multiMatchQuery).get(); assertHitCount(countResponse, 0l); multiMatchQuery.minimumShouldMatch("70%"); - countResponse = client().prepareCount() - .setQuery(multiMatchQuery) - .execute().actionGet(); + countResponse = client().prepareCount().setQuery(multiMatchQuery).get(); assertHitCount(countResponse, 1l); } @Test public void testFuzzyQueryString() { - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet(); - client().prepareIndex("test", "type1", "1").setSource("str", "kimchy", "date", "2012-02-01", "num", 12).execute().actionGet(); - client().prepareIndex("test", "type1", "2").setSource("str", "shay", "date", "2012-02-05", "num", 20).execute().actionGet(); - client().admin().indices().prepareRefresh().execute().actionGet(); + assertAcked(prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1)); + client().prepareIndex("test", "type1", "1").setSource("str", "kimchy", "date", "2012-02-01", "num", 12).get(); + client().prepareIndex("test", "type1", "2").setSource("str", "shay", "date", "2012-02-05", "num", 20).get(); + refresh(); - CountResponse countResponse = client().prepareCount() - .setQuery(queryString("str:kimcy~1")) - .execute().actionGet(); - assertNoFailures(countResponse); + CountResponse countResponse = client().prepareCount().setQuery(queryString("str:kimcy~1")).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount() - .setQuery(queryString("num:11~1")) - .execute().actionGet(); - assertNoFailures(countResponse); + countResponse = client().prepareCount().setQuery(queryString("num:11~1")).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount() - .setQuery(queryString("date:2012-02-02~1d")) - .execute().actionGet(); - assertNoFailures(countResponse); + countResponse = client().prepareCount().setQuery(queryString("date:2012-02-02~1d")).get(); assertHitCount(countResponse, 1l); } @Test public void testSpecialRangeSyntaxInQueryString() { - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet(); - client().prepareIndex("test", "type1", "1").setSource("str", "kimchy", "date", "2012-02-01", "num", 12).execute().actionGet(); - client().prepareIndex("test", "type1", "2").setSource("str", "shay", "date", "2012-02-05", "num", 20).execute().actionGet(); - client().admin().indices().prepareRefresh().execute().actionGet(); + assertAcked(prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1)); + client().prepareIndex("test", "type1", "1").setSource("str", "kimchy", "date", "2012-02-01", "num", 12).get(); + client().prepareIndex("test", "type1", "2").setSource("str", "shay", "date", "2012-02-05", "num", 20).get(); + refresh(); - CountResponse countResponse = client().prepareCount() - .setQuery(queryString("num:>19")) - .execute().actionGet(); - assertNoFailures(countResponse); + CountResponse countResponse = client().prepareCount().setQuery(queryString("num:>19")).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount() - .setQuery(queryString("num:>20")) - .execute().actionGet(); - assertNoFailures(countResponse); + countResponse = client().prepareCount().setQuery(queryString("num:>20")).get(); assertHitCount(countResponse, 0l); - countResponse = client().prepareCount() - .setQuery(queryString("num:>=20")) - .execute().actionGet(); - assertNoFailures(countResponse); + countResponse = client().prepareCount().setQuery(queryString("num:>=20")).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount() - .setQuery(queryString("num:>11")) - .execute().actionGet(); - assertNoFailures(countResponse); + countResponse = client().prepareCount().setQuery(queryString("num:>11")).get(); assertHitCount(countResponse, 2l); - countResponse = client().prepareCount() - .setQuery(queryString("num:<20")) - .execute().actionGet(); - assertNoFailures(countResponse); + countResponse = client().prepareCount().setQuery(queryString("num:<20")).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount() - .setQuery(queryString("num:<=20")) - .execute().actionGet(); - assertNoFailures(countResponse); + countResponse = client().prepareCount().setQuery(queryString("num:<=20")).get(); assertHitCount(countResponse, 2l); - countResponse = client().prepareCount() - .setQuery(queryString("+num:>11 +num:<20")) - .execute().actionGet(); - assertNoFailures(countResponse); + countResponse = client().prepareCount().setQuery(queryString("+num:>11 +num:<20")).get(); assertHitCount(countResponse, 1l); } @Test public void testEmptyTermsFilter() throws Exception { - assertAcked(prepareCreate("test").addMapping("type", - jsonBuilder().startObject().startObject("type").startObject("properties") - .startObject("terms").field("type", "string").endObject() - .endObject().endObject().endObject())); + assertAcked(prepareCreate("test").addMapping("type", "terms", "type=string")); ensureGreen(); - client().prepareIndex("test", "type", "1").setSource("term", "1").execute().actionGet(); - client().prepareIndex("test", "type", "2").setSource("term", "2").execute().actionGet(); - client().prepareIndex("test", "type", "3").setSource("term", "3").execute().actionGet(); - client().prepareIndex("test", "type", "4").setSource("term", "4").execute().actionGet(); + client().prepareIndex("test", "type", "1").setSource("term", "1").get(); + client().prepareIndex("test", "type", "2").setSource("term", "2").get(); + client().prepareIndex("test", "type", "3").setSource("term", "3").get(); + client().prepareIndex("test", "type", "4").setSource("term", "4").get(); refresh(); CountResponse countResponse = client().prepareCount("test") - .setQuery(filteredQuery(matchAllQuery(), termsFilter("term", new String[0])) - ).execute().actionGet(); - assertNoFailures(countResponse); + .setQuery(filteredQuery(matchAllQuery(), termsFilter("term", new String[0]))).get(); assertHitCount(countResponse, 0l); countResponse = client().prepareCount("test") - .setQuery(filteredQuery(matchAllQuery(), idsFilter()) - ).execute().actionGet(); - assertNoFailures(countResponse); + .setQuery(filteredQuery(matchAllQuery(), idsFilter())).get(); assertHitCount(countResponse, 0l); } @Test public void testTermsLookupFilter() throws Exception { - assertAcked(prepareCreate("lookup").addMapping("type", - jsonBuilder().startObject().startObject("type").startObject("properties") - .startObject("terms").field("type", "string").endObject() - .startObject("other").field("type", "string").endObject() - .endObject().endObject().endObject())); + assertAcked(prepareCreate("lookup").addMapping("type", "terms", "type=string", "other", "type=string")); assertAcked(prepareCreate("lookup2").addMapping("type", jsonBuilder().startObject().startObject("type").startObject("properties") .startObject("arr").startObject("properties").startObject("term").field("type", "string") .endObject().endObject().endObject().endObject().endObject().endObject())); - assertAcked(prepareCreate("test").addMapping("type", - jsonBuilder().startObject().startObject("type").startObject("properties") - .startObject("term").field("type", "string").endObject() - .endObject().endObject().endObject())); + assertAcked(prepareCreate("test").addMapping("type", "term", "type=string")); ensureGreen(); - client().prepareIndex("lookup", "type", "1").setSource("terms", new String[]{"1", "3"}).execute().actionGet(); - client().prepareIndex("lookup", "type", "2").setSource("terms", new String[]{"2"}).execute().actionGet(); - client().prepareIndex("lookup", "type", "3").setSource("terms", new String[]{"2", "4"}).execute().actionGet(); - client().prepareIndex("lookup", "type", "4").setSource("other", "value").execute().actionGet(); - client().prepareIndex("lookup2", "type", "1").setSource(XContentFactory.jsonBuilder().startObject() - .startArray("arr") - .startObject().field("term", "1").endObject() - .startObject().field("term", "3").endObject() - .endArray() - .endObject()).execute().actionGet(); - client().prepareIndex("lookup2", "type", "2").setSource(XContentFactory.jsonBuilder().startObject() - .startArray("arr") - .startObject().field("term", "2").endObject() - .endArray() - .endObject()).execute().actionGet(); - client().prepareIndex("lookup2", "type", "3").setSource(XContentFactory.jsonBuilder().startObject() - .startArray("arr") - .startObject().field("term", "2").endObject() - .startObject().field("term", "4").endObject() - .endArray() - .endObject()).execute().actionGet(); - - client().prepareIndex("test", "type", "1").setSource("term", "1").execute().actionGet(); - client().prepareIndex("test", "type", "2").setSource("term", "2").execute().actionGet(); - client().prepareIndex("test", "type", "3").setSource("term", "3").execute().actionGet(); - client().prepareIndex("test", "type", "4").setSource("term", "4").execute().actionGet(); - refresh(); + indexRandom(true, client().prepareIndex("lookup", "type", "1").setSource("terms", new String[]{"1", "3"}), + client().prepareIndex("lookup", "type", "2").setSource("terms", new String[]{"2"}), + client().prepareIndex("lookup", "type", "3").setSource("terms", new String[]{"2", "4"}), + client().prepareIndex("lookup", "type", "4").setSource("other", "value"), + client().prepareIndex("lookup2", "type", "1").setSource(XContentFactory.jsonBuilder().startObject() + .startArray("arr") + .startObject().field("term", "1").endObject() + .startObject().field("term", "3").endObject() + .endArray() + .endObject()), + client().prepareIndex("lookup2", "type", "2").setSource(XContentFactory.jsonBuilder().startObject() + .startArray("arr") + .startObject().field("term", "2").endObject() + .endArray() + .endObject()), + client().prepareIndex("lookup2", "type", "3").setSource(XContentFactory.jsonBuilder().startObject() + .startArray("arr") + .startObject().field("term", "2").endObject() + .startObject().field("term", "4").endObject() + .endArray() + .endObject()), + client().prepareIndex("test", "type", "1").setSource("term", "1"), + client().prepareIndex("test", "type", "2").setSource("term", "2"), + client().prepareIndex("test", "type", "3").setSource("term", "3"), + client().prepareIndex("test", "type", "4").setSource("term", "4")); CountResponse countResponse = client().prepareCount("test") - .setQuery(filteredQuery(matchAllQuery(), termsLookupFilter("term").lookupIndex("lookup").lookupType("type").lookupId("1").lookupPath("terms")) - ).execute().actionGet(); - assertNoFailures(countResponse); + .setQuery(filteredQuery(matchAllQuery(), termsLookupFilter("term").lookupIndex("lookup").lookupType("type").lookupId("1").lookupPath("terms"))).get(); assertHitCount(countResponse, 2l); // same as above, just on the _id... countResponse = client().prepareCount("test") - .setQuery(filteredQuery(matchAllQuery(), termsLookupFilter("_id").lookupIndex("lookup").lookupType("type").lookupId("1").lookupPath("terms")) - ).execute().actionGet(); - assertNoFailures(countResponse); + .setQuery(filteredQuery(matchAllQuery(), termsLookupFilter("_id").lookupIndex("lookup").lookupType("type").lookupId("1").lookupPath("terms"))).get(); assertHitCount(countResponse, 2l); // another search with same parameters... countResponse = client().prepareCount("test") - .setQuery(filteredQuery(matchAllQuery(), termsLookupFilter("term").lookupIndex("lookup").lookupType("type").lookupId("1").lookupPath("terms")) - ).execute().actionGet(); - assertNoFailures(countResponse); + .setQuery(filteredQuery(matchAllQuery(), termsLookupFilter("term").lookupIndex("lookup").lookupType("type").lookupId("1").lookupPath("terms"))).get(); assertHitCount(countResponse, 2l); countResponse = client().prepareCount("test") - .setQuery(filteredQuery(matchAllQuery(), termsLookupFilter("term").lookupIndex("lookup").lookupType("type").lookupId("2").lookupPath("terms")) - ).execute().actionGet(); - assertNoFailures(countResponse); + .setQuery(filteredQuery(matchAllQuery(), termsLookupFilter("term").lookupIndex("lookup").lookupType("type").lookupId("2").lookupPath("terms"))).get(); assertHitCount(countResponse, 1l); countResponse = client().prepareCount("test") .setQuery(filteredQuery(matchAllQuery(), termsLookupFilter("term").lookupIndex("lookup").lookupType("type").lookupId("3").lookupPath("terms")) - ).execute().actionGet(); + ).get(); assertNoFailures(countResponse); assertHitCount(countResponse, 2l); countResponse = client().prepareCount("test") - .setQuery(filteredQuery(matchAllQuery(), termsLookupFilter("term").lookupIndex("lookup").lookupType("type").lookupId("4").lookupPath("terms")) - ).execute().actionGet(); - assertNoFailures(countResponse); + .setQuery(filteredQuery(matchAllQuery(), termsLookupFilter("term").lookupIndex("lookup").lookupType("type").lookupId("4").lookupPath("terms"))).get(); assertHitCount(countResponse, 0l); - countResponse = client().prepareCount("test") - .setQuery(filteredQuery(matchAllQuery(), termsLookupFilter("term").lookupIndex("lookup2").lookupType("type").lookupId("1").lookupPath("arr.term")) - ).execute().actionGet(); - assertNoFailures(countResponse); + .setQuery(filteredQuery(matchAllQuery(), termsLookupFilter("term").lookupIndex("lookup2").lookupType("type").lookupId("1").lookupPath("arr.term"))).get(); assertHitCount(countResponse, 2l); countResponse = client().prepareCount("test") - .setQuery(filteredQuery(matchAllQuery(), termsLookupFilter("term").lookupIndex("lookup2").lookupType("type").lookupId("2").lookupPath("arr.term")) - ).execute().actionGet(); - assertNoFailures(countResponse); + .setQuery(filteredQuery(matchAllQuery(), termsLookupFilter("term").lookupIndex("lookup2").lookupType("type").lookupId("2").lookupPath("arr.term"))).get(); assertHitCount(countResponse, 1l); countResponse = client().prepareCount("test") - .setQuery(filteredQuery(matchAllQuery(), termsLookupFilter("term").lookupIndex("lookup2").lookupType("type").lookupId("3").lookupPath("arr.term")) - ).execute().actionGet(); - assertNoFailures(countResponse); + .setQuery(filteredQuery(matchAllQuery(), termsLookupFilter("term").lookupIndex("lookup2").lookupType("type").lookupId("3").lookupPath("arr.term"))).get(); assertHitCount(countResponse, 2l); countResponse = client().prepareCount("test") - .setQuery(filteredQuery(matchAllQuery(), termsLookupFilter("not_exists").lookupIndex("lookup2").lookupType("type").lookupId("3").lookupPath("arr.term")) - ).execute().actionGet(); - assertNoFailures(countResponse); + .setQuery(filteredQuery(matchAllQuery(), termsLookupFilter("not_exists").lookupIndex("lookup2").lookupType("type").lookupId("3").lookupPath("arr.term"))).get(); assertHitCount(countResponse, 0l); } @@ -816,31 +686,26 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest { createIndex("test"); ensureGreen(); - client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject() - .field("field1", "value1") - .endObject()).execute().actionGet(); - client().prepareIndex("test", "type2", "2").setSource(jsonBuilder().startObject() - .field("field1", "value2") - .endObject()).execute().actionGet(); + client().prepareIndex("test", "type1", "1").setSource("field1", "value1").get(); + client().prepareIndex("test", "type2", "2").setSource("field1", "value2").get(); + refresh(); - client().admin().indices().prepareRefresh().execute().actionGet(); - - CountResponse countResponse = client().prepareCount().setQuery(QueryBuilders.constantScoreQuery(FilterBuilders.idsFilter("type1", "type2").ids("1", "2"))).execute().actionGet(); + CountResponse countResponse = client().prepareCount().setQuery(QueryBuilders.constantScoreQuery(FilterBuilders.idsFilter("type1", "type2").ids("1", "2"))).get(); assertHitCount(countResponse, 2l); - countResponse = client().prepareCount().setQuery(QueryBuilders.constantScoreQuery(FilterBuilders.idsFilter().ids("1", "2"))).execute().actionGet(); + countResponse = client().prepareCount().setQuery(QueryBuilders.constantScoreQuery(FilterBuilders.idsFilter().ids("1", "2"))).get(); assertHitCount(countResponse, 2l); - countResponse = client().prepareCount().setQuery(QueryBuilders.constantScoreQuery(FilterBuilders.idsFilter("type1").ids("1", "2"))).execute().actionGet(); + countResponse = client().prepareCount().setQuery(QueryBuilders.constantScoreQuery(FilterBuilders.idsFilter("type1").ids("1", "2"))).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount().setQuery(QueryBuilders.constantScoreQuery(FilterBuilders.idsFilter().ids("1"))).execute().actionGet(); + countResponse = client().prepareCount().setQuery(QueryBuilders.constantScoreQuery(FilterBuilders.idsFilter().ids("1"))).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount().setQuery(QueryBuilders.constantScoreQuery(FilterBuilders.idsFilter(null).ids("1"))).execute().actionGet(); + countResponse = client().prepareCount().setQuery(QueryBuilders.constantScoreQuery(FilterBuilders.idsFilter(null).ids("1"))).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount().setQuery(QueryBuilders.constantScoreQuery(FilterBuilders.idsFilter("type1", "type2", "type3").ids("1", "2", "3", "4"))).execute().actionGet(); + countResponse = client().prepareCount().setQuery(QueryBuilders.constantScoreQuery(FilterBuilders.idsFilter("type1", "type2", "type3").ids("1", "2", "3", "4"))).get(); assertHitCount(countResponse, 2l); } @@ -849,275 +714,133 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest { createIndex("test"); ensureGreen(); - client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject() - .field("field1", "value1") - .endObject()).execute().actionGet(); - client().prepareIndex("test", "type2", "2").setSource(jsonBuilder().startObject() - .field("field1", "value2") - .endObject()).execute().actionGet(); + client().prepareIndex("test", "type1", "1").setSource("field1", "value1").get(); + client().prepareIndex("test", "type2", "2").setSource("field1", "value2").get(); + refresh(); - client().admin().indices().prepareRefresh().execute().actionGet(); - - CountResponse countResponse = client().prepareCount().setQuery(QueryBuilders.idsQuery("type1", "type2").ids("1", "2")).execute().actionGet(); + CountResponse countResponse = client().prepareCount().setQuery(QueryBuilders.idsQuery("type1", "type2").ids("1", "2")).get(); assertHitCount(countResponse, 2l); - countResponse = client().prepareCount().setQuery(QueryBuilders.idsQuery().ids("1")).execute().actionGet(); + countResponse = client().prepareCount().setQuery(QueryBuilders.idsQuery().ids("1")).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount().setQuery(QueryBuilders.idsQuery().ids("1", "2")).execute().actionGet(); + countResponse = client().prepareCount().setQuery(QueryBuilders.idsQuery().ids("1", "2")).get(); assertHitCount(countResponse, 2l); - - countResponse = client().prepareCount().setQuery(QueryBuilders.idsQuery("type1").ids("1", "2")).execute().actionGet(); + countResponse = client().prepareCount().setQuery(QueryBuilders.idsQuery("type1").ids("1", "2")).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount().setQuery(QueryBuilders.idsQuery().ids("1")).execute().actionGet(); + countResponse = client().prepareCount().setQuery(QueryBuilders.idsQuery().ids("1")).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount().setQuery(QueryBuilders.idsQuery(null).ids("1")).execute().actionGet(); + countResponse = client().prepareCount().setQuery(QueryBuilders.idsQuery(null).ids("1")).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount().setQuery(QueryBuilders.idsQuery("type1", "type2", "type3").ids("1", "2", "3", "4")).execute().actionGet(); + countResponse = client().prepareCount().setQuery(QueryBuilders.idsQuery("type1", "type2", "type3").ids("1", "2", "3", "4")).get(); assertHitCount(countResponse, 2l); } @Test public void testNumericTermsAndRanges() throws Exception { - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)) - .addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties") - .startObject("num_byte").field("type", "byte").endObject() - .startObject("num_short").field("type", "short").endObject() - .startObject("num_integer").field("type", "integer").endObject() - .startObject("num_long").field("type", "long").endObject() - .startObject("num_float").field("type", "float").endObject() - .startObject("num_double").field("type", "double").endObject() - .endObject().endObject().endObject()) - .execute().actionGet(); + assertAcked(client().admin().indices().prepareCreate("test") + .setSettings(SETTING_NUMBER_OF_SHARDS, 1) + .addMapping("type1", + "num_byte", "type=byte", "num_short", "type=short", + "num_integer", "type=integer", "num_long", "type=long", + "num_float", "type=float", "num_double", "type=double")); ensureGreen(); - client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject() - .field("num_byte", 1) - .field("num_short", 1) - .field("num_integer", 1) - .field("num_long", 1) - .field("num_float", 1) - .field("num_double", 1) - .endObject()) - .execute().actionGet(); + client().prepareIndex("test", "type1", "1").setSource("num_byte", 1, "num_short", 1, "num_integer", 1, + "num_long", 1, "num_float", 1, "num_double", 1).get(); - client().prepareIndex("test", "type1", "2").setSource(jsonBuilder().startObject() - .field("num_byte", 2) - .field("num_short", 2) - .field("num_integer", 2) - .field("num_long", 2) - .field("num_float", 2) - .field("num_double", 2) - .endObject()) - .execute().actionGet(); + client().prepareIndex("test", "type1", "2").setSource("num_byte", 2, "num_short", 2, "num_integer", 2, + "num_long", 2, "num_float", 2, "num_double", 2).get(); - client().prepareIndex("test", "type1", "17").setSource(jsonBuilder().startObject() - .field("num_byte", 17) - .field("num_short", 17) - .field("num_integer", 17) - .field("num_long", 17) - .field("num_float", 17) - .field("num_double", 17) - .endObject()) - .execute().actionGet(); - - client().admin().indices().prepareRefresh().execute().actionGet(); + client().prepareIndex("test", "type1", "17").setSource("num_byte", 17, "num_short", 17, "num_integer", 17, + "num_long", 17, "num_float", 17, "num_double", 17).get(); + refresh(); CountResponse countResponse; - logger.info("--> term query on 1"); - countResponse = client().prepareCount("test").setQuery(termQuery("num_byte", 1)).execute().actionGet(); + countResponse = client().prepareCount("test").setQuery(termQuery("num_byte", 1)).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount("test").setQuery(termQuery("num_short", 1)).execute().actionGet(); + countResponse = client().prepareCount("test").setQuery(termQuery("num_short", 1)).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount("test").setQuery(termQuery("num_integer", 1)).execute().actionGet(); + countResponse = client().prepareCount("test").setQuery(termQuery("num_integer", 1)).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount("test").setQuery(termQuery("num_long", 1)).execute().actionGet(); + countResponse = client().prepareCount("test").setQuery(termQuery("num_long", 1)).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount("test").setQuery(termQuery("num_float", 1)).execute().actionGet(); + countResponse = client().prepareCount("test").setQuery(termQuery("num_float", 1)).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount("test").setQuery(termQuery("num_double", 1)).execute().actionGet(); + countResponse = client().prepareCount("test").setQuery(termQuery("num_double", 1)).get(); assertHitCount(countResponse, 1l); logger.info("--> terms query on 1"); - countResponse = client().prepareCount("test").setQuery(termsQuery("num_byte", new int[]{1})).execute().actionGet(); + countResponse = client().prepareCount("test").setQuery(termsQuery("num_byte", new int[]{1})).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount("test").setQuery(termsQuery("num_short", new int[]{1})).execute().actionGet(); + countResponse = client().prepareCount("test").setQuery(termsQuery("num_short", new int[]{1})).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount("test").setQuery(termsQuery("num_integer", new int[]{1})).execute().actionGet(); + countResponse = client().prepareCount("test").setQuery(termsQuery("num_integer", new int[]{1})).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount("test").setQuery(termsQuery("num_long", new int[]{1})).execute().actionGet(); + countResponse = client().prepareCount("test").setQuery(termsQuery("num_long", new int[]{1})).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount("test").setQuery(termsQuery("num_float", new double[]{1})).execute().actionGet(); + countResponse = client().prepareCount("test").setQuery(termsQuery("num_float", new double[]{1})).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount("test").setQuery(termsQuery("num_double", new double[]{1})).execute().actionGet(); + countResponse = client().prepareCount("test").setQuery(termsQuery("num_double", new double[]{1})).get(); assertHitCount(countResponse, 1l); logger.info("--> term filter on 1"); - countResponse = client().prepareCount("test").setQuery(filteredQuery(matchAllQuery(), termFilter("num_byte", 1))).execute().actionGet(); + countResponse = client().prepareCount("test").setQuery(filteredQuery(matchAllQuery(), termFilter("num_byte", 1))).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount("test").setQuery(filteredQuery(matchAllQuery(), termFilter("num_short", 1))).execute().actionGet(); + countResponse = client().prepareCount("test").setQuery(filteredQuery(matchAllQuery(), termFilter("num_short", 1))).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount("test").setQuery(filteredQuery(matchAllQuery(), termFilter("num_integer", 1))).execute().actionGet(); + countResponse = client().prepareCount("test").setQuery(filteredQuery(matchAllQuery(), termFilter("num_integer", 1))).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount("test").setQuery(filteredQuery(matchAllQuery(), termFilter("num_long", 1))).execute().actionGet(); + countResponse = client().prepareCount("test").setQuery(filteredQuery(matchAllQuery(), termFilter("num_long", 1))).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount("test").setQuery(filteredQuery(matchAllQuery(), termFilter("num_float", 1))).execute().actionGet(); + countResponse = client().prepareCount("test").setQuery(filteredQuery(matchAllQuery(), termFilter("num_float", 1))).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount("test").setQuery(filteredQuery(matchAllQuery(), termFilter("num_double", 1))).execute().actionGet(); + countResponse = client().prepareCount("test").setQuery(filteredQuery(matchAllQuery(), termFilter("num_double", 1))).get(); assertHitCount(countResponse, 1l); logger.info("--> terms filter on 1"); - countResponse = client().prepareCount("test").setQuery(filteredQuery(matchAllQuery(), termsFilter("num_byte", new int[]{1}))).execute().actionGet(); + countResponse = client().prepareCount("test").setQuery(filteredQuery(matchAllQuery(), termsFilter("num_byte", new int[]{1}))).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount("test").setQuery(filteredQuery(matchAllQuery(), termsFilter("num_short", new int[]{1}))).execute().actionGet(); + countResponse = client().prepareCount("test").setQuery(filteredQuery(matchAllQuery(), termsFilter("num_short", new int[]{1}))).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount("test").setQuery(filteredQuery(matchAllQuery(), termsFilter("num_integer", new int[]{1}))).execute().actionGet(); + countResponse = client().prepareCount("test").setQuery(filteredQuery(matchAllQuery(), termsFilter("num_integer", new int[]{1}))).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount("test").setQuery(filteredQuery(matchAllQuery(), termsFilter("num_long", new int[]{1}))).execute().actionGet(); + countResponse = client().prepareCount("test").setQuery(filteredQuery(matchAllQuery(), termsFilter("num_long", new int[]{1}))).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount("test").setQuery(filteredQuery(matchAllQuery(), termsFilter("num_float", new int[]{1}))).execute().actionGet(); + countResponse = client().prepareCount("test").setQuery(filteredQuery(matchAllQuery(), termsFilter("num_float", new int[]{1}))).get(); assertHitCount(countResponse, 1l); - countResponse = client().prepareCount("test").setQuery(filteredQuery(matchAllQuery(), termsFilter("num_double", new int[]{1}))).execute().actionGet(); + countResponse = client().prepareCount("test").setQuery(filteredQuery(matchAllQuery(), termsFilter("num_double", new int[]{1}))).get(); assertHitCount(countResponse, 1l); } - @Test - public void testNumericRangeFilter_2826() throws Exception { - client().admin().indices().prepareCreate("test").setSettings( - ImmutableSettings.settingsBuilder() - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 0) - ) - .addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties") - .startObject("num_byte").field("type", "byte").endObject() - .startObject("num_short").field("type", "short").endObject() - .startObject("num_integer").field("type", "integer").endObject() - .startObject("num_long").field("type", "long").endObject() - .startObject("num_float").field("type", "float").endObject() - .startObject("num_double").field("type", "double").endObject() - .endObject().endObject().endObject()) - .execute().actionGet(); - ensureGreen(); - - client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject() - .field("field1", "test1") - .field("num_long", 1) - .endObject()) - .execute().actionGet(); - - client().prepareIndex("test", "type1", "2").setSource(jsonBuilder().startObject() - .field("field1", "test1") - .field("num_long", 2) - .endObject()) - .execute().actionGet(); - - client().prepareIndex("test", "type1", "3").setSource(jsonBuilder().startObject() - .field("field1", "test2") - .field("num_long", 3) - .endObject()) - .execute().actionGet(); - - client().prepareIndex("test", "type1", "4").setSource(jsonBuilder().startObject() - .field("field1", "test2") - .field("num_long", 4) - .endObject()) - .execute().actionGet(); - - client().admin().indices().prepareRefresh().execute().actionGet(); - CountResponse response = client().prepareCount("test").setQuery( - filteredQuery(matchAllQuery(), FilterBuilders.boolFilter() - .should(rangeFilter("num_long", 1, 2)) - .should(rangeFilter("num_long", 3, 4))) - ).execute().actionGet(); - assertHitCount(response, 4l); - - // This made 2826 fail! (only with bit based filters) - response = client().prepareCount("test").setQuery( - filteredQuery(matchAllQuery(), FilterBuilders.boolFilter() - .should(rangeFilter("num_long", 1, 2)) - .should(rangeFilter("num_long", 3, 4))) - ).execute().actionGet(); - - assertHitCount(response, 4l); - - // This made #2979 fail! - response = client().prepareCount("test").setQuery( - filteredQuery(matchAllQuery(), FilterBuilders.boolFilter() - .must(FilterBuilders.termFilter("field1", "test1")) - .should(rangeFilter("num_long", 1, 2)) - .should(rangeFilter("num_long", 3, 4))) - ).execute().actionGet(); - - assertHitCount(response, 2l); - } - @Test // see #2994 public void testSimpleSpan() throws ElasticSearchException, IOException { - - client().admin().indices().prepareCreate("test").setSettings( - ImmutableSettings.settingsBuilder() - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 0) - ) - .execute().actionGet(); + assertAcked(prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1, SETTING_NUMBER_OF_REPLICAS, 0)); ensureGreen(); - 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(); + client().prepareIndex("test", "test", "1").setSource("description", "foo other anything bar").get(); + client().prepareIndex("test", "test", "2").setSource("description", "foo other anything").get(); + client().prepareIndex("test", "test", "3").setSource("description", "foo other").get(); + client().prepareIndex("test", "test", "4").setSource("description", "foo").get(); + refresh(); CountResponse response = client().prepareCount("test") - .setQuery(QueryBuilders.spanOrQuery().clause(QueryBuilders.spanTermQuery("description", "bar"))) - .execute().actionGet(); - assertNoFailures(response); + .setQuery(QueryBuilders.spanOrQuery().clause(QueryBuilders.spanTermQuery("description", "bar"))).get(); assertHitCount(response, 1l); response = client().prepareCount("test") - .setQuery(QueryBuilders.spanOrQuery().clause(QueryBuilders.spanTermQuery("test.description", "bar"))) - .execute().actionGet(); - assertNoFailures(response); + .setQuery(QueryBuilders.spanOrQuery().clause(QueryBuilders.spanTermQuery("test.description", "bar"))).get(); assertHitCount(response, 1l); response = client().prepareCount("test").setQuery( QueryBuilders.spanNearQuery() .clause(QueryBuilders.spanTermQuery("description", "foo")) .clause(QueryBuilders.spanTermQuery("test.description", "other")) - .slop(3)).execute().actionGet(); - assertNoFailures(response); + .slop(3)).get(); assertHitCount(response, 3l); } - - public static FilterBuilder rangeFilter(String field, Object from, Object to) { - if (randomBoolean()) { - if (randomBoolean()) { - return FilterBuilders.rangeFilter(field).from(from).to(to); - } else { - return FilterBuilders.rangeFilter(field).from(from).to(to).setExecution("fielddata"); - } - } else { - return FilterBuilders.numericRangeFilter(field).from(from).to(to); - } - } } diff --git a/src/test/java/org/elasticsearch/search/query/SimpleQueryTests.java b/src/test/java/org/elasticsearch/search/query/SimpleQueryTests.java index 95d5292a645..0bed9a45bc5 100644 --- a/src/test/java/org/elasticsearch/search/query/SimpleQueryTests.java +++ b/src/test/java/org/elasticsearch/search/query/SimpleQueryTests.java @@ -27,8 +27,6 @@ import org.elasticsearch.action.search.SearchPhaseExecutionException; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchType; import org.elasticsearch.action.search.ShardSearchFailure; -import org.elasticsearch.common.settings.ImmutableSettings; -import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.index.query.*; import org.elasticsearch.index.query.CommonTermsQueryBuilder.Operator; @@ -51,73 +49,71 @@ import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_SHARDS; import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; -import static org.elasticsearch.count.query.SimpleQueryTests.rangeFilter; import static org.elasticsearch.index.query.FilterBuilders.*; import static org.elasticsearch.index.query.QueryBuilders.*; import static org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders.scriptFunction; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*; import static org.hamcrest.Matchers.*; -/** - * - */ + public class SimpleQueryTests extends ElasticsearchIntegrationTest { @Test // see https://github.com/elasticsearch/elasticsearch/issues/3177 public void testIssue3177() { - prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).get(); - client().prepareIndex("test", "type1", "1").setSource("field1", "value1").execute().actionGet(); - client().prepareIndex("test", "type1", "2").setSource("field1", "value2").execute().actionGet(); - client().prepareIndex("test", "type1", "3").setSource("field1", "value3").execute().actionGet(); + assertAcked(prepareCreate("test").setSettings(settingsBuilder().put(SETTING_NUMBER_OF_SHARDS, 1))); + client().prepareIndex("test", "type1", "1").setSource("field1", "value1").get(); + client().prepareIndex("test", "type1", "2").setSource("field1", "value2").get(); + client().prepareIndex("test", "type1", "3").setSource("field1", "value3").get(); ensureGreen(); waitForRelocation(); optimize(); refresh(); - assertThat( + assertHitCount( client().prepareSearch() .setQuery(matchAllQuery()) .setFilter( - FilterBuilders.andFilter( - FilterBuilders.queryFilter(matchAllQuery()), - FilterBuilders.notFilter(FilterBuilders.andFilter(FilterBuilders.queryFilter(QueryBuilders.termQuery("field1", "value1")), - FilterBuilders.queryFilter(QueryBuilders.termQuery("field1", "value2")))))).execute().actionGet().getHits().getTotalHits(), - equalTo(3l)); - assertThat( + andFilter( + queryFilter(matchAllQuery()), + notFilter(andFilter(queryFilter(termQuery("field1", "value1")), + queryFilter(termQuery("field1", "value2")))))).get(), + 3l); + assertHitCount( client().prepareSearch() .setQuery( - QueryBuilders.filteredQuery( - QueryBuilders.boolQuery().should(QueryBuilders.termQuery("field1", "value1")).should(QueryBuilders.termQuery("field1", "value2")) - .should(QueryBuilders.termQuery("field1", "value3")), - FilterBuilders.notFilter(FilterBuilders.andFilter(FilterBuilders.queryFilter(QueryBuilders.termQuery("field1", "value1")), - FilterBuilders.queryFilter(QueryBuilders.termQuery("field1", "value2")))))).execute().actionGet().getHits().getTotalHits(), - equalTo(3l)); - assertThat(client().prepareSearch().setQuery(matchAllQuery()).setFilter(FilterBuilders.notFilter(FilterBuilders.termFilter("field1", "value3"))).execute().actionGet() - .getHits().getTotalHits(), equalTo(2l)); + filteredQuery( + boolQuery().should(termQuery("field1", "value1")).should(termQuery("field1", "value2")) + .should(termQuery("field1", "value3")), + notFilter(andFilter(queryFilter(termQuery("field1", "value1")), + queryFilter(termQuery("field1", "value2")))))).get(), + 3l); + assertHitCount( + client().prepareSearch().setQuery(matchAllQuery()).setFilter(notFilter(termFilter("field1", "value3"))).get(), + 2l); } @Test public void passQueryAsStringTest() throws Exception { - client().admin().indices().prepareCreate("test").setSettings("index.number_of_shards", 1).execute().actionGet(); + assertAcked(client().admin().indices().prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1)); - client().prepareIndex("test", "type1", "1").setSource("field1", "value1_1", "field2", "value2_1").setRefresh(true).execute().actionGet(); + client().prepareIndex("test", "type1", "1").setSource("field1", "value1_1", "field2", "value2_1").setRefresh(true).get(); - SearchResponse searchResponse = client().prepareSearch().setQuery("{ \"term\" : { \"field1\" : \"value1_1\" }}").execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); + SearchResponse searchResponse = client().prepareSearch().setQuery("{ \"term\" : { \"field1\" : \"value1_1\" }}").get(); + assertHitCount(searchResponse, 1l); } @Test public void testIndexOptions() throws Exception { - client().admin().indices().prepareCreate("test") + assertAcked(client().admin().indices().prepareCreate("test") .addMapping("type1", "field1", "type=string,index_options=docs") - .setSettings("index.number_of_shards", 1).get(); + .setSettings(SETTING_NUMBER_OF_SHARDS, 1)); - client().prepareIndex("test", "type1", "1").setSource("field1", "quick brown fox", "field2", "quick brown fox").execute().actionGet(); - client().prepareIndex("test", "type1", "2").setSource("field1", "quick lazy huge brown fox", "field2", "quick lazy huge brown fox").setRefresh(true).execute().actionGet(); + client().prepareIndex("test", "type1", "1").setSource("field1", "quick brown fox", "field2", "quick brown fox").get(); + client().prepareIndex("test", "type1", "2").setSource("field1", "quick lazy huge brown fox", "field2", "quick lazy huge brown fox").setRefresh(true).get(); - SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("field2", "quick brown").type(MatchQueryBuilder.Type.PHRASE).slop(0)).get(); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); + SearchResponse searchResponse = client().prepareSearch().setQuery(matchQuery("field2", "quick brown").type(MatchQueryBuilder.Type.PHRASE).slop(0)).get(); + assertHitCount(searchResponse, 1l); try { - client().prepareSearch().setQuery(QueryBuilders.matchQuery("field1", "quick brown").type(MatchQueryBuilder.Type.PHRASE).slop(0)).get(); + client().prepareSearch().setQuery(matchQuery("field1", "quick brown").type(MatchQueryBuilder.Type.PHRASE).slop(0)).get(); } catch (SearchPhaseExecutionException e) { assertTrue("wrong exception message " + e.getMessage(), e.getMessage().endsWith("IllegalStateException[field \"field1\" was indexed without position data; cannot run PhraseQuery (term=quick)]; }")); } @@ -129,33 +125,29 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest { createIndex("test"); indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("field1", "quick brown fox", "field2", "quick brown fox"), client().prepareIndex("test", "type1", "2").setSource("field1", "quick lazy huge brown fox", "field2", "quick lazy huge brown fox")); ensureYellow(); - SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.constantScoreQuery(QueryBuilders.matchQuery("field1", "quick"))).get(); - SearchHits hits = searchResponse.getHits(); - assertThat(hits.totalHits(), equalTo(2l)); - for (SearchHit searchHit : hits) { - assertThat(searchHit.getScore(), equalTo(1.0f)); + SearchResponse searchResponse = client().prepareSearch().setQuery(constantScoreQuery(matchQuery("field1", "quick"))).get(); + assertHitCount(searchResponse, 2l); + for (SearchHit searchHit : searchResponse.getHits().hits()) { + assertSearchHit(searchHit, hasScore(1.0f)); } searchResponse = client().prepareSearch("test").setQuery( - QueryBuilders.boolQuery().must(QueryBuilders.matchAllQuery()).must( - QueryBuilders.constantScoreQuery(QueryBuilders.matchQuery("field1", "quick")).boost(1.0f + getRandom().nextFloat()))).get(); - hits = searchResponse.getHits(); - assertThat(hits.totalHits(), equalTo(2l)); - assertThat(hits.getAt(0).score(), equalTo(hits.getAt(1).score())); - - client().prepareSearch("test").setQuery(QueryBuilders.constantScoreQuery(QueryBuilders.matchQuery("field1", "quick")).boost(1.0f + getRandom().nextFloat())).get(); - hits = searchResponse.getHits(); - assertThat(hits.totalHits(), equalTo(2l)); - assertThat(hits.getAt(0).score(), equalTo(hits.getAt(1).score())); - + boolQuery().must(matchAllQuery()).must( + constantScoreQuery(matchQuery("field1", "quick")).boost(1.0f + getRandom().nextFloat()))).get(); + assertHitCount(searchResponse, 2l); + assertFirstHit(searchResponse, hasScore(searchResponse.getHits().getAt(1).score())); + + client().prepareSearch("test").setQuery(constantScoreQuery(matchQuery("field1", "quick")).boost(1.0f + getRandom().nextFloat())).get(); + assertHitCount(searchResponse, 2l); + assertFirstHit(searchResponse, hasScore(searchResponse.getHits().getAt(1).score())); + searchResponse = client().prepareSearch("test").setQuery( - QueryBuilders.constantScoreQuery(QueryBuilders.boolQuery().must(QueryBuilders.matchAllQuery()).must( - QueryBuilders.constantScoreQuery(QueryBuilders.matchQuery("field1", "quick")).boost(1.0f + (random.nextBoolean() ? 0.0f : random.nextFloat()))))).get(); - hits = searchResponse.getHits(); - assertThat(hits.totalHits(), equalTo(2l)); - assertThat(hits.getAt(0).score(), equalTo(hits.getAt(1).score())); - for (SearchHit searchHit : hits) { - assertThat(searchHit.getScore(), equalTo(1.0f)); + constantScoreQuery(boolQuery().must(matchAllQuery()).must( + constantScoreQuery(matchQuery("field1", "quick")).boost(1.0f + (random.nextBoolean()? 0.0f : random.nextFloat()))))).get(); + assertHitCount(searchResponse, 2l); + assertFirstHit(searchResponse, hasScore(searchResponse.getHits().getAt(1).score())); + for (SearchHit searchHit : searchResponse.getHits().hits()) { + assertSearchHit(searchHit, hasScore(1.0f)); } int num = atLeast(100); @@ -168,30 +160,30 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest { ensureYellow(); int queryRounds = atLeast(10); for (int i = 0; i < queryRounds; i++) { - MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("f", English.intToEnglish(between(0, num))); + MatchQueryBuilder matchQuery = matchQuery("f", English.intToEnglish(between(0, num))); searchResponse = client().prepareSearch("test_1").setQuery(matchQuery).setSize(num).get(); long totalHits = searchResponse.getHits().totalHits(); - hits = searchResponse.getHits(); + SearchHits hits = searchResponse.getHits(); for (SearchHit searchHit : hits) { - assertThat(searchHit.getScore(), equalTo(1.0f)); + assertSearchHit(searchHit, hasScore(1.0f)); } if (random.nextBoolean()) { searchResponse = client().prepareSearch("test_1").setQuery( - QueryBuilders.boolQuery().must(QueryBuilders.matchAllQuery()).must( - QueryBuilders.constantScoreQuery(matchQuery).boost(1.0f + (random.nextBoolean() ? 0.0f : random.nextFloat())))).setSize(num).get(); + boolQuery().must(matchAllQuery()).must( + constantScoreQuery(matchQuery).boost(1.0f + (random.nextBoolean()? 0.0f : random.nextFloat())))).setSize(num).get(); hits = searchResponse.getHits(); } else { - FilterBuilder filter = FilterBuilders.queryFilter(matchQuery); + FilterBuilder filter = queryFilter(matchQuery); searchResponse = client().prepareSearch("test_1").setQuery( - QueryBuilders.boolQuery().must(QueryBuilders.matchAllQuery()).must( - QueryBuilders.constantScoreQuery(filter).boost(1.0f + (random.nextBoolean() ? 0.0f : random.nextFloat())))).setSize(num).get(); + boolQuery().must(matchAllQuery()).must( + constantScoreQuery(filter).boost(1.0f + (random.nextBoolean()? 0.0f : random.nextFloat())))).setSize(num).get(); hits = searchResponse.getHits(); } assertThat(hits.totalHits(), equalTo(totalHits)); if (totalHits > 1) { float expected = hits.getAt(0).score(); for (SearchHit searchHit : hits) { - assertThat(searchHit.getScore(), equalTo(expected)); + assertSearchHit(searchHit, hasScore(expected)); } } } @@ -199,130 +191,122 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest { @Test // see #3521 public void testAllDocsQueryString() throws InterruptedException, ExecutionException { - client().admin().indices().prepareCreate("test") - .setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_replicas", 0)).execute().actionGet(); + assertAcked(client().admin().indices().prepareCreate("test").setSettings(SETTING_NUMBER_OF_REPLICAS, 0)); indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("foo", "bar"), client().prepareIndex("test", "type1", "2").setSource("foo", "bar") ); int iters = atLeast(100); for (int i = 0; i < iters; i++) { - SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(queryString("*:*^10.0").boost(10.0f)) - .execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); - - searchResponse = client().prepareSearch("test") - .setQuery(QueryBuilders.boolQuery().must(QueryBuilders.matchAllQuery()).must( - QueryBuilders.constantScoreQuery(QueryBuilders.matchAllQuery()))) - .execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); - assertThat((double) searchResponse.getHits().getAt(0).score(), closeTo(Math.sqrt(2), 0.1)); - assertThat((double) searchResponse.getHits().getAt(1).score(), closeTo(Math.sqrt(2), 0.1)); + SearchResponse searchResponse = client().prepareSearch("test").setQuery(queryString("*:*^10.0").boost(10.0f)).get(); + assertHitCount(searchResponse, 2l); + + searchResponse = client().prepareSearch("test").setQuery( + boolQuery().must(matchAllQuery()).must(constantScoreQuery(matchAllQuery()))).get(); + assertHitCount(searchResponse, 2l); + assertThat((double)searchResponse.getHits().getAt(0).score(), closeTo(Math.sqrt(2), 0.1)); + assertThat((double)searchResponse.getHits().getAt(1).score(),closeTo(Math.sqrt(2), 0.1)); } } - @Test public void testCommonTermsQuery() throws Exception { client().admin().indices().prepareCreate("test") .addMapping("type1", "field1", "type=string,analyzer=whitespace") - .setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet(); + .setSettings(SETTING_NUMBER_OF_SHARDS, 1).get(); indexRandom(true, client().prepareIndex("test", "type1", "3").setSource("field1", "quick lazy huge brown pidgin", "field2", "the quick lazy huge brown fox jumps over the tree"), client().prepareIndex("test", "type1", "1").setSource("field1", "the quick brown fox"), - client().prepareIndex("test", "type1", "2").setSource("field1", "the quick lazy huge brown fox jumps over the tree") - ); + client().prepareIndex("test", "type1", "2").setSource("field1", "the quick lazy huge brown fox jumps over the tree") ); - SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.commonTerms("field1", "the quick brown").cutoffFrequency(3).lowFreqOperator(Operator.OR)).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(3l)); - assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("1")); - assertThat(searchResponse.getHits().getHits()[1].getId(), equalTo("2")); - assertThat(searchResponse.getHits().getHits()[2].getId(), equalTo("3")); + SearchResponse searchResponse = client().prepareSearch().setQuery(commonTerms("field1", "the quick brown").cutoffFrequency(3).lowFreqOperator(Operator.OR)).get(); + assertHitCount(searchResponse, 3l); + assertFirstHit(searchResponse, hasId("1")); + assertSecondHit(searchResponse, hasId("2")); + assertThirdHit(searchResponse, hasId("3")); - searchResponse = client().prepareSearch().setQuery(QueryBuilders.commonTerms("field1", "the quick brown").cutoffFrequency(3).lowFreqOperator(Operator.AND)).execute().actionGet(); + searchResponse = client().prepareSearch().setQuery(commonTerms("field1", "the quick brown").cutoffFrequency(3).lowFreqOperator(Operator.AND)).get(); assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("1")); - assertThat(searchResponse.getHits().getHits()[1].getId(), equalTo("2")); + assertFirstHit(searchResponse, hasId("1")); + assertSecondHit(searchResponse, hasId("2")); // Default - searchResponse = client().prepareSearch().setQuery(QueryBuilders.commonTerms("field1", "the quick brown").cutoffFrequency(3)).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(3l)); - assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("1")); - assertThat(searchResponse.getHits().getHits()[1].getId(), equalTo("2")); - assertThat(searchResponse.getHits().getHits()[2].getId(), equalTo("3")); + searchResponse = client().prepareSearch().setQuery(commonTerms("field1", "the quick brown").cutoffFrequency(3)).get(); + assertHitCount(searchResponse, 3l); + assertFirstHit(searchResponse, hasId("1")); + assertSecondHit(searchResponse, hasId("2")); + assertThirdHit(searchResponse, hasId("3")); - searchResponse = client().prepareSearch().setQuery(QueryBuilders.commonTerms("field1", "the huge fox").lowFreqMinimumShouldMatch("2")).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("2")); - searchResponse = client().prepareSearch().setQuery(QueryBuilders.commonTerms("field1", "the lazy fox brown").cutoffFrequency(1).highFreqMinimumShouldMatch("3")).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("1")); - assertThat(searchResponse.getHits().getHits()[1].getId(), equalTo("2")); + searchResponse = client().prepareSearch().setQuery(commonTerms("field1", "the huge fox").lowFreqMinimumShouldMatch("2")).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("2")); - searchResponse = client().prepareSearch().setQuery(QueryBuilders.commonTerms("field1", "the lazy fox brown").cutoffFrequency(1).highFreqMinimumShouldMatch("4")).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("2")); + searchResponse = client().prepareSearch().setQuery(commonTerms("field1", "the lazy fox brown").cutoffFrequency(1).highFreqMinimumShouldMatch("3")).get(); + assertHitCount(searchResponse, 2l); + assertFirstHit(searchResponse, hasId("1")); + assertSecondHit(searchResponse, hasId("2")); - searchResponse = client().prepareSearch().setQuery("{ \"common\" : { \"field1\" : { \"query\" : \"the lazy fox brown\", \"cutoff_frequency\" : 1, \"minimum_should_match\" : { \"high_freq\" : 4 } } } }").execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("2")); + searchResponse = client().prepareSearch().setQuery(commonTerms("field1", "the lazy fox brown").cutoffFrequency(1).highFreqMinimumShouldMatch("4")).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("2")); + + searchResponse = client().prepareSearch().setQuery("{ \"common\" : { \"field1\" : { \"query\" : \"the lazy fox brown\", \"cutoff_frequency\" : 1, \"minimum_should_match\" : { \"high_freq\" : 4 } } } }").get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("2")); // Default - searchResponse = client().prepareSearch().setQuery(QueryBuilders.commonTerms("field1", "the lazy fox brown").cutoffFrequency(1)).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("2")); + searchResponse = client().prepareSearch().setQuery(commonTerms("field1", "the lazy fox brown").cutoffFrequency(1)).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("2")); - searchResponse = client().prepareSearch().setQuery(QueryBuilders.commonTerms("field1", "the quick brown").cutoffFrequency(3).analyzer("stop")).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(3l)); + searchResponse = client().prepareSearch().setQuery(commonTerms("field1", "the quick brown").cutoffFrequency(3).analyzer("stop")).get(); + assertHitCount(searchResponse, 3l); // standard drops "the" since its a stopword - assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("1")); - assertThat(searchResponse.getHits().getHits()[1].getId(), equalTo("3")); - assertThat(searchResponse.getHits().getHits()[2].getId(), equalTo("2")); + assertFirstHit(searchResponse, hasId("1")); + assertSecondHit(searchResponse, hasId("3")); + assertThirdHit(searchResponse, hasId("2")); // try the same with match query - searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("field1", "the quick brown").cutoffFrequency(3).operator(MatchQueryBuilder.Operator.AND)).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("1")); - assertThat(searchResponse.getHits().getHits()[1].getId(), equalTo("2")); + searchResponse = client().prepareSearch().setQuery(matchQuery("field1", "the quick brown").cutoffFrequency(3).operator(MatchQueryBuilder.Operator.AND)).get(); + assertHitCount(searchResponse, 2l); + assertFirstHit(searchResponse, hasId("1")); + assertSecondHit(searchResponse, hasId("2")); - searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("field1", "the quick brown").cutoffFrequency(3).operator(MatchQueryBuilder.Operator.OR)).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(3l)); - assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("1")); - assertThat(searchResponse.getHits().getHits()[1].getId(), equalTo("2")); - assertThat(searchResponse.getHits().getHits()[2].getId(), equalTo("3")); + searchResponse = client().prepareSearch().setQuery(matchQuery("field1", "the quick brown").cutoffFrequency(3).operator(MatchQueryBuilder.Operator.OR)).get(); + assertHitCount(searchResponse, 3l); + assertFirstHit(searchResponse, hasId("1")); + assertSecondHit(searchResponse, hasId("2")); + assertThirdHit(searchResponse, hasId("3")); - searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("field1", "the quick brown").cutoffFrequency(3).operator(MatchQueryBuilder.Operator.AND).analyzer("stop")).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(3l)); + searchResponse = client().prepareSearch().setQuery(matchQuery("field1", "the quick brown").cutoffFrequency(3).operator(MatchQueryBuilder.Operator.AND).analyzer("stop")).get(); + assertHitCount(searchResponse, 3l); // standard drops "the" since its a stopword - assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("1")); - assertThat(searchResponse.getHits().getHits()[1].getId(), equalTo("3")); - assertThat(searchResponse.getHits().getHits()[2].getId(), equalTo("2")); + assertFirstHit(searchResponse, hasId("1")); + assertSecondHit(searchResponse, hasId("3")); + assertThirdHit(searchResponse, hasId("2")); // try the same with multi match query - searchResponse = client().prepareSearch().setQuery(QueryBuilders.multiMatchQuery("the quick brown", "field1", "field2").cutoffFrequency(3).operator(MatchQueryBuilder.Operator.AND)).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(3l)); - assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("3")); // better score due to different query stats - assertThat(searchResponse.getHits().getHits()[1].getId(), equalTo("1")); - assertThat(searchResponse.getHits().getHits()[2].getId(), equalTo("2")); + searchResponse = client().prepareSearch().setQuery(multiMatchQuery("the quick brown", "field1", "field2").cutoffFrequency(3).operator(MatchQueryBuilder.Operator.AND)).get(); + assertHitCount(searchResponse, 3l); + assertFirstHit(searchResponse, hasId("3")); // better score due to different query stats + assertSecondHit(searchResponse, hasId("1")); + assertThirdHit(searchResponse, hasId("2")); } @Test public void testOmitTermFreqsAndPositions() throws Exception { // backwards compat test! - client().admin().indices().prepareCreate("test") + assertAcked(client().admin().indices().prepareCreate("test") .addMapping("type1", "field1", "type=string,omit_term_freq_and_positions=true") - .setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).get(); + .setSettings(SETTING_NUMBER_OF_SHARDS, 1)); indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("field1", "quick brown fox", "field2", "quick brown fox"), client().prepareIndex("test", "type1", "2").setSource("field1", "quick lazy huge brown fox", "field2", "quick lazy huge brown fox")); - - SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("field2", "quick brown").type(MatchQueryBuilder.Type.PHRASE).slop(0)).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); + SearchResponse searchResponse = client().prepareSearch().setQuery(matchQuery("field2", "quick brown").type(MatchQueryBuilder.Type.PHRASE).slop(0)).get(); + assertHitCount(searchResponse, 1l); try { - client().prepareSearch().setQuery(QueryBuilders.matchQuery("field1", "quick brown").type(MatchQueryBuilder.Type.PHRASE).slop(0)).execute().actionGet(); + client().prepareSearch().setQuery(matchQuery("field1", "quick brown").type(MatchQueryBuilder.Type.PHRASE).slop(0)).get(); + fail("SearchPhaseExecutionException should have been thrown"); } catch (SearchPhaseExecutionException e) { assertTrue(e.getMessage().endsWith("IllegalStateException[field \"field1\" was indexed without position data; cannot run PhraseQuery (term=quick)]; }")); } @@ -330,69 +314,65 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest { @Test public void queryStringAnalyzedWildcard() throws Exception { - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet(); + assertAcked(client().admin().indices().prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1)); - client().prepareIndex("test", "type1", "1").setSource("field1", "value_1", "field2", "value_2").execute().actionGet(); + client().prepareIndex("test", "type1", "1").setSource("field1", "value_1", "field2", "value_2").get(); + refresh(); - client().admin().indices().prepareRefresh().execute().actionGet(); + SearchResponse searchResponse = client().prepareSearch().setQuery(queryString("value*").analyzeWildcard(true)).get(); + assertHitCount(searchResponse, 1l); - SearchResponse searchResponse = client().prepareSearch().setQuery(queryString("value*").analyzeWildcard(true)).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); + searchResponse = client().prepareSearch().setQuery(queryString("*ue*").analyzeWildcard(true)).get(); + assertHitCount(searchResponse, 1l); - searchResponse = client().prepareSearch().setQuery(queryString("*ue*").analyzeWildcard(true)).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); + searchResponse = client().prepareSearch().setQuery(queryString("*ue_1").analyzeWildcard(true)).get(); + assertHitCount(searchResponse, 1l); - searchResponse = client().prepareSearch().setQuery(queryString("*ue_1").analyzeWildcard(true)).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); + searchResponse = client().prepareSearch().setQuery(queryString("val*e_1").analyzeWildcard(true)).get(); + assertHitCount(searchResponse, 1l); - searchResponse = client().prepareSearch().setQuery(queryString("val*e_1").analyzeWildcard(true)).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); - - searchResponse = client().prepareSearch().setQuery(queryString("v?l*e?1").analyzeWildcard(true)).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); + searchResponse = client().prepareSearch().setQuery(queryString("v?l*e?1").analyzeWildcard(true)).get(); + assertHitCount(searchResponse, 1l); } @Test public void testLowercaseExpandedTerms() { - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet(); + assertAcked(client().admin().indices().prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1)); - client().prepareIndex("test", "type1", "1").setSource("field1", "value_1", "field2", "value_2").execute().actionGet(); + client().prepareIndex("test", "type1", "1").setSource("field1", "value_1", "field2", "value_2").get(); + refresh(); - client().admin().indices().prepareRefresh().execute().actionGet(); - - SearchResponse searchResponse = client().prepareSearch().setQuery(queryString("VALUE_3~1").lowercaseExpandedTerms(true)).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); - searchResponse = client().prepareSearch().setQuery(queryString("VALUE_3~1").lowercaseExpandedTerms(false)).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(0l)); - searchResponse = client().prepareSearch().setQuery(queryString("ValUE_*").lowercaseExpandedTerms(true)).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); - searchResponse = client().prepareSearch().setQuery(queryString("vAl*E_1")).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); - searchResponse = client().prepareSearch().setQuery(queryString("[VALUE_1 TO VALUE_3]")).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); - searchResponse = client().prepareSearch().setQuery(queryString("[VALUE_1 TO VALUE_3]").lowercaseExpandedTerms(false)).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(0l)); + SearchResponse searchResponse = client().prepareSearch().setQuery(queryString("VALUE_3~1").lowercaseExpandedTerms(true)).get(); + assertHitCount(searchResponse, 1l); + searchResponse = client().prepareSearch().setQuery(queryString("VALUE_3~1").lowercaseExpandedTerms(false)).get(); + assertHitCount(searchResponse, 0l); + searchResponse = client().prepareSearch().setQuery(queryString("ValUE_*").lowercaseExpandedTerms(true)).get(); + assertHitCount(searchResponse, 1l); + searchResponse = client().prepareSearch().setQuery(queryString("vAl*E_1")).get(); + assertHitCount(searchResponse, 1l); + searchResponse = client().prepareSearch().setQuery(queryString("[VALUE_1 TO VALUE_3]")).get(); + assertHitCount(searchResponse, 1l); + searchResponse = client().prepareSearch().setQuery(queryString("[VALUE_1 TO VALUE_3]").lowercaseExpandedTerms(false)).get(); + assertHitCount(searchResponse, 0l); } @Test public void testDateRangeInQueryString() { - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet(); + assertAcked(client().admin().indices().prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1)); String aMonthAgo = ISODateTimeFormat.yearMonthDay().print(new DateTime(DateTimeZone.UTC).minusMonths(1)); String aMonthFromNow = ISODateTimeFormat.yearMonthDay().print(new DateTime(DateTimeZone.UTC).plusMonths(1)); + client().prepareIndex("test", "type", "1").setSource("past", aMonthAgo, "future", aMonthFromNow).get(); + refresh(); - client().prepareIndex("test", "type", "1").setSource("past", aMonthAgo, "future", aMonthFromNow).execute().actionGet(); + SearchResponse searchResponse = client().prepareSearch().setQuery(queryString("past:[now-2M/d TO now/d]")).get(); + assertHitCount(searchResponse, 1l); - client().admin().indices().prepareRefresh().execute().actionGet(); - - SearchResponse searchResponse = client().prepareSearch().setQuery(queryString("past:[now-2M/d TO now/d]")).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); - - searchResponse = client().prepareSearch().setQuery(queryString("future:[now/d TO now+2M/d]").lowercaseExpandedTerms(false)).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); + searchResponse = client().prepareSearch().setQuery(queryString("future:[now/d TO now+2M/d]").lowercaseExpandedTerms(false)).get(); + assertHitCount(searchResponse, 1l); try { - client().prepareSearch().setQuery(queryString("future:[now/D TO now+2M/d]").lowercaseExpandedTerms(false)).execute().actionGet(); + client().prepareSearch().setQuery(queryString("future:[now/D TO now+2M/d]").lowercaseExpandedTerms(false)).get(); fail("D is an unsupported unit in date math"); } catch (Exception e) { // expected @@ -410,27 +390,26 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest { } private void typeFilterTests(String index) throws Exception { - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)) + assertAcked(client().admin().indices().prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1) .addMapping("type1", jsonBuilder().startObject().startObject("type1") .startObject("_type").field("index", index).endObject() .endObject().endObject()) .addMapping("type2", jsonBuilder().startObject().startObject("type2") .startObject("_type").field("index", index).endObject() - .endObject().endObject()) - .execute().actionGet(); + .endObject().endObject())); indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("field1", "value1"), client().prepareIndex("test", "type2", "1").setSource("field1", "value1"), client().prepareIndex("test", "type1", "2").setSource("field1", "value1"), client().prepareIndex("test", "type2", "2").setSource("field1", "value1"), client().prepareIndex("test", "type2", "3").setSource("field1", "value1")); - assertThat(client().prepareCount().setQuery(filteredQuery(matchAllQuery(), typeFilter("type1"))).execute().actionGet().getCount(), equalTo(2l)); - assertThat(client().prepareCount().setQuery(filteredQuery(matchAllQuery(), typeFilter("type2"))).execute().actionGet().getCount(), equalTo(3l)); + assertHitCount(client().prepareSearch().setQuery(filteredQuery(matchAllQuery(), typeFilter("type1"))).get(), 2l); + assertHitCount(client().prepareSearch().setQuery(filteredQuery(matchAllQuery(), typeFilter("type2"))).get(), 3l); - assertThat(client().prepareCount().setTypes("type1").setQuery(matchAllQuery()).execute().actionGet().getCount(), equalTo(2l)); - assertThat(client().prepareCount().setTypes("type2").setQuery(matchAllQuery()).execute().actionGet().getCount(), equalTo(3l)); + assertHitCount(client().prepareSearch().setTypes("type1").setQuery(matchAllQuery()).get(), 2l); + assertHitCount(client().prepareSearch().setTypes("type2").setQuery(matchAllQuery()).get(), 3l); - assertThat(client().prepareCount().setTypes("type1", "type2").setQuery(matchAllQuery()).execute().actionGet().getCount(), equalTo(5l)); + assertHitCount(client().prepareSearch().setTypes("type1", "type2").setQuery(matchAllQuery()).get(), 5l); } @Test @@ -444,211 +423,174 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest { } private void idsFilterTests(String index) throws Exception { - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)) + assertAcked(client().admin().indices().prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1) .addMapping("type1", jsonBuilder().startObject().startObject("type1") .startObject("_id").field("index", index).endObject() - .endObject().endObject()) - .execute().actionGet(); + .endObject().endObject())); - client().prepareIndex("test", "type1", "1").setSource("field1", "value1").execute().actionGet(); - client().admin().indices().prepareFlush().execute().actionGet(); - client().prepareIndex("test", "type1", "2").setSource("field1", "value2").execute().actionGet(); - client().prepareIndex("test", "type1", "3").setSource("field1", "value3").execute().actionGet(); + indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("field1", "value1"), + client().prepareIndex("test", "type1", "2").setSource("field1", "value2"), + client().prepareIndex("test", "type1", "3").setSource("field1", "value3")); - client().admin().indices().prepareRefresh().execute().actionGet(); - - SearchResponse searchResponse = client().prepareSearch().setQuery(constantScoreQuery(idsFilter("type1").ids("1", "3"))).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getAt(0).id(), anyOf(equalTo("1"), equalTo("3"))); - assertThat(searchResponse.getHits().getAt(1).id(), anyOf(equalTo("1"), equalTo("3"))); + SearchResponse searchResponse = client().prepareSearch().setQuery(constantScoreQuery(idsFilter("type1").ids("1", "3"))).get(); + assertHitCount(searchResponse, 2l); + assertSearchHits(searchResponse, "1", "3"); // no type - searchResponse = client().prepareSearch().setQuery(constantScoreQuery(idsFilter().ids("1", "3"))).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getAt(0).id(), anyOf(equalTo("1"), equalTo("3"))); - assertThat(searchResponse.getHits().getAt(1).id(), anyOf(equalTo("1"), equalTo("3"))); + searchResponse = client().prepareSearch().setQuery(constantScoreQuery(idsFilter().ids("1", "3"))).get(); + assertHitCount(searchResponse, 2l); + assertSearchHits(searchResponse, "1", "3"); - searchResponse = client().prepareSearch().setQuery(idsQuery("type1").ids("1", "3")).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getAt(0).id(), anyOf(equalTo("1"), equalTo("3"))); - assertThat(searchResponse.getHits().getAt(1).id(), anyOf(equalTo("1"), equalTo("3"))); + searchResponse = client().prepareSearch().setQuery(idsQuery("type1").ids("1", "3")).get(); + assertHitCount(searchResponse, 2l); + assertSearchHits(searchResponse, "1", "3"); // no type - searchResponse = client().prepareSearch().setQuery(idsQuery().ids("1", "3")).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getAt(0).id(), anyOf(equalTo("1"), equalTo("3"))); - assertThat(searchResponse.getHits().getAt(1).id(), anyOf(equalTo("1"), equalTo("3"))); + searchResponse = client().prepareSearch().setQuery(idsQuery().ids("1", "3")).get(); + assertHitCount(searchResponse, 2l); + assertSearchHits(searchResponse, "1", "3"); - searchResponse = client().prepareSearch().setQuery(idsQuery("type1").ids("7", "10")).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(0l)); + searchResponse = client().prepareSearch().setQuery(idsQuery("type1").ids("7", "10")).get(); + assertHitCount(searchResponse, 0l); // repeat..., with terms - searchResponse = client().prepareSearch().setTypes("type1").setQuery(constantScoreQuery(termsFilter("_id", "1", "3"))).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getAt(0).id(), anyOf(equalTo("1"), equalTo("3"))); - assertThat(searchResponse.getHits().getAt(1).id(), anyOf(equalTo("1"), equalTo("3"))); + searchResponse = client().prepareSearch().setTypes("type1").setQuery(constantScoreQuery(termsFilter("_id", "1", "3"))).get(); + assertHitCount(searchResponse, 2l); + assertSearchHits(searchResponse, "1", "3"); } @Test public void testLimitFilter() throws Exception { - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet(); + assertAcked(client().admin().indices().prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1)); - client().prepareIndex("test", "type1", "1").setSource("field1", "value1_1").execute().actionGet(); - client().prepareIndex("test", "type1", "2").setSource("field1", "value1_2").execute().actionGet(); - client().prepareIndex("test", "type1", "3").setSource("field2", "value2_3").execute().actionGet(); - client().prepareIndex("test", "type1", "4").setSource("field3", "value3_4").execute().actionGet(); + indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("field1", "value1_1"), + client().prepareIndex("test", "type1", "2").setSource("field1", "value1_2"), + client().prepareIndex("test", "type1", "3").setSource("field2", "value2_3"), + client().prepareIndex("test", "type1", "4").setSource("field3", "value3_4")); - client().admin().indices().prepareRefresh().execute().actionGet(); - - SearchResponse searchResponse = client().prepareSearch().setQuery(filteredQuery(matchAllQuery(), limitFilter(2))).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); + assertHitCount(client().prepareSearch().setQuery(filteredQuery(matchAllQuery(), limitFilter(2))).get(), 2l); } @Test public void filterExistsMissingTests() throws Exception { - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet(); + assertAcked(client().admin().indices().prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1)); - client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject().startObject("obj1").field("obj1_val", "1").endObject().field("x1", "x_1").field("field1", "value1_1").field("field2", "value2_1").endObject()).execute().actionGet(); - client().prepareIndex("test", "type1", "2").setSource(jsonBuilder().startObject().startObject("obj1").field("obj1_val", "1").endObject().field("x2", "x_2").field("field1", "value1_2").endObject()).execute().actionGet(); - client().prepareIndex("test", "type1", "3").setSource(jsonBuilder().startObject().startObject("obj2").field("obj2_val", "1").endObject().field("y1", "y_1").field("field2", "value2_3").endObject()).execute().actionGet(); - client().prepareIndex("test", "type1", "4").setSource(jsonBuilder().startObject().startObject("obj2").field("obj2_val", "1").endObject().field("y2", "y_2").field("field3", "value3_4").endObject()).execute().actionGet(); + indexRandom(true, + client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject().startObject("obj1").field("obj1_val", "1").endObject().field("x1", "x_1").field("field1", "value1_1").field("field2", "value2_1").endObject()), + client().prepareIndex("test", "type1", "2").setSource(jsonBuilder().startObject().startObject("obj1").field("obj1_val", "1").endObject().field("x2", "x_2").field("field1", "value1_2").endObject()), + client().prepareIndex("test", "type1", "3").setSource(jsonBuilder().startObject().startObject("obj2").field("obj2_val", "1").endObject().field("y1", "y_1").field("field2", "value2_3").endObject()), + client().prepareIndex("test", "type1", "4").setSource(jsonBuilder().startObject().startObject("obj2").field("obj2_val", "1").endObject().field("y2", "y_2").field("field3", "value3_4").endObject()) ); - client().admin().indices().prepareRefresh().execute().actionGet(); + SearchResponse searchResponse = client().prepareSearch().setQuery(filteredQuery(matchAllQuery(), existsFilter("field1"))).get(); + assertHitCount(searchResponse, 2l); + assertSearchHits(searchResponse, "1", "2"); - SearchResponse searchResponse = client().prepareSearch().setQuery(filteredQuery(matchAllQuery(), existsFilter("field1"))).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getAt(0).id(), anyOf(equalTo("1"), equalTo("2"))); - assertThat(searchResponse.getHits().getAt(1).id(), anyOf(equalTo("1"), equalTo("2"))); + searchResponse = client().prepareSearch().setQuery(constantScoreQuery(existsFilter("field1"))).get(); + assertHitCount(searchResponse, 2l); + assertSearchHits(searchResponse, "1", "2"); - searchResponse = client().prepareSearch().setQuery(constantScoreQuery(existsFilter("field1"))).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getAt(0).id(), anyOf(equalTo("1"), equalTo("2"))); - assertThat(searchResponse.getHits().getAt(1).id(), anyOf(equalTo("1"), equalTo("2"))); + searchResponse = client().prepareSearch().setQuery(queryString("_exists_:field1")).get(); + assertHitCount(searchResponse, 2l); + assertSearchHits(searchResponse, "1", "2"); - searchResponse = client().prepareSearch().setQuery(queryString("_exists_:field1")).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getAt(0).id(), anyOf(equalTo("1"), equalTo("2"))); - assertThat(searchResponse.getHits().getAt(1).id(), anyOf(equalTo("1"), equalTo("2"))); + searchResponse = client().prepareSearch().setQuery(filteredQuery(matchAllQuery(), existsFilter("field2"))).get(); + assertHitCount(searchResponse, 2l); + assertSearchHits(searchResponse, "1", "3"); - searchResponse = client().prepareSearch().setQuery(filteredQuery(matchAllQuery(), existsFilter("field2"))).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getAt(0).id(), anyOf(equalTo("1"), equalTo("3"))); - assertThat(searchResponse.getHits().getAt(1).id(), anyOf(equalTo("1"), equalTo("3"))); - - searchResponse = client().prepareSearch().setQuery(filteredQuery(matchAllQuery(), existsFilter("field3"))).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).id(), equalTo("4")); + searchResponse = client().prepareSearch().setQuery(filteredQuery(matchAllQuery(), existsFilter("field3"))).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("4")); // wildcard check - searchResponse = client().prepareSearch().setQuery(filteredQuery(matchAllQuery(), existsFilter("x*"))).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getAt(0).id(), anyOf(equalTo("1"), equalTo("2"))); - assertThat(searchResponse.getHits().getAt(1).id(), anyOf(equalTo("1"), equalTo("2"))); + searchResponse = client().prepareSearch().setQuery(filteredQuery(matchAllQuery(), existsFilter("x*"))).get(); + assertHitCount(searchResponse, 2l); + assertSearchHits(searchResponse, "1", "2"); // object check - searchResponse = client().prepareSearch().setQuery(filteredQuery(matchAllQuery(), existsFilter("obj1"))).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getAt(0).id(), anyOf(equalTo("1"), equalTo("2"))); - assertThat(searchResponse.getHits().getAt(1).id(), anyOf(equalTo("1"), equalTo("2"))); + searchResponse = client().prepareSearch().setQuery(filteredQuery(matchAllQuery(), existsFilter("obj1"))).get(); + assertHitCount(searchResponse, 2l); + assertSearchHits(searchResponse, "1", "2"); + searchResponse = client().prepareSearch().setQuery(filteredQuery(matchAllQuery(), missingFilter("field1"))).get(); + assertHitCount(searchResponse, 2l); + assertSearchHits(searchResponse, "3", "4"); - searchResponse = client().prepareSearch().setQuery(filteredQuery(matchAllQuery(), missingFilter("field1"))).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getAt(0).id(), anyOf(equalTo("3"), equalTo("4"))); - assertThat(searchResponse.getHits().getAt(1).id(), anyOf(equalTo("3"), equalTo("4"))); + searchResponse = client().prepareSearch().setQuery(filteredQuery(matchAllQuery(), missingFilter("field1"))).get(); + assertHitCount(searchResponse, 2l); + assertSearchHits(searchResponse, "3", "4"); - searchResponse = client().prepareSearch().setQuery(filteredQuery(matchAllQuery(), missingFilter("field1"))).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getAt(0).id(), anyOf(equalTo("3"), equalTo("4"))); - assertThat(searchResponse.getHits().getAt(1).id(), anyOf(equalTo("3"), equalTo("4"))); + searchResponse = client().prepareSearch().setQuery(constantScoreQuery(missingFilter("field1"))).get(); + assertHitCount(searchResponse, 2l); + assertSearchHits(searchResponse, "3", "4"); - searchResponse = client().prepareSearch().setQuery(constantScoreQuery(missingFilter("field1"))).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getAt(0).id(), anyOf(equalTo("3"), equalTo("4"))); - assertThat(searchResponse.getHits().getAt(1).id(), anyOf(equalTo("3"), equalTo("4"))); - - searchResponse = client().prepareSearch().setQuery(queryString("_missing_:field1")).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getAt(0).id(), anyOf(equalTo("3"), equalTo("4"))); - assertThat(searchResponse.getHits().getAt(1).id(), anyOf(equalTo("3"), equalTo("4"))); + searchResponse = client().prepareSearch().setQuery(queryString("_missing_:field1")).get(); + assertHitCount(searchResponse, 2l); + assertSearchHits(searchResponse, "3", "4"); // wildcard check - searchResponse = client().prepareSearch().setQuery(filteredQuery(matchAllQuery(), missingFilter("x*"))).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getAt(0).id(), anyOf(equalTo("3"), equalTo("4"))); - assertThat(searchResponse.getHits().getAt(1).id(), anyOf(equalTo("3"), equalTo("4"))); + searchResponse = client().prepareSearch().setQuery(filteredQuery(matchAllQuery(), missingFilter("x*"))).get(); + assertHitCount(searchResponse, 2l); + assertSearchHits(searchResponse, "3", "4"); // object check - searchResponse = client().prepareSearch().setQuery(filteredQuery(matchAllQuery(), missingFilter("obj1"))).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getAt(0).id(), anyOf(equalTo("3"), equalTo("4"))); - assertThat(searchResponse.getHits().getAt(1).id(), anyOf(equalTo("3"), equalTo("4"))); + searchResponse = client().prepareSearch().setQuery(filteredQuery(matchAllQuery(), missingFilter("obj1"))).get(); + assertHitCount(searchResponse, 2l); + assertSearchHits(searchResponse, "3", "4"); } @Test public void passQueryOrFilterAsJSONStringTest() throws Exception { - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet(); + assertAcked(client().admin().indices().prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1)); - client().prepareIndex("test", "type1", "1").setSource("field1", "value1_1", "field2", "value2_1").setRefresh(true).execute().actionGet(); + client().prepareIndex("test", "type1", "1").setSource("field1", "value1_1", "field2", "value2_1").setRefresh(true).get(); WrapperQueryBuilder wrapper = new WrapperQueryBuilder("{ \"term\" : { \"field1\" : \"value1_1\" } }"); - SearchResponse searchResponse = client().prepareSearch().setQuery(wrapper).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); + assertHitCount(client().prepareSearch().setQuery(wrapper).get(), 1l); - BoolQueryBuilder bool = new BoolQueryBuilder(); - bool.must(wrapper); - bool.must(new TermQueryBuilder("field2", "value2_1")); - - searchResponse = client().prepareSearch().setQuery(wrapper).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); + BoolQueryBuilder bool = boolQuery().must(wrapper).must(new TermQueryBuilder("field2", "value2_1")); + assertHitCount(client().prepareSearch().setQuery(bool).get(), 1l); WrapperFilterBuilder wrapperFilter = new WrapperFilterBuilder("{ \"term\" : { \"field1\" : \"value1_1\" } }"); - - searchResponse = client().prepareSearch().setFilter(wrapperFilter).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); + assertHitCount(client().prepareSearch().setFilter(wrapperFilter).get(), 1l); } @Test public void testFiltersWithCustomCacheKey() throws Exception { createIndex("test"); ensureGreen(); - client().prepareIndex("test", "type1", "1").setSource("field1", "value1").execute().actionGet(); + client().prepareIndex("test", "type1", "1").setSource("field1", "value1").get(); refresh(); - SearchResponse searchResponse = client().prepareSearch("test").setQuery(constantScoreQuery(termsFilter("field1", "value1").cacheKey("test1"))).execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); + SearchResponse searchResponse = client().prepareSearch("test").setQuery(constantScoreQuery(termsFilter("field1", "value1").cacheKey("test1"))).get(); + assertHitCount(searchResponse, 1l); - searchResponse = client().prepareSearch("test").setQuery(constantScoreQuery(termsFilter("field1", "value1").cacheKey("test1"))).execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); + searchResponse = client().prepareSearch("test").setQuery(constantScoreQuery(termsFilter("field1", "value1").cacheKey("test1"))).get(); + assertHitCount(searchResponse, 1l); - searchResponse = client().prepareSearch("test").setQuery(constantScoreQuery(termsFilter("field1", "value1"))).execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); + searchResponse = client().prepareSearch("test").setQuery(constantScoreQuery(termsFilter("field1", "value1"))).get(); + assertHitCount(searchResponse, 1l); - searchResponse = client().prepareSearch("test").setQuery(constantScoreQuery(termsFilter("field1", "value1"))).execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); + searchResponse = client().prepareSearch("test").setQuery(constantScoreQuery(termsFilter("field1", "value1"))).get(); + assertHitCount(searchResponse, 1l); } @Test public void testMatchQueryNumeric() throws Exception { - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet(); + assertAcked(client().admin().indices().prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1)); - client().prepareIndex("test", "type1", "1").setSource("long", 1l, "double", 1.0d).execute().actionGet(); - client().prepareIndex("test", "type1", "2").setSource("long", 2l, "double", 2.0d).execute().actionGet(); - client().prepareIndex("test", "type1", "3").setSource("long", 3l, "double", 3.0d).execute().actionGet(); - client().admin().indices().prepareRefresh("test").execute().actionGet(); - SearchResponse searchResponse = client().prepareSearch().setQuery(matchQuery("long", "1")).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).id(), equalTo("1")); + indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("long", 1l, "double", 1.0d), + client().prepareIndex("test", "type1", "2").setSource("long", 2l, "double", 2.0d), + client().prepareIndex("test", "type1", "3").setSource("long", 3l, "double", 3.0d)); - searchResponse = client().prepareSearch().setQuery(matchQuery("double", "2")).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).id(), equalTo("2")); + SearchResponse searchResponse = client().prepareSearch().setQuery(matchQuery("long", "1")).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); + + searchResponse = client().prepareSearch().setQuery(matchQuery("double", "2")).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("2")); try { - searchResponse = client().prepareSearch().setQuery(matchQuery("double", "2 3 4")).execute().actionGet(); - assert false; + client().prepareSearch().setQuery(matchQuery("double", "2 3 4")).get(); + fail("SearchPhaseExecutionException should have been thrown"); } catch (SearchPhaseExecutionException ex) { // number format exception } @@ -656,160 +598,126 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest { @Test public void testMultiMatchQuery() throws Exception { + assertAcked(client().admin().indices().prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1)); - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet(); + indexRandom(true, + client().prepareIndex("test", "type1", "1").setSource("field1", "value1", "field2", "value4", "field3", "value3"), + client().prepareIndex("test", "type1", "2").setSource("field1", "value2", "field2", "value5", "field3", "value2"), + client().prepareIndex("test", "type1", "3").setSource("field1", "value3", "field2", "value6", "field3", "value1") ); - client().prepareIndex("test", "type1", "1").setSource("field1", "value1", "field2", "value4", "field3", "value3").execute().actionGet(); - client().prepareIndex("test", "type1", "2").setSource("field1", "value2", "field2", "value5", "field3", "value2").execute().actionGet(); - client().prepareIndex("test", "type1", "3").setSource("field1", "value3", "field2", "value6", "field3", "value1").execute().actionGet(); - client().admin().indices().prepareRefresh("test").execute().actionGet(); + MultiMatchQueryBuilder builder = multiMatchQuery("value1 value2 value4", "field1", "field2"); + SearchResponse searchResponse = client().prepareSearch().setQuery(builder) + .addFacet(FacetBuilders.termsFacet("field1").field("field1")).get(); - MultiMatchQueryBuilder builder = QueryBuilders.multiMatchQuery("value1 value2 value4", "field1", "field2"); - SearchResponse searchResponse = client().prepareSearch() - .setQuery(builder) - .addFacet(FacetBuilders.termsFacet("field1").field("field1")) - .execute().actionGet(); - - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); + assertHitCount(searchResponse, 2l); // this uses dismax so scores are equal and the order can be arbitrary assertSearchHits(searchResponse, "1", "2"); builder.useDisMax(false); searchResponse = client().prepareSearch() .setQuery(builder) - .execute().actionGet(); + .get(); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); - assertThat("1", equalTo(searchResponse.getHits().getAt(0).id())); - assertThat("2", equalTo(searchResponse.getHits().getAt(1).id())); + assertHitCount(searchResponse, 2l); + assertSearchHits(searchResponse, "1", "2"); - client().admin().indices().prepareRefresh("test").execute().actionGet(); - builder = QueryBuilders.multiMatchQuery("value1", "field1", "field2") + client().admin().indices().prepareRefresh("test").get(); + builder = multiMatchQuery("value1", "field1", "field2") .operator(MatchQueryBuilder.Operator.AND); // Operator only applies on terms inside a field! Fields are always OR-ed together. searchResponse = client().prepareSearch() .setQuery(builder) - .execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); - assertThat("1", equalTo(searchResponse.getHits().getAt(0).id())); + .get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); - client().admin().indices().prepareRefresh("test").execute().actionGet(); - builder = QueryBuilders.multiMatchQuery("value1", "field1", "field3^1.5") + refresh(); + builder = multiMatchQuery("value1", "field1", "field3^1.5") .operator(MatchQueryBuilder.Operator.AND); // Operator only applies on terms inside a field! Fields are always OR-ed together. - searchResponse = client().prepareSearch() - .setQuery(builder) - .execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); - assertThat("3", equalTo(searchResponse.getHits().getAt(0).id())); - assertThat("1", equalTo(searchResponse.getHits().getAt(1).id())); + searchResponse = client().prepareSearch().setQuery(builder).get(); + assertHitCount(searchResponse, 2l); + assertSearchHits(searchResponse, "3", "1"); - client().admin().indices().prepareRefresh("test").execute().actionGet(); - builder = QueryBuilders.multiMatchQuery("value1").field("field1").field("field3", 1.5f) + client().admin().indices().prepareRefresh("test").get(); + builder = multiMatchQuery("value1").field("field1").field("field3", 1.5f) .operator(MatchQueryBuilder.Operator.AND); // Operator only applies on terms inside a field! Fields are always OR-ed together. - searchResponse = client().prepareSearch() - .setQuery(builder) - .execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); - assertThat("3", equalTo(searchResponse.getHits().getAt(0).id())); - assertThat("1", equalTo(searchResponse.getHits().getAt(1).id())); + searchResponse = client().prepareSearch().setQuery(builder).get(); + assertHitCount(searchResponse, 2l); + assertSearchHits(searchResponse, "3", "1"); // Test lenient - client().prepareIndex("test", "type1", "3").setSource("field1", "value7", "field2", "value8", "field4", 5).execute().actionGet(); - client().admin().indices().prepareRefresh("test").execute().actionGet(); + client().prepareIndex("test", "type1", "3").setSource("field1", "value7", "field2", "value8", "field4", 5).get(); + refresh(); - builder = QueryBuilders.multiMatchQuery("value1", "field1", "field2", "field4"); + builder = multiMatchQuery("value1", "field1", "field2", "field4"); try { - client().prepareSearch() - .setQuery(builder) - .execute().actionGet(); + client().prepareSearch().setQuery(builder).get(); fail("Exception expected"); } catch (SearchPhaseExecutionException e) { assertThat(e.shardFailures()[0].status(), equalTo(RestStatus.BAD_REQUEST)); } builder.lenient(true); - searchResponse = client().prepareSearch().setQuery(builder).execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); - assertThat("1", equalTo(searchResponse.getHits().getAt(0).id())); + searchResponse = client().prepareSearch().setQuery(builder).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); } @Test - public void testMatchQueryZeroTermsQuery() throws IOException { - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)) - .addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties") - .startObject("field1").field("type", "string").field("analyzer", "classic").endObject() - .startObject("field2").field("type", "string").field("analyzer", "classic").endObject() - .endObject().endObject().endObject()) - .execute().actionGet(); - client().prepareIndex("test", "type1", "1").setSource("field1", "value1").execute().actionGet(); - client().prepareIndex("test", "type1", "2").setSource("field1", "value2").execute().actionGet(); - client().admin().indices().prepareRefresh("test").execute().actionGet(); + public void testMatchQueryZeroTermsQuery() { + assertAcked(client().admin().indices().prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1) + .addMapping("type1", "field1", "type=string,analyzer=classic", "field2", "type=string,analyzer=classic")); + client().prepareIndex("test", "type1", "1").setSource("field1", "value1").get(); + client().prepareIndex("test", "type1", "2").setSource("field1", "value2").get(); + refresh(); BoolQueryBuilder boolQuery = boolQuery() .must(matchQuery("field1", "a").zeroTermsQuery(MatchQueryBuilder.ZeroTermsQuery.NONE)) .must(matchQuery("field1", "value1").zeroTermsQuery(MatchQueryBuilder.ZeroTermsQuery.NONE)); - SearchResponse searchResponse = client().prepareSearch() - .setQuery(boolQuery) - .execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(0l)); + SearchResponse searchResponse = client().prepareSearch().setQuery(boolQuery).get(); + assertHitCount(searchResponse, 0l); boolQuery = boolQuery() .must(matchQuery("field1", "a").zeroTermsQuery(MatchQueryBuilder.ZeroTermsQuery.ALL)) .must(matchQuery("field1", "value1").zeroTermsQuery(MatchQueryBuilder.ZeroTermsQuery.ALL)); - searchResponse = client().prepareSearch() - .setQuery(boolQuery) - .execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); + searchResponse = client().prepareSearch().setQuery(boolQuery).get(); + assertHitCount(searchResponse, 1l); - boolQuery = boolQuery() - .must(matchQuery("field1", "a").zeroTermsQuery(MatchQueryBuilder.ZeroTermsQuery.ALL)); - searchResponse = client().prepareSearch() - .setQuery(boolQuery) - .execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); + boolQuery = boolQuery().must(matchQuery("field1", "a").zeroTermsQuery(MatchQueryBuilder.ZeroTermsQuery.ALL)); + searchResponse = client().prepareSearch().setQuery(boolQuery).get(); + assertHitCount(searchResponse, 2l); } - @Test - public void testMultiMatchQueryZeroTermsQuery() throws IOException { - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)) - .addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties") - .startObject("field1").field("type", "string").field("analyzer", "classic").endObject() - .startObject("field2").field("type", "string").field("analyzer", "classic").endObject() - .endObject().endObject().endObject()) - .execute().actionGet(); client().prepareIndex("test", "type1", "1").setSource("field1", "value1", "field2", "value2").execute().actionGet(); - client().prepareIndex("test", "type1", "2").setSource("field1", "value3", "field2", "value4").execute().actionGet(); - client().admin().indices().prepareRefresh("test").execute().actionGet(); + public void testMultiMatchQueryZeroTermsQuery() { + assertAcked(client().admin().indices().prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1) + .addMapping("type1", "field1", "type=string,analyzer=classic", "field2", "type=string,analyzer=classic")); + client().prepareIndex("test", "type1", "1").setSource("field1", "value1", "field2", "value2").get(); + client().prepareIndex("test", "type1", "2").setSource("field1", "value3", "field2", "value4").get(); + refresh(); + BoolQueryBuilder boolQuery = boolQuery() .must(multiMatchQuery("a", "field1", "field2").zeroTermsQuery(MatchQueryBuilder.ZeroTermsQuery.NONE)) .must(multiMatchQuery("value1", "field1", "field2").zeroTermsQuery(MatchQueryBuilder.ZeroTermsQuery.NONE)); // Fields are ORed together - SearchResponse searchResponse = client().prepareSearch() - .setQuery(boolQuery) - .execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(0l)); + SearchResponse searchResponse = client().prepareSearch().setQuery(boolQuery).get(); + assertHitCount(searchResponse, 0l); boolQuery = boolQuery() .must(multiMatchQuery("a", "field1", "field2").zeroTermsQuery(MatchQueryBuilder.ZeroTermsQuery.ALL)) .must(multiMatchQuery("value4", "field1", "field2").zeroTermsQuery(MatchQueryBuilder.ZeroTermsQuery.ALL)); - searchResponse = client().prepareSearch() - .setQuery(boolQuery) - .execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); + searchResponse = client().prepareSearch().setQuery(boolQuery).get(); + assertHitCount(searchResponse, 1l); - boolQuery = boolQuery() - .must(multiMatchQuery("a", "field1").zeroTermsQuery(MatchQueryBuilder.ZeroTermsQuery.ALL)); - searchResponse = client().prepareSearch() - .setQuery(boolQuery) - .execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); + boolQuery = boolQuery().must(multiMatchQuery("a", "field1").zeroTermsQuery(MatchQueryBuilder.ZeroTermsQuery.ALL)); + searchResponse = client().prepareSearch().setQuery(boolQuery).get(); + assertHitCount(searchResponse, 2l); } @Test public void testMultiMatchQueryMinShouldMatch() { - - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet(); - client().prepareIndex("test", "type1", "1").setSource("field1", new String[]{"value1", "value2", "value3"}).execute().actionGet(); - client().prepareIndex("test", "type1", "2").setSource("field2", "value1").execute().actionGet(); - client().admin().indices().prepareRefresh("test").execute().actionGet(); + assertAcked(client().admin().indices().prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1)); + client().prepareIndex("test", "type1", "1").setSource("field1", new String[]{"value1", "value2", "value3"}).get(); + client().prepareIndex("test", "type1", "2").setSource("field2", "value1").get(); + refresh(); MultiMatchQueryBuilder multiMatchQuery = multiMatchQuery("value1 value2 foo", "field1", "field2"); @@ -817,399 +725,296 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest { multiMatchQuery.minimumShouldMatch("70%"); SearchResponse searchResponse = client().prepareSearch() .setQuery(multiMatchQuery) - .execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getHits()[0].id(), equalTo("1")); + .get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); multiMatchQuery.minimumShouldMatch("30%"); - searchResponse = client().prepareSearch() - .setQuery(multiMatchQuery) - .execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getHits()[0].id(), equalTo("1")); - assertThat(searchResponse.getHits().getHits()[1].id(), equalTo("2")); + searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get(); + assertHitCount(searchResponse, 2l); + assertFirstHit(searchResponse, hasId("1")); + assertSecondHit(searchResponse, hasId("2")); multiMatchQuery.useDisMax(false); multiMatchQuery.minimumShouldMatch("70%"); - searchResponse = client().prepareSearch() - .setQuery(multiMatchQuery) - .execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getHits()[0].id(), equalTo("1")); + searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); multiMatchQuery.minimumShouldMatch("30%"); - searchResponse = client().prepareSearch() - .setQuery(multiMatchQuery) - .execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getHits()[0].id(), equalTo("1")); - assertThat(searchResponse.getHits().getHits()[1].id(), equalTo("2")); + searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get(); + assertHitCount(searchResponse, 2l); + assertFirstHit(searchResponse, hasId("1")); + assertSecondHit(searchResponse, hasId("2")); multiMatchQuery = multiMatchQuery("value1 value2 bar", "field1"); multiMatchQuery.minimumShouldMatch("100%"); - searchResponse = client().prepareSearch() - .setQuery(multiMatchQuery) - .execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(0l)); + searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get(); + assertHitCount(searchResponse, 0l); multiMatchQuery.minimumShouldMatch("70%"); - searchResponse = client().prepareSearch() - .setQuery(multiMatchQuery) - .execute().actionGet(); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getHits()[0].id(), equalTo("1")); + searchResponse = client().prepareSearch().setQuery(multiMatchQuery).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); } @Test public void testFuzzyQueryString() { - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet(); - client().prepareIndex("test", "type1", "1").setSource("str", "kimchy", "date", "2012-02-01", "num", 12).execute().actionGet(); - client().prepareIndex("test", "type1", "2").setSource("str", "shay", "date", "2012-02-05", "num", 20).execute().actionGet(); - client().admin().indices().prepareRefresh().execute().actionGet(); + assertAcked(client().admin().indices().prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1)); + client().prepareIndex("test", "type1", "1").setSource("str", "kimchy", "date", "2012-02-01", "num", 12).get(); + client().prepareIndex("test", "type1", "2").setSource("str", "shay", "date", "2012-02-05", "num", 20).get(); + refresh(); - SearchResponse searchResponse = client().prepareSearch() - .setQuery(queryString("str:kimcy~1")) - .execute().actionGet(); + SearchResponse searchResponse = client().prepareSearch().setQuery(queryString("str:kimcy~1")).get(); assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).id(), equalTo("1")); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); - searchResponse = client().prepareSearch() - .setQuery(queryString("num:11~1")) - .execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).id(), equalTo("1")); + searchResponse = client().prepareSearch().setQuery(queryString("num:11~1")).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); - searchResponse = client().prepareSearch() - .setQuery(queryString("date:2012-02-02~1d")) - .execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); + searchResponse = client().prepareSearch().setQuery(queryString("date:2012-02-02~1d")).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); } @Test public void testQuotedQueryStringWithBoost() throws InterruptedException, ExecutionException { float boost = 10.0f; - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet(); + assertAcked(client().admin().indices().prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1)); indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("important", "phrase match", "less_important", "nothing important"), client().prepareIndex("test", "type1", "2").setSource("important", "nothing important", "less_important", "phrase match") ); SearchResponse searchResponse = client().prepareSearch() - .setQuery(queryString("\"phrase match\"").field("important", boost).field("less_important")) - .execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getAt(0).id(), equalTo("1")); - assertThat(searchResponse.getHits().getAt(1).id(), equalTo("2")); - assertThat((double) searchResponse.getHits().getAt(0).score(), closeTo(boost * searchResponse.getHits().getAt(1).score(), .1)); + .setQuery(queryString("\"phrase match\"").field("important", boost).field("less_important")).get(); + assertHitCount(searchResponse, 2l); + assertFirstHit(searchResponse, hasId("1")); + assertSecondHit(searchResponse, hasId("2")); + assertThat((double)searchResponse.getHits().getAt(0).score(), closeTo(boost * searchResponse.getHits().getAt(1).score(), .1)); searchResponse = client().prepareSearch() - .setQuery(queryString("\"phrase match\"").field("important", boost).field("less_important").useDisMax(false)) - .execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getAt(0).id(), equalTo("1")); - assertThat(searchResponse.getHits().getAt(1).id(), equalTo("2")); - assertThat((double) searchResponse.getHits().getAt(0).score(), closeTo(boost * searchResponse.getHits().getAt(1).score(), .1)); + .setQuery(queryString("\"phrase match\"").field("important", boost).field("less_important").useDisMax(false)).get(); + assertHitCount(searchResponse, 2l); + assertFirstHit(searchResponse, hasId("1")); + assertSecondHit(searchResponse, hasId("2")); + assertThat((double)searchResponse.getHits().getAt(0).score(), closeTo(boost * searchResponse.getHits().getAt(1).score(), .1)); } @Test public void testSpecialRangeSyntaxInQueryString() { - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet(); - client().prepareIndex("test", "type1", "1").setSource("str", "kimchy", "date", "2012-02-01", "num", 12).execute().actionGet(); - client().prepareIndex("test", "type1", "2").setSource("str", "shay", "date", "2012-02-05", "num", 20).execute().actionGet(); - client().admin().indices().prepareRefresh().execute().actionGet(); + assertAcked(client().admin().indices().prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1)); + client().prepareIndex("test", "type1", "1").setSource("str", "kimchy", "date", "2012-02-01", "num", 12).get(); + client().prepareIndex("test", "type1", "2").setSource("str", "shay", "date", "2012-02-05", "num", 20).get(); + refresh(); - SearchResponse searchResponse = client().prepareSearch() - .setQuery(queryString("num:>19")) - .execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).id(), equalTo("2")); + SearchResponse searchResponse = client().prepareSearch().setQuery(queryString("num:>19")).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("2")); - searchResponse = client().prepareSearch() - .setQuery(queryString("num:>20")) - .execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().totalHits(), equalTo(0l)); + searchResponse = client().prepareSearch().setQuery(queryString("num:>20")).get(); + assertHitCount(searchResponse, 0l); - searchResponse = client().prepareSearch() - .setQuery(queryString("num:>=20")) - .execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).id(), equalTo("2")); + searchResponse = client().prepareSearch().setQuery(queryString("num:>=20")).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("2")); - searchResponse = client().prepareSearch() - .setQuery(queryString("num:>11")) - .execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); + searchResponse = client().prepareSearch().setQuery(queryString("num:>11")).get(); + assertHitCount(searchResponse, 2l); - searchResponse = client().prepareSearch() - .setQuery(queryString("num:<20")) - .execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); + searchResponse = client().prepareSearch().setQuery(queryString("num:<20")).get(); + assertHitCount(searchResponse, 1l); - searchResponse = client().prepareSearch() - .setQuery(queryString("num:<=20")) - .execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); + searchResponse = client().prepareSearch().setQuery(queryString("num:<=20")).get(); + assertHitCount(searchResponse, 2l); - searchResponse = client().prepareSearch() - .setQuery(queryString("+num:>11 +num:<20")) - .execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); + searchResponse = client().prepareSearch().setQuery(queryString("+num:>11 +num:<20")).get(); + assertHitCount(searchResponse, 1l); } @Test public void testEmptyTermsFilter() throws Exception { - assertAcked(prepareCreate("test").addMapping("type", - jsonBuilder().startObject().startObject("type").startObject("properties") - .startObject("term").field("type", "string").endObject() - .endObject().endObject().endObject())); + assertAcked(prepareCreate("test").addMapping("type", "term", "type=string")); ensureGreen(); - client().prepareIndex("test", "type", "1").setSource("term", "1").execute().actionGet(); - client().prepareIndex("test", "type", "2").setSource("term", "2").execute().actionGet(); - client().prepareIndex("test", "type", "3").setSource("term", "3").execute().actionGet(); - client().prepareIndex("test", "type", "4").setSource("term", "4").execute().actionGet(); - refresh(); - SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(filteredQuery(matchAllQuery(), termsFilter("term", new String[0])) - ).execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(0l)); + indexRandom(true, client().prepareIndex("test", "type", "1").setSource("term", "1"), + client().prepareIndex("test", "type", "2").setSource("term", "2"), + client().prepareIndex("test", "type", "3").setSource("term", "3"), + client().prepareIndex("test", "type", "4").setSource("term", "4") ); - searchResponse = client().prepareSearch("test") - .setQuery(filteredQuery(matchAllQuery(), idsFilter()) - ).execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(0l)); + SearchResponse searchResponse = client().prepareSearch("test") + .setQuery(filteredQuery(matchAllQuery(), termsFilter("term", new String[0]))).get(); + assertHitCount(searchResponse, 0l); + + searchResponse = client().prepareSearch("test").setQuery(filteredQuery(matchAllQuery(), idsFilter())).get(); + assertHitCount(searchResponse, 0l); } @Test public void testFieldDataTermsFilter() throws Exception { - assertAcked(prepareCreate("test").addMapping("type", - jsonBuilder().startObject().startObject("type").startObject("properties") - .startObject("str").field("type", "string").endObject() - .startObject("lng").field("type", "long").endObject() - .startObject("dbl").field("type", "double").endObject() - .endObject().endObject().endObject())); + assertAcked(prepareCreate("test").addMapping("type", "str", "type=string", "lng", "type=long", "dbl", "type=double")); ensureGreen(); - client().prepareIndex("test", "type", "1").setSource("str", "1", "lng", 1l, "dbl", 1.0d).execute().actionGet(); - client().prepareIndex("test", "type", "2").setSource("str", "2", "lng", 2l, "dbl", 2.0d).execute().actionGet(); - client().prepareIndex("test", "type", "3").setSource("str", "3", "lng", 3l, "dbl", 3.0d).execute().actionGet(); - client().prepareIndex("test", "type", "4").setSource("str", "4", "lng", 4l, "dbl", 4.0d).execute().actionGet(); + client().prepareIndex("test", "type", "1").setSource("str", "1", "lng", 1l, "dbl", 1.0d).get(); + client().prepareIndex("test", "type", "2").setSource("str", "2", "lng", 2l, "dbl", 2.0d).get(); + client().prepareIndex("test", "type", "3").setSource("str", "3", "lng", 3l, "dbl", 3.0d).get(); + client().prepareIndex("test", "type", "4").setSource("str", "4", "lng", 4l, "dbl", 4.0d).get(); refresh(); SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(filteredQuery(matchAllQuery(), termsFilter("str", "1", "4").execution("fielddata"))) - .execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getAt(0).getId(), anyOf(equalTo("1"), equalTo("4"))); - assertThat(searchResponse.getHits().getAt(1).getId(), anyOf(equalTo("1"), equalTo("4"))); + .setQuery(filteredQuery(matchAllQuery(), termsFilter("str", "1", "4").execution("fielddata"))).get(); + assertHitCount(searchResponse, 2l); + assertSearchHits(searchResponse, "1", "4"); searchResponse = client().prepareSearch("test") - .setQuery(filteredQuery(matchAllQuery(), termsFilter("lng", new long[] {2, 3}).execution("fielddata"))) - .execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getAt(0).getId(), anyOf(equalTo("2"), equalTo("3"))); - assertThat(searchResponse.getHits().getAt(1).getId(), anyOf(equalTo("2"), equalTo("3"))); + .setQuery(filteredQuery(matchAllQuery(), termsFilter("lng", new long[] {2, 3}).execution("fielddata"))).get(); + assertHitCount(searchResponse, 2l); + assertSearchHits(searchResponse, "2", "3"); searchResponse = client().prepareSearch("test") - .setQuery(filteredQuery(matchAllQuery(), termsFilter("dbl", new double[] {2, 3}).execution("fielddata"))) - .execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getAt(0).getId(), anyOf(equalTo("2"), equalTo("3"))); - assertThat(searchResponse.getHits().getAt(1).getId(), anyOf(equalTo("2"), equalTo("3"))); + .setQuery(filteredQuery(matchAllQuery(), termsFilter("dbl", new double[]{2, 3}).execution("fielddata"))).get(); + assertHitCount(searchResponse, 2l); + assertSearchHits(searchResponse, "2", "3"); searchResponse = client().prepareSearch("test") - .setQuery(filteredQuery(matchAllQuery(), termsFilter("lng", new int[] {1, 3}).execution("fielddata"))) - .execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getAt(0).getId(), anyOf(equalTo("1"), equalTo("3"))); - assertThat(searchResponse.getHits().getAt(1).getId(), anyOf(equalTo("1"), equalTo("3"))); + .setQuery(filteredQuery(matchAllQuery(), termsFilter("lng", new int[] {1, 3}).execution("fielddata"))).get(); + assertHitCount(searchResponse, 2l); + assertSearchHits(searchResponse, "1", "3"); searchResponse = client().prepareSearch("test") - .setQuery(filteredQuery(matchAllQuery(), termsFilter("dbl", new float[] {2, 4}).execution("fielddata"))) - .execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getAt(0).getId(), anyOf(equalTo("2"), equalTo("4"))); - assertThat(searchResponse.getHits().getAt(1).getId(), anyOf(equalTo("2"), equalTo("4"))); + .setQuery(filteredQuery(matchAllQuery(), termsFilter("dbl", new float[] {2, 4}).execution("fielddata"))).get(); + assertHitCount(searchResponse, 2l); + assertSearchHits(searchResponse, "2", "4"); // test partial matching searchResponse = client().prepareSearch("test") - .setQuery(filteredQuery(matchAllQuery(), termsFilter("str", "2", "5").execution("fielddata"))) - .execute().actionGet(); + .setQuery(filteredQuery(matchAllQuery(), termsFilter("str", "2", "5").execution("fielddata"))).get(); assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("2")); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("2")); searchResponse = client().prepareSearch("test") - .setQuery(filteredQuery(matchAllQuery(), termsFilter("dbl", new double[] {2, 5}).execution("fielddata"))) - .execute().actionGet(); + .setQuery(filteredQuery(matchAllQuery(), termsFilter("dbl", new double[] {2, 5}).execution("fielddata"))).get(); assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("2")); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("2")); searchResponse = client().prepareSearch("test") - .setQuery(filteredQuery(matchAllQuery(), termsFilter("lng", new long[] {2, 5}).execution("fielddata"))) - .execute().actionGet(); + .setQuery(filteredQuery(matchAllQuery(), termsFilter("lng", new long[] {2, 5}).execution("fielddata"))).get(); assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("2")); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("2")); // test valid type, but no matching terms searchResponse = client().prepareSearch("test") - .setQuery(filteredQuery(matchAllQuery(), termsFilter("str", "5", "6").execution("fielddata"))) - .execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(0l)); + .setQuery(filteredQuery(matchAllQuery(), termsFilter("str", "5", "6").execution("fielddata"))).get(); + assertHitCount(searchResponse, 0l); searchResponse = client().prepareSearch("test") - .setQuery(filteredQuery(matchAllQuery(), termsFilter("dbl", new double[] {5, 6}).execution("fielddata"))) - .execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(0l)); + .setQuery(filteredQuery(matchAllQuery(), termsFilter("dbl", new double[] {5, 6}).execution("fielddata"))).get(); + assertHitCount(searchResponse, 0l); searchResponse = client().prepareSearch("test") - .setQuery(filteredQuery(matchAllQuery(), termsFilter("lng", new long[] {5, 6}).execution("fielddata"))) - .execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(0l)); + .setQuery(filteredQuery(matchAllQuery(), termsFilter("lng", new long[] {5, 6}).execution("fielddata"))).get(); + assertHitCount(searchResponse, 0l); } @Test public void testTermsLookupFilter() throws Exception { - assertAcked(prepareCreate("lookup").addMapping("type", - jsonBuilder().startObject().startObject("type").startObject("properties") - .startObject("terms").field("type", "string").endObject() - .startObject("other").field("type", "string").endObject() - .endObject().endObject().endObject())); + assertAcked(prepareCreate("lookup").addMapping("type", "terms","type=string", "other", "type=string")); assertAcked(prepareCreate("lookup2").addMapping("type", jsonBuilder().startObject().startObject("type").startObject("properties") .startObject("arr").startObject("properties").startObject("term").field("type", "string") .endObject().endObject().endObject().endObject().endObject().endObject())); - assertAcked(prepareCreate("test").addMapping("type", - jsonBuilder().startObject().startObject("type").startObject("properties") - .startObject("term").field("type", "string").endObject() - .endObject().endObject().endObject())); + assertAcked(prepareCreate("test").addMapping("type", "term", "type=string")); + ensureGreen(); - client().prepareIndex("lookup", "type", "1").setSource("terms", new String[]{"1", "3"}).execute().actionGet(); - client().prepareIndex("lookup", "type", "2").setSource("terms", new String[]{"2"}).execute().actionGet(); - client().prepareIndex("lookup", "type", "3").setSource("terms", new String[]{"2", "4"}).execute().actionGet(); - client().prepareIndex("lookup", "type", "4").setSource("other", "value").execute().actionGet(); - client().prepareIndex("lookup2", "type", "1").setSource(XContentFactory.jsonBuilder().startObject() - .startArray("arr") - .startObject().field("term", "1").endObject() - .startObject().field("term", "3").endObject() - .endArray() - .endObject()).execute().actionGet(); - client().prepareIndex("lookup2", "type", "2").setSource(XContentFactory.jsonBuilder().startObject() - .startArray("arr") - .startObject().field("term", "2").endObject() - .endArray() - .endObject()).execute().actionGet(); - client().prepareIndex("lookup2", "type", "3").setSource(XContentFactory.jsonBuilder().startObject() - .startArray("arr") - .startObject().field("term", "2").endObject() - .startObject().field("term", "4").endObject() - .endArray() - .endObject()).execute().actionGet(); - - client().prepareIndex("test", "type", "1").setSource("term", "1").execute().actionGet(); - client().prepareIndex("test", "type", "2").setSource("term", "2").execute().actionGet(); - client().prepareIndex("test", "type", "3").setSource("term", "3").execute().actionGet(); - client().prepareIndex("test", "type", "4").setSource("term", "4").execute().actionGet(); - refresh(); + indexRandom(true, + client().prepareIndex("lookup", "type", "1").setSource("terms", new String[]{"1", "3"}), + client().prepareIndex("lookup", "type", "2").setSource("terms", new String[]{"2"}), + client().prepareIndex("lookup", "type", "3").setSource("terms", new String[]{"2", "4"}), + client().prepareIndex("lookup", "type", "4").setSource("other", "value"), + client().prepareIndex("lookup2", "type", "1").setSource(XContentFactory.jsonBuilder().startObject() + .startArray("arr") + .startObject().field("term", "1").endObject() + .startObject().field("term", "3").endObject() + .endArray() + .endObject()), + client().prepareIndex("lookup2", "type", "2").setSource(XContentFactory.jsonBuilder().startObject() + .startArray("arr") + .startObject().field("term", "2").endObject() + .endArray() + .endObject()), + client().prepareIndex("lookup2", "type", "3").setSource(XContentFactory.jsonBuilder().startObject() + .startArray("arr") + .startObject().field("term", "2").endObject() + .startObject().field("term", "4").endObject() + .endArray() + .endObject()), + client().prepareIndex("test", "type", "1").setSource("term", "1"), + client().prepareIndex("test", "type", "2").setSource("term", "2"), + client().prepareIndex("test", "type", "3").setSource("term", "3"), + client().prepareIndex("test", "type", "4").setSource("term", "4") ); SearchResponse searchResponse = client().prepareSearch("test") .setQuery(filteredQuery(matchAllQuery(), termsLookupFilter("term").lookupIndex("lookup").lookupType("type").lookupId("1").lookupPath("terms")) - ).execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getHits()[0].getId(), anyOf(equalTo("1"), equalTo("3"))); - assertThat(searchResponse.getHits().getHits()[1].getId(), anyOf(equalTo("1"), equalTo("3"))); + ).get(); + assertHitCount(searchResponse, 2l); + assertSearchHits(searchResponse, "1", "3"); // same as above, just on the _id... searchResponse = client().prepareSearch("test") .setQuery(filteredQuery(matchAllQuery(), termsLookupFilter("_id").lookupIndex("lookup").lookupType("type").lookupId("1").lookupPath("terms")) - ).execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getHits()[0].getId(), anyOf(equalTo("1"), equalTo("3"))); - assertThat(searchResponse.getHits().getHits()[1].getId(), anyOf(equalTo("1"), equalTo("3"))); + ).get(); + assertHitCount(searchResponse, 2l); + assertSearchHits(searchResponse, "1", "3"); // another search with same parameters... searchResponse = client().prepareSearch("test") .setQuery(filteredQuery(matchAllQuery(), termsLookupFilter("term").lookupIndex("lookup").lookupType("type").lookupId("1").lookupPath("terms")) - ).execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getHits()[0].getId(), anyOf(equalTo("1"), equalTo("3"))); - assertThat(searchResponse.getHits().getHits()[1].getId(), anyOf(equalTo("1"), equalTo("3"))); + ).get(); + assertHitCount(searchResponse, 2l); + assertSearchHits(searchResponse, "1", "3"); searchResponse = client().prepareSearch("test") .setQuery(filteredQuery(matchAllQuery(), termsLookupFilter("term").lookupIndex("lookup").lookupType("type").lookupId("2").lookupPath("terms")) - ).execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getHits()[0].getId(), anyOf(equalTo("1"), equalTo("2"))); + ).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("2")); searchResponse = client().prepareSearch("test") .setQuery(filteredQuery(matchAllQuery(), termsLookupFilter("term").lookupIndex("lookup").lookupType("type").lookupId("3").lookupPath("terms")) - ).execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getHits()[0].getId(), anyOf(equalTo("2"), equalTo("4"))); - assertThat(searchResponse.getHits().getHits()[1].getId(), anyOf(equalTo("2"), equalTo("4"))); + ).get(); + assertHitCount(searchResponse, 2l); + assertSearchHits(searchResponse, "2", "4"); searchResponse = client().prepareSearch("test") .setQuery(filteredQuery(matchAllQuery(), termsLookupFilter("term").lookupIndex("lookup").lookupType("type").lookupId("4").lookupPath("terms")) - ).execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(0l)); - + ).get(); + assertHitCount(searchResponse, 0l); searchResponse = client().prepareSearch("test") .setQuery(filteredQuery(matchAllQuery(), termsLookupFilter("term").lookupIndex("lookup2").lookupType("type").lookupId("1").lookupPath("arr.term")) - ).execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getHits()[0].getId(), anyOf(equalTo("1"), equalTo("3"))); - assertThat(searchResponse.getHits().getHits()[1].getId(), anyOf(equalTo("1"), equalTo("3"))); + ).get(); + assertHitCount(searchResponse, 2l); + assertSearchHits(searchResponse, "1", "3"); searchResponse = client().prepareSearch("test") .setQuery(filteredQuery(matchAllQuery(), termsLookupFilter("term").lookupIndex("lookup2").lookupType("type").lookupId("2").lookupPath("arr.term")) - ).execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getHits()[0].getId(), anyOf(equalTo("1"), equalTo("2"))); + ).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("2")); searchResponse = client().prepareSearch("test") .setQuery(filteredQuery(matchAllQuery(), termsLookupFilter("term").lookupIndex("lookup2").lookupType("type").lookupId("3").lookupPath("arr.term")) - ).execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(2l)); - assertThat(searchResponse.getHits().getHits()[0].getId(), anyOf(equalTo("2"), equalTo("4"))); - assertThat(searchResponse.getHits().getHits()[1].getId(), anyOf(equalTo("2"), equalTo("4"))); + ).get(); + assertHitCount(searchResponse, 2l); + assertSearchHits(searchResponse, "2", "4"); searchResponse = client().prepareSearch("test") .setQuery(filteredQuery(matchAllQuery(), termsLookupFilter("not_exists").lookupIndex("lookup2").lookupType("type").lookupId("3").lookupPath("arr.term")) - ).execute().actionGet(); - assertNoFailures(searchResponse); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(0l)); + ).get(); + assertHitCount(searchResponse, 0l); } @Test @@ -1217,50 +1022,44 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest { createIndex("test"); ensureGreen(); - client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject() - .field("field1", "value1") - .endObject()).execute().actionGet(); - client().prepareIndex("test", "type2", "2").setSource(jsonBuilder().startObject() - .field("field1", "value2") - .endObject()).execute().actionGet(); + client().prepareIndex("test", "type1", "1").setSource("field1", "value1").get(); + client().prepareIndex("test", "type2", "2").setSource("field1", "value2").get(); + refresh(); - client().admin().indices().prepareRefresh().execute().actionGet(); - - SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setFilter(FilterBuilders.idsFilter("type1").ids("1")).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); + SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setFilter(idsFilter("type1").ids("1")).get(); + assertHitCount(searchResponse, 1l); assertThat(searchResponse.getHits().hits().length, equalTo(1)); - searchResponse = client().prepareSearch().setQuery(QueryBuilders.constantScoreQuery(FilterBuilders.idsFilter("type1", "type2").ids("1", "2"))).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(2l)); + searchResponse = client().prepareSearch().setQuery(constantScoreQuery(idsFilter("type1", "type2").ids("1", "2"))).get(); + assertHitCount(searchResponse, 2l); assertThat(searchResponse.getHits().hits().length, equalTo(2)); - searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setFilter(FilterBuilders.idsFilter().ids("1")).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); + searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setFilter(idsFilter().ids("1")).get(); + assertHitCount(searchResponse, 1l); assertThat(searchResponse.getHits().hits().length, equalTo(1)); - searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setFilter(FilterBuilders.idsFilter().ids("1", "2")).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(2l)); + searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setFilter(idsFilter().ids("1", "2")).get(); + assertHitCount(searchResponse, 2l); assertThat(searchResponse.getHits().hits().length, equalTo(2)); - searchResponse = client().prepareSearch().setQuery(QueryBuilders.constantScoreQuery(FilterBuilders.idsFilter().ids("1", "2"))).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(2l)); + searchResponse = client().prepareSearch().setQuery(constantScoreQuery(idsFilter().ids("1", "2"))).get(); + assertHitCount(searchResponse, 2l); assertThat(searchResponse.getHits().hits().length, equalTo(2)); - - searchResponse = client().prepareSearch().setQuery(QueryBuilders.constantScoreQuery(FilterBuilders.idsFilter("type1").ids("1", "2"))).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); + searchResponse = client().prepareSearch().setQuery(constantScoreQuery(idsFilter("type1").ids("1", "2"))).get(); + assertHitCount(searchResponse, 1l); assertThat(searchResponse.getHits().hits().length, equalTo(1)); - searchResponse = client().prepareSearch().setQuery(QueryBuilders.constantScoreQuery(FilterBuilders.idsFilter().ids("1"))).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); + searchResponse = client().prepareSearch().setQuery(constantScoreQuery(idsFilter().ids("1"))).get(); + assertHitCount(searchResponse, 1l); assertThat(searchResponse.getHits().hits().length, equalTo(1)); - searchResponse = client().prepareSearch().setQuery(QueryBuilders.constantScoreQuery(FilterBuilders.idsFilter(null).ids("1"))).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); + searchResponse = client().prepareSearch().setQuery(constantScoreQuery(idsFilter(null).ids("1"))).get(); + assertHitCount(searchResponse, 1l); assertThat(searchResponse.getHits().hits().length, equalTo(1)); - searchResponse = client().prepareSearch().setQuery(QueryBuilders.constantScoreQuery(FilterBuilders.idsFilter("type1", "type2", "type3").ids("1", "2", "3", "4"))).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(2l)); + searchResponse = client().prepareSearch().setQuery(constantScoreQuery(idsFilter("type1", "type2", "type3").ids("1", "2", "3", "4"))).get(); + assertHitCount(searchResponse, 2l); assertThat(searchResponse.getHits().hits().length, equalTo(2)); } @@ -1269,426 +1068,276 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest { createIndex("test"); ensureGreen(); - client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject() - .field("field1", "value1") - .endObject()).execute().actionGet(); - client().prepareIndex("test", "type2", "2").setSource(jsonBuilder().startObject() - .field("field1", "value2") - .endObject()).execute().actionGet(); + client().prepareIndex("test", "type1", "1").setSource("field1", "value1").get(); + client().prepareIndex("test", "type2", "2").setSource("field1", "value2").get(); + refresh(); - client().admin().indices().prepareRefresh().execute().actionGet(); - - SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.idsQuery("type1", "type2").ids("1", "2")).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(2l)); + SearchResponse searchResponse = client().prepareSearch().setQuery(idsQuery("type1", "type2").ids("1", "2")).get(); + assertHitCount(searchResponse, 2l); assertThat(searchResponse.getHits().hits().length, equalTo(2)); - searchResponse = client().prepareSearch().setQuery(QueryBuilders.idsQuery().ids("1")).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); + searchResponse = client().prepareSearch().setQuery(idsQuery().ids("1")).get(); + assertHitCount(searchResponse, 1l); assertThat(searchResponse.getHits().hits().length, equalTo(1)); - searchResponse = client().prepareSearch().setQuery(QueryBuilders.idsQuery().ids("1", "2")).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(2l)); + searchResponse = client().prepareSearch().setQuery(idsQuery().ids("1", "2")).get(); + assertHitCount(searchResponse, 2l); assertThat(searchResponse.getHits().hits().length, equalTo(2)); - searchResponse = client().prepareSearch().setQuery(QueryBuilders.idsQuery("type1").ids("1", "2")).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); + searchResponse = client().prepareSearch().setQuery(idsQuery("type1").ids("1", "2")).get(); + assertHitCount(searchResponse, 1l); assertThat(searchResponse.getHits().hits().length, equalTo(1)); - searchResponse = client().prepareSearch().setQuery(QueryBuilders.idsQuery().ids("1")).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); + searchResponse = client().prepareSearch().setQuery(idsQuery().ids("1")).get(); + assertHitCount(searchResponse, 1l); assertThat(searchResponse.getHits().hits().length, equalTo(1)); - searchResponse = client().prepareSearch().setQuery(QueryBuilders.idsQuery(null).ids("1")).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); + searchResponse = client().prepareSearch().setQuery(idsQuery(null).ids("1")).get(); + assertHitCount(searchResponse, 1l); assertThat(searchResponse.getHits().hits().length, equalTo(1)); - searchResponse = client().prepareSearch().setQuery(QueryBuilders.idsQuery("type1", "type2", "type3").ids("1", "2", "3", "4")).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(2l)); + searchResponse = client().prepareSearch().setQuery(idsQuery("type1", "type2", "type3").ids("1", "2", "3", "4")).get(); + assertHitCount(searchResponse, 2l); assertThat(searchResponse.getHits().hits().length, equalTo(2)); } @Test public void testNumericTermsAndRanges() throws Exception { - client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)) - .addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties") - .startObject("num_byte").field("type", "byte").endObject() - .startObject("num_short").field("type", "short").endObject() - .startObject("num_integer").field("type", "integer").endObject() - .startObject("num_long").field("type", "long").endObject() - .startObject("num_float").field("type", "float").endObject() - .startObject("num_double").field("type", "double").endObject() - .endObject().endObject().endObject()) - .execute().actionGet(); + assertAcked(client().admin().indices().prepareCreate("test") + .setSettings(SETTING_NUMBER_OF_SHARDS, 1) + .addMapping("type1", + "num_byte", "type=byte", "num_short", "type=short", + "num_integer", "type=integer", "num_long", "type=long", + "num_float", "type=float", "num_double", "type=double")); ensureGreen(); - client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject() - .field("num_byte", 1) - .field("num_short", 1) - .field("num_integer", 1) - .field("num_long", 1) - .field("num_float", 1) - .field("num_double", 1) - .endObject()) - .execute().actionGet(); + client().prepareIndex("test", "type1", "1").setSource("num_byte", 1, "num_short", 1, "num_integer", 1, + "num_long", 1, "num_float", 1, "num_double", 1).get(); - client().prepareIndex("test", "type1", "2").setSource(jsonBuilder().startObject() - .field("num_byte", 2) - .field("num_short", 2) - .field("num_integer", 2) - .field("num_long", 2) - .field("num_float", 2) - .field("num_double", 2) - .endObject()) - .execute().actionGet(); + client().prepareIndex("test", "type1", "2").setSource("num_byte", 2, "num_short", 2, "num_integer", 2, + "num_long", 2, "num_float", 2, "num_double", 2).get(); - client().prepareIndex("test", "type1", "17").setSource(jsonBuilder().startObject() - .field("num_byte", 17) - .field("num_short", 17) - .field("num_integer", 17) - .field("num_long", 17) - .field("num_float", 17) - .field("num_double", 17) - .endObject()) - .execute().actionGet(); - - client().admin().indices().prepareRefresh().execute().actionGet(); + client().prepareIndex("test", "type1", "17").setSource("num_byte", 17, "num_short", 17, "num_integer", 17, + "num_long", 17, "num_float", 17, "num_double", 17).get(); + refresh(); SearchResponse searchResponse; - logger.info("--> term query on 1"); - searchResponse = client().prepareSearch("test").setQuery(termQuery("num_byte", 1)).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1")); - searchResponse = client().prepareSearch("test").setQuery(termQuery("num_short", 1)).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1")); - searchResponse = client().prepareSearch("test").setQuery(termQuery("num_integer", 1)).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1")); - searchResponse = client().prepareSearch("test").setQuery(termQuery("num_long", 1)).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1")); - searchResponse = client().prepareSearch("test").setQuery(termQuery("num_float", 1)).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1")); - searchResponse = client().prepareSearch("test").setQuery(termQuery("num_double", 1)).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1")); + searchResponse = client().prepareSearch("test").setQuery(termQuery("num_byte", 1)).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); + searchResponse = client().prepareSearch("test").setQuery(termQuery("num_short", 1)).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); + searchResponse = client().prepareSearch("test").setQuery(termQuery("num_integer", 1)).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); + searchResponse = client().prepareSearch("test").setQuery(termQuery("num_long", 1)).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); + searchResponse = client().prepareSearch("test").setQuery(termQuery("num_float", 1)).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); + searchResponse = client().prepareSearch("test").setQuery(termQuery("num_double", 1)).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); logger.info("--> terms query on 1"); - searchResponse = client().prepareSearch("test").setQuery(termsQuery("num_byte", new int[]{1})).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1")); - searchResponse = client().prepareSearch("test").setQuery(termsQuery("num_short", new int[]{1})).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1")); - searchResponse = client().prepareSearch("test").setQuery(termsQuery("num_integer", new int[]{1})).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1")); - searchResponse = client().prepareSearch("test").setQuery(termsQuery("num_long", new int[]{1})).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1")); - searchResponse = client().prepareSearch("test").setQuery(termsQuery("num_float", new double[]{1})).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1")); - searchResponse = client().prepareSearch("test").setQuery(termsQuery("num_double", new double[]{1})).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1")); + searchResponse = client().prepareSearch("test").setQuery(termsQuery("num_byte", new int[]{1})).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); + searchResponse = client().prepareSearch("test").setQuery(termsQuery("num_short", new int[]{1})).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); + searchResponse = client().prepareSearch("test").setQuery(termsQuery("num_integer", new int[]{1})).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); + searchResponse = client().prepareSearch("test").setQuery(termsQuery("num_long", new int[]{1})).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); + searchResponse = client().prepareSearch("test").setQuery(termsQuery("num_float", new double[]{1})).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); + searchResponse = client().prepareSearch("test").setQuery(termsQuery("num_double", new double[]{1})).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); logger.info("--> term filter on 1"); - searchResponse = client().prepareSearch("test").setQuery(filteredQuery(matchAllQuery(), termFilter("num_byte", 1))).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1")); - searchResponse = client().prepareSearch("test").setQuery(filteredQuery(matchAllQuery(), termFilter("num_short", 1))).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1")); - searchResponse = client().prepareSearch("test").setQuery(filteredQuery(matchAllQuery(), termFilter("num_integer", 1))).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1")); - searchResponse = client().prepareSearch("test").setQuery(filteredQuery(matchAllQuery(), termFilter("num_long", 1))).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1")); - searchResponse = client().prepareSearch("test").setQuery(filteredQuery(matchAllQuery(), termFilter("num_float", 1))).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1")); - searchResponse = client().prepareSearch("test").setQuery(filteredQuery(matchAllQuery(), termFilter("num_double", 1))).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1")); + searchResponse = client().prepareSearch("test").setQuery(filteredQuery(matchAllQuery(), termFilter("num_byte", 1))).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); + searchResponse = client().prepareSearch("test").setQuery(filteredQuery(matchAllQuery(), termFilter("num_short", 1))).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); + searchResponse = client().prepareSearch("test").setQuery(filteredQuery(matchAllQuery(), termFilter("num_integer", 1))).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); + searchResponse = client().prepareSearch("test").setQuery(filteredQuery(matchAllQuery(), termFilter("num_long", 1))).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); + searchResponse = client().prepareSearch("test").setQuery(filteredQuery(matchAllQuery(), termFilter("num_float", 1))).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); + searchResponse = client().prepareSearch("test").setQuery(filteredQuery(matchAllQuery(), termFilter("num_double", 1))).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); logger.info("--> terms filter on 1"); - searchResponse = client().prepareSearch("test").setQuery(filteredQuery(matchAllQuery(), termsFilter("num_byte", new int[]{1}))).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1")); - searchResponse = client().prepareSearch("test").setQuery(filteredQuery(matchAllQuery(), termsFilter("num_short", new int[]{1}))).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1")); - searchResponse = client().prepareSearch("test").setQuery(filteredQuery(matchAllQuery(), termsFilter("num_integer", new int[]{1}))).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1")); - searchResponse = client().prepareSearch("test").setQuery(filteredQuery(matchAllQuery(), termsFilter("num_long", new int[]{1}))).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1")); - searchResponse = client().prepareSearch("test").setQuery(filteredQuery(matchAllQuery(), termsFilter("num_float", new int[]{1}))).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1")); - searchResponse = client().prepareSearch("test").setQuery(filteredQuery(matchAllQuery(), termsFilter("num_double", new int[]{1}))).execute().actionGet(); - assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l)); - assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1")); + searchResponse = client().prepareSearch("test").setQuery(filteredQuery(matchAllQuery(), termsFilter("num_byte", new int[]{1}))).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); + searchResponse = client().prepareSearch("test").setQuery(filteredQuery(matchAllQuery(), termsFilter("num_short", new int[]{1}))).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); + searchResponse = client().prepareSearch("test").setQuery(filteredQuery(matchAllQuery(), termsFilter("num_integer", new int[]{1}))).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); + searchResponse = client().prepareSearch("test").setQuery(filteredQuery(matchAllQuery(), termsFilter("num_long", new int[]{1}))).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); + searchResponse = client().prepareSearch("test").setQuery(filteredQuery(matchAllQuery(), termsFilter("num_float", new int[]{1}))).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); + searchResponse = client().prepareSearch("test").setQuery(filteredQuery(matchAllQuery(), termsFilter("num_double", new int[]{1}))).get(); + assertHitCount(searchResponse, 1l); + assertFirstHit(searchResponse, hasId("1")); } @Test public void testNumericRangeFilter_2826() throws Exception { - client().admin().indices().prepareCreate("test").setSettings( - ImmutableSettings.settingsBuilder() - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 0) - ) - .addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties") - .startObject("num_byte").field("type", "byte").endObject() - .startObject("num_short").field("type", "short").endObject() - .startObject("num_integer").field("type", "integer").endObject() - .startObject("num_long").field("type", "long").endObject() - .startObject("num_float").field("type", "float").endObject() - .startObject("num_double").field("type", "double").endObject() - .endObject().endObject().endObject()) - .execute().actionGet(); + assertAcked(client().admin().indices().prepareCreate("test") + .setSettings(SETTING_NUMBER_OF_SHARDS, 1, SETTING_NUMBER_OF_REPLICAS, 0) + .addMapping("type1", + "num_byte", "type=byte", "num_short", "type=short", + "num_integer", "type=integer", "num_long", "type=long", + "num_float", "type=float", "num_double", "type=double")); ensureGreen(); - client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject() - .field("field1", "test1") - .field("num_long", 1) - .endObject()) - .execute().actionGet(); + client().prepareIndex("test", "type1", "1").setSource("field1", "test1", "num_long", 1).get(); + client().prepareIndex("test", "type1", "2").setSource("field1", "test1", "num_long", 2).get(); + client().prepareIndex("test", "type1", "3").setSource("field1", "test2", "num_long", 3).get(); + client().prepareIndex("test", "type1", "4").setSource("field1", "test2", "num_long", 4).get(); + refresh(); - client().prepareIndex("test", "type1", "2").setSource(jsonBuilder().startObject() - .field("field1", "test1") - .field("num_long", 2) - .endObject()) - .execute().actionGet(); - - client().prepareIndex("test", "type1", "3").setSource(jsonBuilder().startObject() - .field("field1", "test2") - .field("num_long", 3) - .endObject()) - .execute().actionGet(); - - client().prepareIndex("test", "type1", "4").setSource(jsonBuilder().startObject() - .field("field1", "test2") - .field("num_long", 4) - .endObject()) - .execute().actionGet(); - - client().admin().indices().prepareRefresh().execute().actionGet(); - SearchResponse response = client().prepareSearch("test").setFilter( - FilterBuilders.boolFilter() + SearchResponse searchResponse = client().prepareSearch("test").setFilter( + boolFilter() .should(rangeFilter("num_long", 1, 2)) .should(rangeFilter("num_long", 3, 4)) - ).execute().actionGet(); - assertThat(response.getHits().totalHits(), equalTo(4l)); + ).get(); + assertHitCount(searchResponse, 4l); // This made 2826 fail! (only with bit based filters) - response = client().prepareSearch("test").setFilter( - FilterBuilders.boolFilter() + searchResponse = client().prepareSearch("test").setFilter( + boolFilter() .should(rangeFilter("num_long", 1, 2)) .should(rangeFilter("num_long", 3, 4)) - ).execute().actionGet(); - - assertThat(response.getHits().totalHits(), equalTo(4l)); + ).get(); + assertHitCount(searchResponse, 4l); // This made #2979 fail! - response = client().prepareSearch("test").setFilter( - FilterBuilders.boolFilter() - .must(FilterBuilders.termFilter("field1", "test1")) + searchResponse = client().prepareSearch("test").setFilter( + boolFilter() + .must(termFilter("field1", "test1")) .should(rangeFilter("num_long", 1, 2)) .should(rangeFilter("num_long", 3, 4)) - ).execute().actionGet(); - - assertThat(response.getHits().totalHits(), equalTo(2l)); + ).get(); + assertHitCount(searchResponse, 2l); } @Test public void testEmptyTopLevelFilter() { - client().prepareIndex("test", "type", "1").setSource("field", "value").setRefresh(true).execute().actionGet(); - SearchResponse searchResponse = client().prepareSearch().setFilter("{}").execute().actionGet(); - assertNoFailures(searchResponse); + client().prepareIndex("test", "type", "1").setSource("field", "value").setRefresh(true).get(); + SearchResponse searchResponse = client().prepareSearch().setFilter("{}").get(); + assertHitCount(searchResponse, 1l); } @Test // see #2926 - public void testMustNot() throws ElasticSearchException, IOException { - client().admin().indices().prepareCreate("test").setSettings( - ImmutableSettings.settingsBuilder() - .put("index.number_of_shards", 2) - .put("index.number_of_replicas", 0) - ) - .execute().actionGet(); + public void testMustNot() throws ElasticSearchException, IOException, ExecutionException, InterruptedException { + assertAcked(client().admin().indices().prepareCreate("test") + .setSettings(SETTING_NUMBER_OF_SHARDS, 2, SETTING_NUMBER_OF_REPLICAS, 0)); ensureGreen(); - client().prepareIndex("test", "test", "1").setSource(jsonBuilder().startObject() - .field("description", "foo other anything bar") - .endObject()) - .execute().actionGet(); + indexRandom(true, client().prepareIndex("test", "test", "1").setSource("description", "foo other anything bar"), + client().prepareIndex("test", "test", "2").setSource("description", "foo other anything"), + client().prepareIndex("test", "test", "3").setSource("description", "foo other"), + client().prepareIndex("test", "test", "4").setSource("description", "foo")); - client().prepareIndex("test", "test", "2").setSource(jsonBuilder().startObject() - .field("description", "foo other anything") - .endObject()) - .execute().actionGet(); + SearchResponse searchResponse = client().prepareSearch("test").setQuery(matchAllQuery()) + .setSearchType(SearchType.DFS_QUERY_THEN_FETCH).get(); + assertHitCount(searchResponse, 4l); - 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(); - assertNoFailures(response); - 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)); + searchResponse = client().prepareSearch("test").setQuery( + boolQuery() + .mustNot(matchQuery("description", "anything").type(Type.BOOLEAN)) + ).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).get(); + assertHitCount(searchResponse, 2l); } @Test // see #2994 - public void testSimpleSpan() throws ElasticSearchException, IOException { - - client().admin().indices().prepareCreate("test").setSettings( - ImmutableSettings.settingsBuilder() - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 0) - ) - .execute().actionGet(); + public void testSimpleSpan() throws ElasticSearchException, IOException, ExecutionException, InterruptedException { + assertAcked(client().admin().indices().prepareCreate("test") + .setSettings(SETTING_NUMBER_OF_SHARDS, 1, SETTING_NUMBER_OF_REPLICAS, 0)); ensureGreen(); - client().prepareIndex("test", "test", "1").setSource(jsonBuilder().startObject() - .field("description", "foo other anything bar") - .endObject()) - .execute().actionGet(); + indexRandom(true, client().prepareIndex("test", "test", "1").setSource("description", "foo other anything bar"), + client().prepareIndex("test", "test", "2").setSource("description", "foo other anything"), + client().prepareIndex("test", "test", "3").setSource("description", "foo other"), + client().prepareIndex("test", "test", "4").setSource("description", "foo")); - client().prepareIndex("test", "test", "2").setSource(jsonBuilder().startObject() - .field("description", "foo other anything") - .endObject()) - .execute().actionGet(); + SearchResponse searchResponse = client().prepareSearch("test") + .setQuery(spanOrQuery().clause(spanTermQuery("description", "bar"))).get(); + assertHitCount(searchResponse, 1l); - client().prepareIndex("test", "test", "3").setSource(jsonBuilder().startObject() - .field("description", "foo other") - .endObject()) - .execute().actionGet(); + searchResponse = client().prepareSearch("test") + .setQuery(spanOrQuery().clause(spanTermQuery("test.description", "bar"))).get(); + assertHitCount(searchResponse, 1l); - 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.spanOrQuery().clause(QueryBuilders.spanTermQuery("description", "bar"))) - .execute().actionGet(); - assertNoFailures(response); - assertHitCount(response, 1l); - response = client().prepareSearch("test") - .setQuery(QueryBuilders.spanOrQuery().clause(QueryBuilders.spanTermQuery("test.description", "bar"))) - .execute().actionGet(); - assertNoFailures(response); - assertHitCount(response, 1l); - - response = client().prepareSearch("test").setQuery( - QueryBuilders.spanNearQuery() - .clause(QueryBuilders.spanTermQuery("description", "foo")) - .clause(QueryBuilders.spanTermQuery("test.description", "other")) - .slop(3)).execute().actionGet(); - assertNoFailures(response); - assertHitCount(response, 3l); + searchResponse = client().prepareSearch("test").setQuery( + spanNearQuery() + .clause(spanTermQuery("description", "foo")) + .clause(spanTermQuery("test.description", "other")) + .slop(3)).get(); + assertHitCount(searchResponse, 3l); } @Test public void testSpanMultiTermQuery() throws ElasticSearchException, IOException { - - client().admin().indices().prepareCreate("test").setSettings( - ImmutableSettings.settingsBuilder() - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 0) - ) - .execute().actionGet(); + assertAcked(prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 1, SETTING_NUMBER_OF_REPLICAS, 0)); ensureGreen(); - client().prepareIndex("test", "test", "1").setSource(jsonBuilder().startObject() - .field("description", "foo other anything bar") - .field("count", 1) - .endObject()) - .execute().actionGet(); - - client().prepareIndex("test", "test", "2").setSource(jsonBuilder().startObject() - .field("description", "foo other anything") - .field("count", 2) - .endObject()) - .execute().actionGet(); - - client().prepareIndex("test", "test", "3").setSource(jsonBuilder().startObject() - .field("description", "foo other") - .field("count", 3) - .endObject()) - .execute().actionGet(); - - client().prepareIndex("test", "test", "4").setSource(jsonBuilder().startObject() - .field("description", "fop") - .field("count", 4) - .endObject()) - .execute().actionGet(); - + client().prepareIndex("test", "test", "1").setSource("description", "foo other anything bar", "count", 1).get(); + client().prepareIndex("test", "test", "2").setSource("description", "foo other anything", "count", 2).get(); + client().prepareIndex("test", "test", "3").setSource("description", "foo other", "count", 3).get(); + client().prepareIndex("test", "test", "4").setSource("description", "fop", "count", 4).get(); refresh(); SearchResponse response = client().prepareSearch("test") - .setQuery(QueryBuilders.spanOrQuery().clause(QueryBuilders.spanMultiTermQueryBuilder(QueryBuilders.fuzzyQuery("description", "fop")))) - .execute().actionGet(); - assertNoFailures(response); + .setQuery(spanOrQuery().clause(spanMultiTermQueryBuilder(fuzzyQuery("description", "fop")))).get(); assertHitCount(response, 4); response = client().prepareSearch("test") - .setQuery(QueryBuilders.spanOrQuery().clause(QueryBuilders.spanMultiTermQueryBuilder(QueryBuilders.prefixQuery("description", "fo")))) - .execute().actionGet(); - assertNoFailures(response); + .setQuery(spanOrQuery().clause(spanMultiTermQueryBuilder(prefixQuery("description", "fo")))).get(); assertHitCount(response, 4); response = client().prepareSearch("test") - .setQuery(QueryBuilders.spanOrQuery().clause(QueryBuilders.spanMultiTermQueryBuilder(QueryBuilders.wildcardQuery("description", "oth*")))) - .execute().actionGet(); - assertNoFailures(response); + .setQuery(spanOrQuery().clause(spanMultiTermQueryBuilder(wildcardQuery("description", "oth*")))).get(); assertHitCount(response, 3); response = client().prepareSearch("test") - .setQuery(QueryBuilders.spanOrQuery().clause(QueryBuilders.spanMultiTermQueryBuilder(QueryBuilders.rangeQuery("description").from("ffa").to("foo")))) + .setQuery(spanOrQuery().clause(spanMultiTermQueryBuilder(rangeQuery("description").from("ffa").to("foo")))) .execute().actionGet(); - assertNoFailures(response); assertHitCount(response, 3); response = client().prepareSearch("test") - .setQuery(QueryBuilders.spanOrQuery().clause(QueryBuilders.spanMultiTermQueryBuilder(QueryBuilders.regexpQuery("description", "fo{2}")))) - .execute().actionGet(); - assertNoFailures(response); + .setQuery(spanOrQuery().clause(spanMultiTermQueryBuilder(regexpQuery("description", "fo{2}")))).get(); assertHitCount(response, 3); } @Test public void testSimpleDFSQuery() throws ElasticSearchException, IOException { - prepareCreate("test", -1, - ImmutableSettings.settingsBuilder() - .put("index.number_of_shards", 5) - .put("index.number_of_replicas", 0) - ).addMapping("s", jsonBuilder() + assertAcked(prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 5, SETTING_NUMBER_OF_REPLICAS, 0) + .addMapping("s", jsonBuilder() .startObject() .startObject("s") .startObject("_routing") @@ -1711,71 +1360,33 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest { .endObject() .endObject() .endObject()) - .addMapping("bs", jsonBuilder() - .startObject() - .startObject("bs") - .startObject("properties") - .startObject("online") - .field("type", "boolean") - .endObject() - .startObject("ts") - .field("type", "date") - .field("ignore_malformed", false) - .field("format", "dateOptionalTime") - .endObject() - .endObject() - .endObject() - .endObject()) - .execute().actionGet(); + .addMapping("bs", "online", "type=boolean", "ts", "type=date,ignore_malformed=false,format=dateOptionalTime")); ensureGreen(); - client().prepareIndex("test", "s", "1").setSource(jsonBuilder().startObject() - .field("online", false) - .field("bs", "Y") - .field("ts", System.currentTimeMillis() - 100) - .endObject()) - .execute().actionGet(); + client().prepareIndex("test", "s", "1").setSource("online", false, "bs", "Y", "ts", System.currentTimeMillis() - 100).get(); + client().prepareIndex("test", "s", "2").setSource("online", true, "bs", "X", "ts", System.currentTimeMillis() - 10000000).get(); + client().prepareIndex("test", "bs", "3").setSource("online", false, "ts", System.currentTimeMillis() - 100).get(); + client().prepareIndex("test", "bs", "4").setSource("online", true, "ts", System.currentTimeMillis() - 123123).get(); + refresh(); - client().prepareIndex("test", "s", "2").setSource(jsonBuilder().startObject() - .field("online", true) - .field("bs", "X") - .field("ts", System.currentTimeMillis() - 10000000) - .endObject()) - .execute().actionGet(); - - client().prepareIndex("test", "bs", "3").setSource(jsonBuilder().startObject() - .field("online", false) - .field("ts", System.currentTimeMillis() - 100) - .endObject()) - .execute().actionGet(); - - client().prepareIndex("test", "bs", "4").setSource(jsonBuilder().startObject() - .field("online", true) - .field("ts", System.currentTimeMillis() - 123123) - .endObject()) - .execute().actionGet(); - - client().admin().indices().prepareRefresh().execute().actionGet(); SearchResponse response = client().prepareSearch("test") .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) .setQuery( - QueryBuilders.boolQuery() - .must(QueryBuilders.termQuery("online", true)) - .must(QueryBuilders.boolQuery() - .should(QueryBuilders.boolQuery() - .must(QueryBuilders.rangeQuery("ts").lt(System.currentTimeMillis() - (15 * 1000))) - .must(QueryBuilders.termQuery("_type", "bs")) + boolQuery() + .must(termQuery("online", true)) + .must(boolQuery() + .should(boolQuery() + .must(rangeQuery("ts").lt(System.currentTimeMillis() - (15 * 1000))) + .must(termQuery("_type", "bs")) ) - .should(QueryBuilders.boolQuery() - .must(QueryBuilders.rangeQuery("ts").lt(System.currentTimeMillis() - (15 * 1000))) - .must(QueryBuilders.termQuery("_type", "s")) + .should(boolQuery() + .must(rangeQuery("ts").lt(System.currentTimeMillis() - (15 * 1000))) + .must(termQuery("_type", "s")) ) ) ) .setVersion(true) - .setFrom(0).setSize(100).setExplain(true) - .execute() - .actionGet(); + .setFrom(0).setSize(100).setExplain(true).get(); assertNoFailures(response); } @@ -1810,42 +1421,25 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest { .put("index.analysis.analyzer.index.filter", "lowercase") .put("index.analysis.analyzer.search.type", "custom") .put("index.analysis.analyzer.search.tokenizer", "standard") - .putArray("index.analysis.analyzer.search.filter", "lowercase", "synonym") .put("index.analysis.filter.synonym.type", "synonym") .putArray("index.analysis.filter.synonym.synonyms", "fast, quick")); - - XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("test") - .startObject("properties") - .startObject("text") - .field("type", "string") - .field("index_analyzer", "index") - .field("search_analyzer", "search") - .endObject() - .endObject() - .endObject().endObject(); - assertAcked(builder.addMapping("test", mapping)); + assertAcked(builder.addMapping("test", "text", "type=string,index_analyzer=index,search_analyzer=search")); ensureGreen(); - client().prepareIndex("test", "test", "1").setSource(jsonBuilder().startObject() - .field("text", "quick brown fox") - .endObject()) - .execute().actionGet(); - client().admin().indices().prepareRefresh().execute().actionGet(); - SearchResponse searchResponse = client().prepareSearch("test").setQuery(QueryBuilders.matchQuery("text", "quick").operator(MatchQueryBuilder.Operator.AND)).get(); + client().prepareIndex("test", "test", "1").setSource("text", "quick brown fox").get(); + refresh(); + SearchResponse searchResponse = client().prepareSearch("test").setQuery(matchQuery("text", "quick").operator(MatchQueryBuilder.Operator.AND)).get(); assertHitCount(searchResponse, 1); - searchResponse = client().prepareSearch("test").setQuery(QueryBuilders.matchQuery("text", "quick brown").operator(MatchQueryBuilder.Operator.AND)).get(); + searchResponse = client().prepareSearch("test").setQuery(matchQuery("text", "quick brown").operator(MatchQueryBuilder.Operator.AND)).get(); assertHitCount(searchResponse, 1); - searchResponse = client().prepareSearch("test").setQuery(QueryBuilders.matchQuery("text", "fast").operator(MatchQueryBuilder.Operator.AND)).get(); + searchResponse = client().prepareSearch("test").setQuery(matchQuery("text", "fast").operator(MatchQueryBuilder.Operator.AND)).get(); assertHitCount(searchResponse, 1); - - client().prepareIndex("test", "test", "2").setSource(jsonBuilder().startObject() - .field("text", "fast brown fox") - .endObject()) - .execute().actionGet(); - client().admin().indices().prepareRefresh().execute().actionGet(); - searchResponse = client().prepareSearch("test").setQuery(QueryBuilders.matchQuery("text", "quick").operator(MatchQueryBuilder.Operator.AND)).get(); + + client().prepareIndex("test", "test", "2").setSource("text", "fast brown fox").get(); + refresh(); + searchResponse = client().prepareSearch("test").setQuery(matchQuery("text", "quick").operator(MatchQueryBuilder.Operator.AND)).get(); assertHitCount(searchResponse, 2); - searchResponse = client().prepareSearch("test").setQuery(QueryBuilders.matchQuery("text", "quick brown").operator(MatchQueryBuilder.Operator.AND)).get(); + searchResponse = client().prepareSearch("test").setQuery(matchQuery("text", "quick brown").operator(MatchQueryBuilder.Operator.AND)).get(); assertHitCount(searchResponse, 2); } @@ -1862,32 +1456,16 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest { .putArray("index.analysis.analyzer.search.filter", "lowercase", "keyword_repeat", "porterStem", "unique_stem") .put("index.analysis.filter.unique_stem.type", "unique") .put("index.analysis.filter.unique_stem.only_on_same_position", true)); - - XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("test") - .startObject("properties") - .startObject("text") - .field("type", "string") - .field("index_analyzer", "index") - .field("search_analyzer", "search") - .endObject() - .endObject() - .endObject().endObject(); - assertAcked(builder.addMapping("test", mapping)); + assertAcked(builder.addMapping("test", "text", "type=string,index_analyzer=index,search_analyzer=search")); ensureGreen(); - client().prepareIndex("test", "test", "1").setSource(jsonBuilder().startObject() - .field("text", "the fox runs across the street") - .endObject()) - .execute().actionGet(); - client().admin().indices().prepareRefresh().execute().actionGet(); - SearchResponse searchResponse = client().prepareSearch("test").setQuery(QueryBuilders.matchQuery("text", "fox runs").operator(MatchQueryBuilder.Operator.AND)).get(); + client().prepareIndex("test", "test", "1").setSource("text", "the fox runs across the street").get(); + refresh(); + SearchResponse searchResponse = client().prepareSearch("test").setQuery(matchQuery("text", "fox runs").operator(MatchQueryBuilder.Operator.AND)).get(); assertHitCount(searchResponse, 1); - client().prepareIndex("test", "test", "2").setSource(jsonBuilder().startObject() - .field("text", "run fox run") - .endObject()) - .execute().actionGet(); - client().admin().indices().prepareRefresh().execute().actionGet(); - searchResponse = client().prepareSearch("test").setQuery(QueryBuilders.matchQuery("text", "fox runs").operator(MatchQueryBuilder.Operator.AND)).get(); + client().prepareIndex("test", "test", "2").setSource("text", "run fox run").get(); + refresh(); + searchResponse = client().prepareSearch("test").setQuery(matchQuery("text", "fox runs").operator(MatchQueryBuilder.Operator.AND)).get(); assertHitCount(searchResponse, 2); } @@ -1901,48 +1479,34 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest { .put("index.analysis.analyzer.index.filter", "lowercase") .put("index.analysis.analyzer.search.type", "custom") .put("index.analysis.analyzer.search.tokenizer", "standard") - .putArray("index.analysis.analyzer.search.filter", "lowercase", "synonym") .put("index.analysis.filter.synonym.type", "synonym") .putArray("index.analysis.filter.synonym.synonyms", "fast, quick")); - - XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("test") - .startObject("properties") - .startObject("text") - .field("type", "string") - .field("index_analyzer", "index") - .field("search_analyzer", "search") - .endObject() - .endObject() - .endObject().endObject(); - assertAcked(builder.addMapping("test", mapping)); + assertAcked(builder.addMapping("test", "text", "type=string,index_analyzer=index,search_analyzer=search")); ensureGreen(); - client().prepareIndex("test", "test", "1").setSource(jsonBuilder().startObject() - .field("text", "quick brown fox") - .endObject()) - .execute().actionGet(); - client().admin().indices().prepareRefresh().execute().actionGet(); - SearchResponse searchResponse = client().prepareSearch("test").setQuery(QueryBuilders.queryString("quick").defaultField("text").defaultOperator(QueryStringQueryBuilder.Operator.AND)).get(); - assertHitCount(searchResponse, 1); - searchResponse = client().prepareSearch("test").setQuery(QueryBuilders.queryString("quick brown").defaultField("text").defaultOperator(QueryStringQueryBuilder.Operator.AND)).get(); - assertHitCount(searchResponse, 1); - searchResponse = client().prepareSearch().setQuery(QueryBuilders.queryString("fast").defaultField("text").defaultOperator(QueryStringQueryBuilder.Operator.AND)).get(); - assertHitCount(searchResponse, 1); - client().prepareIndex("test", "test", "2").setSource(jsonBuilder().startObject() - .field("text", "fast brown fox") - .endObject()) - .execute().actionGet(); - client().admin().indices().prepareRefresh().execute().actionGet(); - searchResponse = client().prepareSearch("test").setQuery(QueryBuilders.queryString("quick").defaultField("text").defaultOperator(QueryStringQueryBuilder.Operator.AND)).get(); + client().prepareIndex("test", "test", "1").setSource("text", "quick brown fox").get(); + refresh(); + + SearchResponse searchResponse = client().prepareSearch("test").setQuery(queryString("quick").defaultField("text").defaultOperator(QueryStringQueryBuilder.Operator.AND)).get(); + assertHitCount(searchResponse, 1); + searchResponse = client().prepareSearch("test").setQuery(queryString("quick brown").defaultField("text").defaultOperator(QueryStringQueryBuilder.Operator.AND)).get(); + assertHitCount(searchResponse, 1); + searchResponse = client().prepareSearch().setQuery(queryString("fast").defaultField("text").defaultOperator(QueryStringQueryBuilder.Operator.AND)).get(); + assertHitCount(searchResponse, 1); + + client().prepareIndex("test", "test", "2").setSource("text", "fast brown fox").get(); + refresh(); + + searchResponse = client().prepareSearch("test").setQuery(queryString("quick").defaultField("text").defaultOperator(QueryStringQueryBuilder.Operator.AND)).get(); assertHitCount(searchResponse, 2); - searchResponse = client().prepareSearch("test").setQuery(QueryBuilders.queryString("quick brown").defaultField("text").defaultOperator(QueryStringQueryBuilder.Operator.AND)).get(); + searchResponse = client().prepareSearch("test").setQuery(queryString("quick brown").defaultField("text").defaultOperator(QueryStringQueryBuilder.Operator.AND)).get(); assertHitCount(searchResponse, 2); } @Test // see https://github.com/elasticsearch/elasticsearch/issues/3898 public void testCustomWordDelimiterQueryString() { - client().admin().indices().prepareCreate("test") + assertAcked(client().admin().indices().prepareCreate("test") .setSettings("analysis.analyzer.my_analyzer.type", "custom", "analysis.analyzer.my_analyzer.tokenizer", "whitespace", "analysis.analyzer.my_analyzer.filter", "custom_word_delimiter", @@ -1954,9 +1518,7 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest { "analysis.filter.custom_word_delimiter.split_on_case_change", "false", "analysis.filter.custom_word_delimiter.split_on_numerics", "false", "analysis.filter.custom_word_delimiter.stem_english_possessive", "false") - .addMapping("type1", "field1", "type=string,analyzer=my_analyzer", "field2", "type=string,analyzer=my_analyzer") - .get(); - + .addMapping("type1", "field1", "type=string,analyzer=my_analyzer", "field2", "type=string,analyzer=my_analyzer")); ensureGreen(); client().prepareIndex("test", "type1", "1").setSource("field1", "foo bar baz", "field2", "not needed").get(); @@ -1965,9 +1527,8 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest { SearchResponse response = client() .prepareSearch("test") .setQuery( - QueryBuilders.queryString("foo.baz").useDisMax(false).defaultOperator(QueryStringQueryBuilder.Operator.AND) + queryString("foo.baz").useDisMax(false).defaultOperator(QueryStringQueryBuilder.Operator.AND) .field("field1").field("field2")).get(); - assertHitCount(response, 1l); } @@ -1979,15 +1540,15 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest { refresh(); SearchResponse searchResponse = client().prepareSearch("test") - .setQuery(QueryBuilders.multiMatchQuery("value2", "field1^2", "field2").lenient(true).useDisMax(false)).get(); + .setQuery(multiMatchQuery("value2", "field1^2", "field2").lenient(true).useDisMax(false)).get(); assertHitCount(searchResponse, 1l); searchResponse = client().prepareSearch("test") - .setQuery(QueryBuilders.multiMatchQuery("value2", "field1^2", "field2").lenient(true).useDisMax(true)).get(); + .setQuery(multiMatchQuery("value2", "field1^2", "field2").lenient(true).useDisMax(true)).get(); assertHitCount(searchResponse, 1l); searchResponse = client().prepareSearch("test") - .setQuery(QueryBuilders.multiMatchQuery("value2", "field2^2").lenient(true)).get(); + .setQuery(multiMatchQuery("value2", "field2^2").lenient(true)).get(); assertHitCount(searchResponse, 1l); } @@ -2134,14 +1695,24 @@ public class SimpleQueryTests extends ElasticsearchIntegrationTest { client().prepareIndex("test", "test", "2").setSource("score", 1).get(); client().prepareIndex("test", "test", "3").setSource("score", 2).get(); client().prepareIndex("test", "test", "4").setSource("score", 0.5).get(); - refresh(); SearchResponse searchResponse = client().prepareSearch("test").setQuery( functionScoreQuery(scriptFunction("_doc['score'].value"))).setMinScore(1.5f).get(); assertHitCount(searchResponse, 2); - assertFirstHit(searchResponse, hasId("3")); assertSecondHit(searchResponse, hasId("1")); } + + private static FilterBuilder rangeFilter(String field, Object from, Object to) { + if (randomBoolean()) { + if (randomBoolean()) { + return FilterBuilders.rangeFilter(field).from(from).to(to); + } else { + return FilterBuilders.rangeFilter(field).from(from).to(to).setExecution("fielddata"); + } + } else { + return FilterBuilders.numericRangeFilter(field).from(from).to(to); + } + } } diff --git a/src/test/java/org/elasticsearch/test/hamcrest/ElasticsearchAssertions.java b/src/test/java/org/elasticsearch/test/hamcrest/ElasticsearchAssertions.java index 3fa71527c60..ef2aebd92c4 100644 --- a/src/test/java/org/elasticsearch/test/hamcrest/ElasticsearchAssertions.java +++ b/src/test/java/org/elasticsearch/test/hamcrest/ElasticsearchAssertions.java @@ -131,7 +131,7 @@ public class ElasticsearchAssertions { assertThat("Expected id: " + hit.getId() + " in the result but wasn't." + shardStatus, idsSet.remove(hit.getId()), equalTo(true)); } - assertThat("Expected ids: " + Arrays.toString(idsSet.toArray(new String[0])) + " in the result - result size differs." + assertThat("Expected ids: " + Arrays.toString(idsSet.toArray(new String[idsSet.size()])) + " in the result - result size differs." + shardStatus, idsSet.size(), equalTo(0)); assertVersionSerializable(searchResponse); } @@ -283,6 +283,10 @@ public class ElasticsearchAssertions { return new ElasticsearchMatchers.SearchHitHasIndexMatcher(index); } + public static Matcher hasScore(final float score) { + return new ElasticsearchMatchers.SearchHitHasScoreMatcher(score); + } + public static T assertBooleanSubQuery(Query query, Class subqueryType, int i) { assertThat(query, instanceOf(BooleanQuery.class)); BooleanQuery q = (BooleanQuery) query; diff --git a/src/test/java/org/elasticsearch/test/hamcrest/ElasticsearchMatchers.java b/src/test/java/org/elasticsearch/test/hamcrest/ElasticsearchMatchers.java index d3cfbe4d19c..96a9eca3917 100644 --- a/src/test/java/org/elasticsearch/test/hamcrest/ElasticsearchMatchers.java +++ b/src/test/java/org/elasticsearch/test/hamcrest/ElasticsearchMatchers.java @@ -93,4 +93,26 @@ public class ElasticsearchMatchers { } } + public static class SearchHitHasScoreMatcher extends TypeSafeMatcher { + private float score; + + public SearchHitHasScoreMatcher(float score) { + this.score = score; + } + + @Override + protected boolean matchesSafely(SearchHit searchHit) { + return searchHit.getScore() == score; + } + + @Override + public void describeMismatchSafely(final SearchHit searchHit, final Description mismatchDescription) { + mismatchDescription.appendText(" was ").appendValue(searchHit.getScore()); + } + + @Override + public void describeTo(final Description description) { + description.appendText("searchHit score should be ").appendValue(score); + } + } }