use of org.elasticsearch.action.get.MultiGetResponse in project elasticsearch by elastic.
the class GetActionIT method multiGetDocument.
private GetResponse multiGetDocument(String index, String type, String docId, String field, @Nullable String routing) {
MultiGetRequest.Item getItem = new MultiGetRequest.Item(index, type, docId).storedFields(field);
if (routing != null) {
getItem.routing(routing);
}
MultiGetRequestBuilder multiGetRequestBuilder = client().prepareMultiGet().add(getItem);
MultiGetResponse multiGetResponse = multiGetRequestBuilder.get();
assertThat(multiGetResponse.getResponses().length, equalTo(1));
return multiGetResponse.getResponses()[0].getResponse();
}
use of org.elasticsearch.action.get.MultiGetResponse in project elasticsearch by elastic.
the class SimpleMgetIT method testThatParentPerDocumentIsSupported.
public void testThatParentPerDocumentIsSupported() throws Exception {
assertAcked(prepareCreate("test").addAlias(new Alias("alias")).addMapping("test", jsonBuilder().startObject().startObject("test").startObject("_parent").field("type", "foo").endObject().endObject().endObject()));
client().prepareIndex("test", "test", "1").setParent("4").setRefreshPolicy(IMMEDIATE).setSource(jsonBuilder().startObject().field("foo", "bar").endObject()).get();
MultiGetResponse mgetResponse = client().prepareMultiGet().add(new MultiGetRequest.Item(indexOrAlias(), "test", "1").parent("4")).add(new MultiGetRequest.Item(indexOrAlias(), "test", "1")).get();
assertThat(mgetResponse.getResponses().length, is(2));
assertThat(mgetResponse.getResponses()[0].isFailed(), is(false));
assertThat(mgetResponse.getResponses()[0].getResponse().isExists(), is(true));
assertThat(mgetResponse.getResponses()[1].isFailed(), is(true));
assertThat(mgetResponse.getResponses()[1].getResponse(), nullValue());
assertThat(mgetResponse.getResponses()[1].getFailure().getMessage(), equalTo("routing is required for [test]/[test]/[1]"));
}
use of org.elasticsearch.action.get.MultiGetResponse in project elasticsearch by elastic.
the class SimpleMgetIT method testThatRoutingPerDocumentIsSupported.
public void testThatRoutingPerDocumentIsSupported() throws Exception {
assertAcked(prepareCreate("test").addAlias(new Alias("alias")).setSettings(Settings.builder().put(indexSettings()).put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, between(2, DEFAULT_MAX_NUM_SHARDS))));
final String id = routingKeyForShard("test", 0);
final String routingOtherShard = routingKeyForShard("test", 1);
client().prepareIndex("test", "test", id).setRefreshPolicy(IMMEDIATE).setRouting(routingOtherShard).setSource(jsonBuilder().startObject().field("foo", "bar").endObject()).get();
MultiGetResponse mgetResponse = client().prepareMultiGet().add(new MultiGetRequest.Item(indexOrAlias(), "test", id).routing(routingOtherShard)).add(new MultiGetRequest.Item(indexOrAlias(), "test", id)).get();
assertThat(mgetResponse.getResponses().length, is(2));
assertThat(mgetResponse.getResponses()[0].isFailed(), is(false));
assertThat(mgetResponse.getResponses()[0].getResponse().isExists(), is(true));
assertThat(mgetResponse.getResponses()[0].getResponse().getIndex(), is("test"));
assertThat(mgetResponse.getResponses()[1].isFailed(), is(false));
assertThat(mgetResponse.getResponses()[1].getResponse().isExists(), is(false));
assertThat(mgetResponse.getResponses()[1].getResponse().getIndex(), is("test"));
}
use of org.elasticsearch.action.get.MultiGetResponse in project elasticsearch by elastic.
the class SimpleMgetIT method testThatSourceFilteringIsSupported.
@SuppressWarnings("unchecked")
public void testThatSourceFilteringIsSupported() throws Exception {
assertAcked(prepareCreate("test").addAlias(new Alias("alias")));
BytesReference sourceBytesRef = jsonBuilder().startObject().array("field", "1", "2").startObject("included").field("field", "should be seen").field("hidden_field", "should not be seen").endObject().field("excluded", "should not be seen").endObject().bytes();
for (int i = 0; i < 100; i++) {
client().prepareIndex("test", "type", Integer.toString(i)).setSource(sourceBytesRef, XContentType.JSON).get();
}
MultiGetRequestBuilder request = client().prepareMultiGet();
for (int i = 0; i < 100; i++) {
if (i % 2 == 0) {
request.add(new MultiGetRequest.Item(indexOrAlias(), "type", Integer.toString(i)).fetchSourceContext(new FetchSourceContext(true, new String[] { "included" }, new String[] { "*.hidden_field" })));
} else {
request.add(new MultiGetRequest.Item(indexOrAlias(), "type", Integer.toString(i)).fetchSourceContext(new FetchSourceContext(false)));
}
}
MultiGetResponse response = request.get();
assertThat(response.getResponses().length, equalTo(100));
for (int i = 0; i < 100; i++) {
MultiGetItemResponse responseItem = response.getResponses()[i];
assertThat(responseItem.getIndex(), equalTo("test"));
if (i % 2 == 0) {
Map<String, Object> source = responseItem.getResponse().getSourceAsMap();
assertThat(source.size(), equalTo(1));
assertThat(source, hasKey("included"));
assertThat(((Map<String, Object>) source.get("included")).size(), equalTo(1));
assertThat(((Map<String, Object>) source.get("included")), hasKey("field"));
} else {
assertThat(responseItem.getResponse().getSourceAsBytes(), nullValue());
}
}
}
use of org.elasticsearch.action.get.MultiGetResponse in project camel by apache.
the class ElasticsearchGetSearchDeleteExistsUpdateTest method testMultiGet.
@Test
public void testMultiGet() throws Exception {
//first, INDEX two values
Map<String, String> map = createIndexedData();
Map<String, Object> headers = new HashMap<String, Object>();
headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchOperation.INDEX);
headers.put(ElasticsearchConstants.PARAM_INDEX_NAME, "twitter");
headers.put(ElasticsearchConstants.PARAM_INDEX_TYPE, "tweet");
headers.put(ElasticsearchConstants.PARAM_INDEX_ID, "1");
template.requestBodyAndHeaders("direct:start", map, headers, String.class);
headers.clear();
headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchOperation.INDEX);
headers.put(ElasticsearchConstants.PARAM_INDEX_NAME, "facebook");
headers.put(ElasticsearchConstants.PARAM_INDEX_TYPE, "status");
headers.put(ElasticsearchConstants.PARAM_INDEX_ID, "2");
template.requestBodyAndHeaders("direct:start", map, headers, String.class);
headers.clear();
//now, verify MULTIGET
headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchOperation.MULTIGET);
Item item1 = new Item("twitter", "tweet", "1");
Item item2 = new Item("facebook", "status", "2");
Item item3 = new Item("instagram", "latest", "3");
List<Item> list = new ArrayList<Item>();
list.add(item1);
list.add(item2);
list.add(item3);
MultiGetResponse response = template.requestBodyAndHeaders("direct:start", list, headers, MultiGetResponse.class);
MultiGetItemResponse[] responses = response.getResponses();
assertNotNull("response should not be null", response);
assertEquals("response should contains three multiGetResponse object", 3, response.getResponses().length);
assertEquals("response 1 should contains tweet as type", "tweet", responses[0].getResponse().getType().toString());
assertEquals("response 2 should contains status as type", "status", responses[1].getResponse().getType().toString());
assertFalse("response 1 should be ok", responses[0].isFailed());
assertFalse("response 2 should be ok", responses[1].isFailed());
assertTrue("response 3 should be failed", responses[2].isFailed());
}
Aggregations