use of org.elasticsearch.action.get.GetResponse in project elasticsearch by elastic.
the class GetActionIT method testGetFieldsMetaData.
public void testGetFieldsMetaData() throws Exception {
assertAcked(prepareCreate("test").addMapping("parent").addMapping("my-type1", "_parent", "type=parent", "field1", "type=keyword,store=true").addAlias(new Alias("alias")).setSettings(Settings.builder().put("index.refresh_interval", -1)));
client().prepareIndex("test", "my-type1", "1").setRouting("1").setParent("parent_1").setSource(jsonBuilder().startObject().field("field1", "value").endObject()).get();
GetResponse getResponse = client().prepareGet(indexOrAlias(), "my-type1", "1").setRouting("1").setStoredFields("field1").get();
assertThat(getResponse.isExists(), equalTo(true));
assertThat(getResponse.getField("field1").isMetadataField(), equalTo(false));
assertThat(getResponse.getField("field1").getValue().toString(), equalTo("value"));
assertThat(getResponse.getField("_routing").isMetadataField(), equalTo(true));
assertThat(getResponse.getField("_routing").getValue().toString(), equalTo("1"));
assertThat(getResponse.getField("_parent").isMetadataField(), equalTo(true));
assertThat(getResponse.getField("_parent").getValue().toString(), equalTo("parent_1"));
flush();
getResponse = client().prepareGet(indexOrAlias(), "my-type1", "1").setStoredFields("field1").setRouting("1").get();
assertThat(getResponse.isExists(), equalTo(true));
assertThat(getResponse.getField("field1").isMetadataField(), equalTo(false));
assertThat(getResponse.getField("field1").getValue().toString(), equalTo("value"));
assertThat(getResponse.getField("_routing").isMetadataField(), equalTo(true));
assertThat(getResponse.getField("_routing").getValue().toString(), equalTo("1"));
assertThat(getResponse.getField("_parent").isMetadataField(), equalTo(true));
assertThat(getResponse.getField("_parent").getValue().toString(), equalTo("parent_1"));
}
use of org.elasticsearch.action.get.GetResponse in project elasticsearch by elastic.
the class GetActionIT method testGetFieldsComplexField.
public void testGetFieldsComplexField() throws Exception {
assertAcked(prepareCreate("my-index").setSettings(Settings.builder().put("index.refresh_interval", -1)).addMapping("my-type2", jsonBuilder().startObject().startObject("my-type2").startObject("properties").startObject("field1").field("type", "object").startObject("properties").startObject("field2").field("type", "object").startObject("properties").startObject("field3").field("type", "object").startObject("properties").startObject("field4").field("type", "text").field("store", true).endObject().endObject().endObject().endObject().endObject().endObject().endObject().endObject().endObject().endObject()));
BytesReference source = jsonBuilder().startObject().startArray("field1").startObject().startObject("field2").startArray("field3").startObject().field("field4", "value1").endObject().endArray().endObject().endObject().startObject().startObject("field2").startArray("field3").startObject().field("field4", "value2").endObject().endArray().endObject().endObject().endArray().endObject().bytes();
logger.info("indexing documents");
client().prepareIndex("my-index", "my-type1", "1").setSource(source, XContentType.JSON).get();
client().prepareIndex("my-index", "my-type2", "1").setSource(source, XContentType.JSON).get();
logger.info("checking real time retrieval");
String field = "field1.field2.field3.field4";
GetResponse getResponse = client().prepareGet("my-index", "my-type1", "1").setStoredFields(field).get();
assertThat(getResponse.isExists(), equalTo(true));
assertThat(getResponse.getField(field).isMetadataField(), equalTo(false));
assertThat(getResponse.getField(field).getValues().size(), equalTo(2));
assertThat(getResponse.getField(field).getValues().get(0).toString(), equalTo("value1"));
assertThat(getResponse.getField(field).getValues().get(1).toString(), equalTo("value2"));
getResponse = client().prepareGet("my-index", "my-type2", "1").setStoredFields(field).get();
assertThat(getResponse.isExists(), equalTo(true));
assertThat(getResponse.getField(field).isMetadataField(), equalTo(false));
assertThat(getResponse.getField(field).getValues().size(), equalTo(2));
assertThat(getResponse.getField(field).getValues().get(0).toString(), equalTo("value1"));
assertThat(getResponse.getField(field).getValues().get(1).toString(), equalTo("value2"));
logger.info("waiting for recoveries to complete");
// Flush fails if shard has ongoing recoveries, make sure the cluster is settled down
ensureGreen();
logger.info("flushing");
FlushResponse flushResponse = client().admin().indices().prepareFlush("my-index").setForce(true).get();
if (flushResponse.getSuccessfulShards() == 0) {
StringBuilder sb = new StringBuilder("failed to flush at least one shard. total shards [").append(flushResponse.getTotalShards()).append("], failed shards: [").append(flushResponse.getFailedShards()).append("]");
for (ShardOperationFailedException failure : flushResponse.getShardFailures()) {
sb.append("\nShard failure: ").append(failure);
}
fail(sb.toString());
}
logger.info("checking post-flush retrieval");
getResponse = client().prepareGet("my-index", "my-type1", "1").setStoredFields(field).get();
assertThat(getResponse.isExists(), equalTo(true));
assertThat(getResponse.getField(field).isMetadataField(), equalTo(false));
assertThat(getResponse.getField(field).getValues().size(), equalTo(2));
assertThat(getResponse.getField(field).getValues().get(0).toString(), equalTo("value1"));
assertThat(getResponse.getField(field).getValues().get(1).toString(), equalTo("value2"));
getResponse = client().prepareGet("my-index", "my-type2", "1").setStoredFields(field).get();
assertThat(getResponse.isExists(), equalTo(true));
assertThat(getResponse.getField(field).isMetadataField(), equalTo(false));
assertThat(getResponse.getField(field).getValues().size(), equalTo(2));
assertThat(getResponse.getField(field).getValues().get(0).toString(), equalTo("value1"));
assertThat(getResponse.getField(field).getValues().get(1).toString(), equalTo("value2"));
}
use of org.elasticsearch.action.get.GetResponse in project elasticsearch by elastic.
the class GetActionIT method testGetDocWithMultivaluedFields.
public void testGetDocWithMultivaluedFields() throws Exception {
String mapping1 = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("field").field("type", "text").field("store", true).endObject().endObject().endObject().endObject().string();
String mapping2 = XContentFactory.jsonBuilder().startObject().startObject("type2").startObject("properties").startObject("field").field("type", "text").field("store", true).endObject().endObject().endObject().endObject().string();
assertAcked(prepareCreate("test").addMapping("type1", mapping1, XContentType.JSON).addMapping("type2", mapping2, XContentType.JSON).setSettings(Settings.builder().put("index.refresh_interval", -1)));
ensureGreen();
GetResponse response = client().prepareGet("test", "type1", "1").get();
assertThat(response.isExists(), equalTo(false));
response = client().prepareGet("test", "type2", "1").get();
assertThat(response.isExists(), equalTo(false));
client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject().array("field", "1", "2").endObject()).get();
client().prepareIndex("test", "type2", "1").setSource(jsonBuilder().startObject().array("field", "1", "2").endObject()).get();
response = client().prepareGet("test", "type1", "1").setStoredFields("field").get();
assertThat(response.isExists(), equalTo(true));
assertThat(response.getId(), equalTo("1"));
assertThat(response.getType(), equalTo("type1"));
Set<String> fields = new HashSet<>(response.getFields().keySet());
assertThat(fields, equalTo(singleton("field")));
assertThat(response.getFields().get("field").getValues().size(), equalTo(2));
assertThat(response.getFields().get("field").getValues().get(0).toString(), equalTo("1"));
assertThat(response.getFields().get("field").getValues().get(1).toString(), equalTo("2"));
response = client().prepareGet("test", "type2", "1").setStoredFields("field").get();
assertThat(response.isExists(), equalTo(true));
assertThat(response.getType(), equalTo("type2"));
assertThat(response.getId(), equalTo("1"));
fields = new HashSet<>(response.getFields().keySet());
assertThat(fields, equalTo(singleton("field")));
assertThat(response.getFields().get("field").getValues().size(), equalTo(2));
assertThat(response.getFields().get("field").getValues().get(0).toString(), equalTo("1"));
assertThat(response.getFields().get("field").getValues().get(1).toString(), equalTo("2"));
// Now test values being fetched from stored fields.
refresh();
response = client().prepareGet("test", "type1", "1").setStoredFields("field").get();
assertThat(response.isExists(), equalTo(true));
assertThat(response.getId(), equalTo("1"));
fields = new HashSet<>(response.getFields().keySet());
assertThat(fields, equalTo(singleton("field")));
assertThat(response.getFields().get("field").getValues().size(), equalTo(2));
assertThat(response.getFields().get("field").getValues().get(0).toString(), equalTo("1"));
assertThat(response.getFields().get("field").getValues().get(1).toString(), equalTo("2"));
response = client().prepareGet("test", "type2", "1").setStoredFields("field").get();
assertThat(response.isExists(), equalTo(true));
assertThat(response.getId(), equalTo("1"));
fields = new HashSet<>(response.getFields().keySet());
assertThat(fields, equalTo(singleton("field")));
assertThat(response.getFields().get("field").getValues().size(), equalTo(2));
assertThat(response.getFields().get("field").getValues().get(0).toString(), equalTo("1"));
assertThat(response.getFields().get("field").getValues().get(1).toString(), equalTo("2"));
}
use of org.elasticsearch.action.get.GetResponse in project elasticsearch by elastic.
the class RecoveryWhileUnderLoadIT method iterateAssertCount.
private void iterateAssertCount(final int numberOfShards, final int iterations, final Set<String> ids) throws Exception {
final long numberOfDocs = ids.size();
SearchResponse[] iterationResults = new SearchResponse[iterations];
boolean error = false;
for (int i = 0; i < iterations; i++) {
SearchResponse searchResponse = client().prepareSearch().setSize((int) numberOfDocs).setQuery(matchAllQuery()).addSort("id", SortOrder.ASC).get();
logSearchResponse(numberOfShards, numberOfDocs, i, searchResponse);
iterationResults[i] = searchResponse;
if (searchResponse.getHits().getTotalHits() != numberOfDocs) {
error = true;
}
}
if (error) {
//Printing out shards and their doc count
IndicesStatsResponse indicesStatsResponse = client().admin().indices().prepareStats().get();
for (ShardStats shardStats : indicesStatsResponse.getShards()) {
DocsStats docsStats = shardStats.getStats().docs;
logger.info("shard [{}] - count {}, primary {}", shardStats.getShardRouting().id(), docsStats.getCount(), shardStats.getShardRouting().primary());
}
ClusterService clusterService = clusterService();
final ClusterState state = clusterService.state();
for (int shard = 0; shard < numberOfShards; shard++) {
for (String id : ids) {
ShardId docShard = clusterService.operationRouting().shardId(state, "test", id, null);
if (docShard.id() == shard) {
for (ShardRouting shardRouting : state.routingTable().shardRoutingTable("test", shard)) {
GetResponse response = client().prepareGet("test", "type", id).setPreference("_only_nodes:" + shardRouting.currentNodeId()).get();
if (response.isExists()) {
logger.info("missing id [{}] on shard {}", id, shardRouting);
}
}
}
}
}
//if there was an error we try to wait and see if at some point it'll get fixed
logger.info("--> trying to wait");
assertTrue(awaitBusy(() -> {
boolean errorOccurred = false;
for (int i = 0; i < iterations; i++) {
SearchResponse searchResponse = client().prepareSearch().setSize(0).setQuery(matchAllQuery()).get();
if (searchResponse.getHits().getTotalHits() != numberOfDocs) {
errorOccurred = true;
}
}
return !errorOccurred;
}, 5, TimeUnit.MINUTES));
assertEquals(numberOfDocs, ids.size());
}
//lets now make the test fail if it was supposed to fail
for (int i = 0; i < iterations; i++) {
assertHitCount(iterationResults[i], numberOfDocs);
}
}
use of org.elasticsearch.action.get.GetResponse in project elasticsearch by elastic.
the class SimpleRecoveryIT method testSimpleRecovery.
public void testSimpleRecovery() throws Exception {
assertAcked(prepareCreate("test", 1).execute().actionGet());
NumShards numShards = getNumShards("test");
client().index(indexRequest("test").type("type1").id("1").source(source("1", "test"), XContentType.JSON)).actionGet();
FlushResponse flushResponse = client().admin().indices().flush(flushRequest("test")).actionGet();
assertThat(flushResponse.getTotalShards(), equalTo(numShards.totalNumShards));
assertThat(flushResponse.getSuccessfulShards(), equalTo(numShards.numPrimaries));
assertThat(flushResponse.getFailedShards(), equalTo(0));
client().index(indexRequest("test").type("type1").id("2").source(source("2", "test"), XContentType.JSON)).actionGet();
RefreshResponse refreshResponse = client().admin().indices().refresh(refreshRequest("test")).actionGet();
assertThat(refreshResponse.getTotalShards(), equalTo(numShards.totalNumShards));
assertThat(refreshResponse.getSuccessfulShards(), equalTo(numShards.numPrimaries));
assertThat(refreshResponse.getFailedShards(), equalTo(0));
allowNodes("test", 2);
logger.info("Running Cluster Health");
ensureGreen();
GetResponse getResult;
for (int i = 0; i < 5; i++) {
getResult = client().get(getRequest("test").type("type1").id("1").operationThreaded(false)).actionGet();
assertThat(getResult.getSourceAsString(), equalTo(source("1", "test")));
getResult = client().get(getRequest("test").type("type1").id("1").operationThreaded(false)).actionGet();
assertThat(getResult.getSourceAsString(), equalTo(source("1", "test")));
getResult = client().get(getRequest("test").type("type1").id("2").operationThreaded(true)).actionGet();
assertThat(getResult.getSourceAsString(), equalTo(source("2", "test")));
getResult = client().get(getRequest("test").type("type1").id("2").operationThreaded(true)).actionGet();
assertThat(getResult.getSourceAsString(), equalTo(source("2", "test")));
}
// now start another one so we move some primaries
allowNodes("test", 3);
Thread.sleep(200);
logger.info("Running Cluster Health");
ensureGreen();
for (int i = 0; i < 5; i++) {
getResult = client().get(getRequest("test").type("type1").id("1")).actionGet();
assertThat(getResult.getSourceAsString(), equalTo(source("1", "test")));
getResult = client().get(getRequest("test").type("type1").id("1")).actionGet();
assertThat(getResult.getSourceAsString(), equalTo(source("1", "test")));
getResult = client().get(getRequest("test").type("type1").id("1")).actionGet();
assertThat(getResult.getSourceAsString(), equalTo(source("1", "test")));
getResult = client().get(getRequest("test").type("type1").id("2").operationThreaded(true)).actionGet();
assertThat(getResult.getSourceAsString(), equalTo(source("2", "test")));
getResult = client().get(getRequest("test").type("type1").id("2").operationThreaded(true)).actionGet();
assertThat(getResult.getSourceAsString(), equalTo(source("2", "test")));
getResult = client().get(getRequest("test").type("type1").id("2").operationThreaded(true)).actionGet();
assertThat(getResult.getSourceAsString(), equalTo(source("2", "test")));
}
}
Aggregations