Search in sources :

Example 1 with MultiGetResponse

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

Example 2 with MultiGetResponse

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]"));
}
Also used : MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) Alias(org.elasticsearch.action.admin.indices.alias.Alias)

Example 3 with MultiGetResponse

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"));
}
Also used : MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) Alias(org.elasticsearch.action.admin.indices.alias.Alias) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 4 with MultiGetResponse

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());
        }
    }
}
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 5 with MultiGetResponse

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());
}
Also used : Item(org.elasticsearch.action.get.MultiGetRequest.Item) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) MultiGetItemResponse(org.elasticsearch.action.get.MultiGetItemResponse) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

MultiGetResponse (org.elasticsearch.action.get.MultiGetResponse)29 MultiGetItemResponse (org.elasticsearch.action.get.MultiGetItemResponse)14 MultiGetRequestBuilder (org.elasticsearch.action.get.MultiGetRequestBuilder)13 ElasticSearchClient (org.apache.skywalking.apm.collector.client.elasticsearch.ElasticSearchClient)8 DurationPoint (org.apache.skywalking.apm.collector.storage.utils.DurationPoint)8 MultiGetRequest (org.elasticsearch.action.get.MultiGetRequest)8 Alias (org.elasticsearch.action.admin.indices.alias.Alias)7 ArrayList (java.util.ArrayList)5 LinkedList (java.util.LinkedList)4 HashMap (java.util.HashMap)3 JsonRawRestResponse (alien4cloud.rest.model.JsonRawRestResponse)2 ApiOperation (io.swagger.annotations.ApiOperation)2 List (java.util.List)2 Map (java.util.Map)2 ElasticsearchException (org.elasticsearch.ElasticsearchException)2 GetResponse (org.elasticsearch.action.get.GetResponse)2 Item (org.elasticsearch.action.get.MultiGetRequest.Item)2 SearchResponse (org.elasticsearch.action.search.SearchResponse)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2