diff --git a/src/test/java/org/elasticsearch/test/integration/AbstractSharedClusterTest.java b/src/test/java/org/elasticsearch/test/integration/AbstractSharedClusterTest.java index b7294b89184..ff99d12beba 100644 --- a/src/test/java/org/elasticsearch/test/integration/AbstractSharedClusterTest.java +++ b/src/test/java/org/elasticsearch/test/integration/AbstractSharedClusterTest.java @@ -20,13 +20,19 @@ package org.elasticsearch.test.integration; import com.google.common.base.Joiner; import com.google.common.collect.Iterators; +import org.elasticsearch.action.ActionRequestBuilder; +import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest; import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus; import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder; import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse; +import org.elasticsearch.action.admin.indices.flush.FlushResponse; import org.elasticsearch.action.admin.indices.optimize.OptimizeResponse; import org.elasticsearch.action.admin.indices.refresh.RefreshResponse; +import org.elasticsearch.action.support.broadcast.BroadcastOperationRequestBuilder; +import org.elasticsearch.action.support.broadcast.BroadcastOperationResponse; +import org.elasticsearch.client.AdminClient; import org.elasticsearch.client.Client; import org.elasticsearch.client.Requests; import org.elasticsearch.cluster.ClusterService; @@ -188,6 +194,23 @@ public abstract class AbstractSharedClusterTest extends ElasticsearchTestCase { } return client().admin().indices().prepareCreate(index).setSettings(builder.build()); } + + public CreateIndexRequestBuilder addMapping(CreateIndexRequestBuilder builder, String type, Object[]... mapping) throws IOException { + XContentBuilder mappingBuilder = jsonBuilder(); + mappingBuilder.startObject().startObject(type).startObject("properties"); + for (Object[] objects : mapping) { + mappingBuilder.startObject(objects[0].toString()); + for (int i = 1; i < objects.length; i++) { + String name = objects[i++].toString(); + Object value = objects[i]; + mappingBuilder.field(name,value); + } + mappingBuilder.endObject().endObject().endObject(); + } + mappingBuilder.endObject(); + builder.addMapping(type, mappingBuilder ); + return builder; + } private ImmutableSettings.Builder getExcludeSettings(String index, int num, ImmutableSettings.Builder builder) { String exclude = Joiner.on(',').join(cluster().allButN(num)); @@ -287,6 +310,12 @@ public abstract class AbstractSharedClusterTest extends ElasticsearchTestCase { return actionGet; } + protected FlushResponse flush() { + FlushResponse actionGet = client().admin().indices().prepareFlush().setRefresh(true).execute().actionGet(); + assertNoFailures(actionGet); + return actionGet; + } + protected OptimizeResponse optimize() { OptimizeResponse actionGet = client().admin().indices().prepareOptimize().execute().actionGet(); assertNoFailures(actionGet); @@ -318,5 +347,20 @@ public abstract class AbstractSharedClusterTest extends ElasticsearchTestCase { IndicesExistsResponse actionGet = client().admin().indices().prepareExists(index).execute().actionGet(); return actionGet.isExists(); } + + protected AdminClient admin() { + return client().admin(); + } + + protected Res run(ActionRequestBuilder builder) { + Res actionGet = builder.execute().actionGet(); + return actionGet; + } + + protected Res run(BroadcastOperationRequestBuilder builder) { + Res actionGet = builder.execute().actionGet(); + assertNoFailures(actionGet); + return actionGet; + } } diff --git a/src/test/java/org/elasticsearch/test/integration/nested/SimpleNestedTests.java b/src/test/java/org/elasticsearch/test/integration/nested/SimpleNestedTests.java index e53a7430f6c..8072d76eb94 100644 --- a/src/test/java/org/elasticsearch/test/integration/nested/SimpleNestedTests.java +++ b/src/test/java/org/elasticsearch/test/integration/nested/SimpleNestedTests.java @@ -54,25 +54,15 @@ public class SimpleNestedTests extends AbstractSharedClusterTest { @Test public void simpleNested() throws Exception { - client().admin().indices().prepareDelete().execute().actionGet(); - - client().admin().indices().prepareCreate("test") - .addMapping("type1", jsonBuilder().startObject().startObject("type1").startObject("properties") - .startObject("nested1") - .field("type", "nested") - .endObject() - .endObject().endObject().endObject()) - .execute().actionGet(); - - client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet(); - + run(addMapping(prepareCreate("test"), "type1", new Object[] {"nested1", "type", "nested"})); + ensureGreen(); // check on no data, see it works SearchResponse searchResponse = client().prepareSearch("test").setQuery(termQuery("_all", "n_value1_1")).execute().actionGet(); assertThat(searchResponse.getHits().totalHits(), equalTo(0l)); searchResponse = client().prepareSearch("test").setQuery(termQuery("nested1.n_field1", "n_value1_1")).execute().actionGet(); assertThat(searchResponse.getHits().totalHits(), equalTo(0l)); - + client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject() .field("field1", "value1") .startArray("nested1") @@ -88,13 +78,13 @@ public class SimpleNestedTests extends AbstractSharedClusterTest { .endObject()).execute().actionGet(); // flush, so we fetch it from the index (as see that we filter nested docs) - client().admin().indices().prepareFlush().setRefresh(true).execute().actionGet(); - GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet(); + flush(); + GetResponse getResponse = run(client().prepareGet("test", "type1", "1")); assertThat(getResponse.isExists(), equalTo(true)); assertThat(getResponse.getSourceAsBytes(), notNullValue()); // check the numDocs - IndicesStatusResponse statusResponse = client().admin().indices().prepareStatus().execute().actionGet(); + IndicesStatusResponse statusResponse = run(admin().indices().prepareStatus()); assertThat(statusResponse.getIndex("test").getDocs().getNumDocs(), equalTo(3l)); // check that _all is working on nested docs @@ -104,17 +94,17 @@ public class SimpleNestedTests extends AbstractSharedClusterTest { assertThat(searchResponse.getHits().totalHits(), equalTo(0l)); // search for something that matches the nested doc, and see that we don't find the nested doc - searchResponse = client().prepareSearch("test").setQuery(matchAllQuery()).execute().actionGet(); + searchResponse = run(client().prepareSearch("test").setQuery(matchAllQuery())); assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); - searchResponse = client().prepareSearch("test").setQuery(termQuery("nested1.n_field1", "n_value1_1")).execute().actionGet(); + searchResponse = run(client().prepareSearch("test").setQuery(termQuery("nested1.n_field1", "n_value1_1"))); assertThat(searchResponse.getHits().totalHits(), equalTo(0l)); // now, do a nested query - searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", termQuery("nested1.n_field1", "n_value1_1"))).execute().actionGet(); + searchResponse = run(client().prepareSearch("test").setQuery(nestedQuery("nested1", termQuery("nested1.n_field1", "n_value1_1")))); assertThat(Arrays.toString(searchResponse.getShardFailures()), searchResponse.getFailedShards(), equalTo(0)); assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); - searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", termQuery("nested1.n_field1", "n_value1_1"))).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).execute().actionGet(); + searchResponse = run(client().prepareSearch("test").setQuery(nestedQuery("nested1", termQuery("nested1.n_field1", "n_value1_1"))).setSearchType(SearchType.DFS_QUERY_THEN_FETCH)); assertThat(Arrays.toString(searchResponse.getShardFailures()), searchResponse.getFailedShards(), equalTo(0)); assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); @@ -135,9 +125,8 @@ public class SimpleNestedTests extends AbstractSharedClusterTest { .endObject()).execute().actionGet(); // flush, so we fetch it from the index (as see that we filter nested docs) - client().admin().indices().prepareFlush().setRefresh(true).execute().actionGet(); - - statusResponse = client().admin().indices().prepareStatus().execute().actionGet(); + flush(); + statusResponse = run(client().admin().indices().prepareStatus()); assertThat(statusResponse.getIndex("test").getDocs().getNumDocs(), equalTo(6l)); searchResponse = client().prepareSearch("test").setQuery(nestedQuery("nested1", @@ -162,8 +151,7 @@ public class SimpleNestedTests extends AbstractSharedClusterTest { assertThat(deleteResponse.isNotFound(), equalTo(false)); // flush, so we fetch it from the index (as see that we filter nested docs) - client().admin().indices().prepareFlush().setRefresh(true).execute().actionGet(); - + flush(); statusResponse = client().admin().indices().prepareStatus().execute().actionGet(); assertThat(statusResponse.getIndex("test").getDocs().getNumDocs(), equalTo(3l)); @@ -188,7 +176,6 @@ public class SimpleNestedTests extends AbstractSharedClusterTest { } private void simpleNestedDeleteByQuery(int total, int docToDelete) throws Exception { - client().admin().indices().prepareDelete().execute().actionGet(); client().admin().indices().prepareCreate("test") .setSettings(settingsBuilder().put("index.number_of_shards", 1).put("index.referesh_interval", -1).build()) @@ -199,8 +186,7 @@ public class SimpleNestedTests extends AbstractSharedClusterTest { .endObject().endObject().endObject()) .execute().actionGet(); - client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet(); - + ensureGreen(); for (int i = 0; i < total; i++) { client().prepareIndex("test", "type1", Integer.toString(i)).setSource(jsonBuilder().startObject() @@ -219,7 +205,7 @@ public class SimpleNestedTests extends AbstractSharedClusterTest { } - client().admin().indices().prepareFlush().setRefresh(true).execute().actionGet(); + flush(); IndicesStatusResponse statusResponse = client().admin().indices().prepareStatus().execute().actionGet(); assertThat(statusResponse.getIndex("test").getDocs().getNumDocs(), equalTo(total * 3l)); diff --git a/src/test/java/org/elasticsearch/test/integration/search/highlight/HighlighterSearchTests.java b/src/test/java/org/elasticsearch/test/integration/search/highlight/HighlighterSearchTests.java index a192023f173..59ed01b3d8e 100644 --- a/src/test/java/org/elasticsearch/test/integration/search/highlight/HighlighterSearchTests.java +++ b/src/test/java/org/elasticsearch/test/integration/search/highlight/HighlighterSearchTests.java @@ -134,22 +134,11 @@ public class HighlighterSearchTests extends AbstractSharedClusterTest { * MultiPhraseQuery can literally kill an entire node if there are too many terms in the * query. We cut off and extract terms if there are more than 16 terms in the query */ - prepareCreate("test") - .addMapping("test", jsonBuilder() - .startObject() - .startObject("test") - .startObject("properties") - .startObject("body") - .field("type", "string") - .field("index_analyzer", "custom_analyzer") - .field("search_analyzer", "custom_analyzer") - .field("term_vector", "with_positions_offsets") - .endObject() - .endObject() - .endObject() - .endObject()) - .setSettings(ImmutableSettings.settingsBuilder() - .put("index.number_of_shards", 1) + run(addMapping(prepareCreate("test"), "test", + new Object[] { + "body", "type", "string", "index_analyzer", "custom_analyzer", "search_analyzer", "custom_analyzer", + "term_vector", "with_positions_offsets" }).setSettings( + ImmutableSettings.settingsBuilder() .put("index.number_of_shards", 1) .put("index.number_of_replicas", 0) .put("analysis.filter.wordDelimiter.type", "word_delimiter") .put("analysis.filter.wordDelimiter.type.split_on_numerics", false) @@ -159,8 +148,7 @@ public class HighlighterSearchTests extends AbstractSharedClusterTest { .put("analysis.filter.wordDelimiter.catenate_numbers", true) .put("analysis.filter.wordDelimiter.catenate_all", false) .put("analysis.analyzer.custom_analyzer.tokenizer", "whitespace") - .putArray("analysis.analyzer.custom_analyzer.filter", "lowercase", "wordDelimiter")) - .execute().actionGet(); + .putArray("analysis.analyzer.custom_analyzer.filter", "lowercase", "wordDelimiter"))); ensureGreen(); client().prepareIndex("test", "test", "1")