Added shortcuts for several common commands

added simple way to add more complex mappings as well as shortcuts for flush
and status etc. all checking if requests return failed shards
This commit is contained in:
Simon Willnauer 2013-06-07 12:30:30 +02:00
parent 8016d32a0e
commit 962e3d58f7
3 changed files with 65 additions and 47 deletions

View File

@ -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;
@ -189,6 +195,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));
builder.put("index.routing.allocation.exclude._name", exclude);
@ -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);
@ -319,4 +348,19 @@ public abstract class AbstractSharedClusterTest extends ElasticsearchTestCase {
return actionGet.isExists();
}
protected AdminClient admin() {
return client().admin();
}
protected <Res extends ActionResponse> Res run(ActionRequestBuilder<?,Res,?> builder) {
Res actionGet = builder.execute().actionGet();
return actionGet;
}
protected <Res extends BroadcastOperationResponse> Res run(BroadcastOperationRequestBuilder<?,Res,?> builder) {
Res actionGet = builder.execute().actionGet();
assertNoFailures(actionGet);
return actionGet;
}
}

View File

@ -54,18 +54,8 @@ 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();
@ -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));

View File

@ -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")