use of org.elasticsearch.action.get.MultiGetRequestBuilder 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.MultiGetRequestBuilder 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.MultiGetRequestBuilder in project elasticsearch by elastic.
the class BulkProcessorIT method indexDocs.
private static MultiGetRequestBuilder indexDocs(Client client, BulkProcessor processor, int numDocs) throws Exception {
MultiGetRequestBuilder multiGetRequestBuilder = client.prepareMultiGet();
for (int i = 1; i <= numDocs; i++) {
if (randomBoolean()) {
processor.add(new IndexRequest("test", "test", Integer.toString(i)).source(Requests.INDEX_CONTENT_TYPE, "field", randomRealisticUnicodeOfLengthBetween(1, 30)));
} else {
final String source = "{ \"index\":{\"_index\":\"test\",\"_type\":\"test\",\"_id\":\"" + Integer.toString(i) + "\"} }\n" + JsonXContent.contentBuilder().startObject().field("field", randomRealisticUnicodeOfLengthBetween(1, 30)).endObject().string() + "\n";
processor.add(new BytesArray(source), null, null, XContentType.JSON);
}
multiGetRequestBuilder.add("test", "test", Integer.toString(i));
}
return multiGetRequestBuilder;
}
use of org.elasticsearch.action.get.MultiGetRequestBuilder in project elasticsearch by elastic.
the class BulkProcessorIT method testBulkProcessorFlush.
public void testBulkProcessorFlush() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
BulkProcessorTestListener listener = new BulkProcessorTestListener(latch);
int numDocs = randomIntBetween(10, 100);
try (BulkProcessor processor = BulkProcessor.builder(client(), listener).setName("foo").setConcurrentRequests(randomIntBetween(0, 10)).setBulkActions(numDocs + randomIntBetween(1, 100)).setFlushInterval(TimeValue.timeValueHours(24)).setBulkSize(new ByteSizeValue(1, ByteSizeUnit.GB)).build()) {
MultiGetRequestBuilder multiGetRequestBuilder = indexDocs(client(), processor, numDocs);
assertThat(latch.await(randomInt(500), TimeUnit.MILLISECONDS), equalTo(false));
//we really need an explicit flush as none of the bulk thresholds was reached
processor.flush();
latch.await();
assertThat(listener.beforeCounts.get(), equalTo(1));
assertThat(listener.afterCounts.get(), equalTo(1));
assertThat(listener.bulkFailures.size(), equalTo(0));
assertResponseItems(listener.bulkItems, numDocs);
assertMultiGetResponse(multiGetRequestBuilder.get(), numDocs);
}
}
use of org.elasticsearch.action.get.MultiGetRequestBuilder in project sonarqube by SonarSource.
the class ProxyMultiGetRequestBuilderTest method multi_get.
@Test
public void multi_get() {
MultiGetRequestBuilder request = esTester.client().prepareMultiGet();
request.add(new MultiGetRequest.Item(FakeIndexDefinition.INDEX, FakeIndexDefinition.TYPE, "ruleKey").fetchSourceContext(FetchSourceContext.FETCH_SOURCE));
request.get();
}
Aggregations