Search in sources :

Example 1 with MultiGetRequestBuilder

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();
}
Also used : MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) MultiGetRequestBuilder(org.elasticsearch.action.get.MultiGetRequestBuilder) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest)

Example 2 with MultiGetRequestBuilder

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());
        }
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) MultiGetItemResponse(org.elasticsearch.action.get.MultiGetItemResponse) Alias(org.elasticsearch.action.admin.indices.alias.Alias) MultiGetRequestBuilder(org.elasticsearch.action.get.MultiGetRequestBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) Map(java.util.Map)

Example 3 with MultiGetRequestBuilder

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;
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) MultiGetRequestBuilder(org.elasticsearch.action.get.MultiGetRequestBuilder) IndexRequest(org.elasticsearch.action.index.IndexRequest)

Example 4 with 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);
    }
}
Also used : MultiGetRequestBuilder(org.elasticsearch.action.get.MultiGetRequestBuilder) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 5 with MultiGetRequestBuilder

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();
}
Also used : MultiGetRequestBuilder(org.elasticsearch.action.get.MultiGetRequestBuilder) Test(org.junit.Test)

Aggregations

MultiGetRequestBuilder (org.elasticsearch.action.get.MultiGetRequestBuilder)13 MultiGetResponse (org.elasticsearch.action.get.MultiGetResponse)5 ByteSizeValue (org.elasticsearch.common.unit.ByteSizeValue)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 HashSet (java.util.HashSet)2 IndexRequest (org.elasticsearch.action.index.IndexRequest)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Alias (org.elasticsearch.action.admin.indices.alias.Alias)1 MultiGetItemResponse (org.elasticsearch.action.get.MultiGetItemResponse)1 MultiGetRequest (org.elasticsearch.action.get.MultiGetRequest)1 BytesArray (org.elasticsearch.common.bytes.BytesArray)1 BytesReference (org.elasticsearch.common.bytes.BytesReference)1 FetchSourceContext (org.elasticsearch.search.fetch.subphase.FetchSourceContext)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 F (play.libs.F)1