Search in sources :

Example 1 with MultiGetItemResponse

use of org.elasticsearch.action.get.MultiGetItemResponse 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)

Example 2 with MultiGetItemResponse

use of org.elasticsearch.action.get.MultiGetItemResponse 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 MultiGetItemResponse

use of org.elasticsearch.action.get.MultiGetItemResponse in project elasticsearch by elastic.

the class BulkProcessorIT method assertMultiGetResponse.

private static void assertMultiGetResponse(MultiGetResponse multiGetResponse, int numDocs) {
    assertThat(multiGetResponse.getResponses().length, equalTo(numDocs));
    int i = 1;
    for (MultiGetItemResponse multiGetItemResponse : multiGetResponse) {
        assertThat(multiGetItemResponse.getIndex(), equalTo("test"));
        assertThat(multiGetItemResponse.getType(), equalTo("test"));
        assertThat(multiGetItemResponse.getId(), equalTo(Integer.toString(i++)));
    }
}
Also used : MultiGetItemResponse(org.elasticsearch.action.get.MultiGetItemResponse)

Example 4 with MultiGetItemResponse

use of org.elasticsearch.action.get.MultiGetItemResponse in project play2-elasticsearch by cleverage.

the class IndexService method getTsFromMultiGetResponse.

private static <T extends Index> List<T> getTsFromMultiGetResponse(Class<T> clazz, MultiGetResponse multiGetResponse) {
    T t = IndexUtils.getInstanceIndex(clazz);
    MultiGetItemResponse[] multiGetItemResponses = multiGetResponse.getResponses();
    if (multiGetItemResponses.length == 0) {
        return null;
    }
    List<T> ts = new ArrayList<>(multiGetItemResponses.length);
    for (MultiGetItemResponse multiGetItemResponse : multiGetItemResponses) {
        Map<String, Object> map = multiGetItemResponse.getResponse().getSourceAsMap();
        t = (T) t.fromIndex(map);
        t.id = multiGetItemResponse.getResponse().getId();
        ts.add(t);
    }
    return ts;
}
Also used : MultiGetItemResponse(org.elasticsearch.action.get.MultiGetItemResponse) ArrayList(java.util.ArrayList)

Example 5 with MultiGetItemResponse

use of org.elasticsearch.action.get.MultiGetItemResponse 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, ElasticsearchConstants.OPERATION_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, ElasticsearchConstants.OPERATION_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, ElasticsearchConstants.OPERATION_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

MultiGetItemResponse (org.elasticsearch.action.get.MultiGetItemResponse)5 ArrayList (java.util.ArrayList)3 MultiGetResponse (org.elasticsearch.action.get.MultiGetResponse)3 HashMap (java.util.HashMap)2 Item (org.elasticsearch.action.get.MultiGetRequest.Item)2 Test (org.junit.Test)2 Map (java.util.Map)1 Alias (org.elasticsearch.action.admin.indices.alias.Alias)1 MultiGetRequestBuilder (org.elasticsearch.action.get.MultiGetRequestBuilder)1 BytesReference (org.elasticsearch.common.bytes.BytesReference)1 FetchSourceContext (org.elasticsearch.search.fetch.subphase.FetchSourceContext)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1