Search in sources :

Example 16 with MultiGetResponse

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

the class IndexService method multiGet.

/**
 * Get a response for a simple request
 * @param indexName
 * @param indexType
 * @param ids
 * @return
 */
public static MultiGetResponse multiGet(String indexName, String indexType, Collection<String> ids) {
    MultiGetRequestBuilder multiGetRequestBuilder = IndexClient.client.prepareMultiGet();
    for (String id : ids) {
        multiGetRequestBuilder.add(indexName, indexType, ids);
    }
    MultiGetResponse multiGetResponse = multiGetRequestBuilder.execute().actionGet();
    return multiGetResponse;
}
Also used : MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) MultiGetRequestBuilder(org.elasticsearch.action.get.MultiGetRequestBuilder)

Example 17 with MultiGetResponse

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

the class SimpleRoutingIT method testRequiredRoutingMappingVariousAPIs.

public void testRequiredRoutingMappingVariousAPIs() throws Exception {
    client().admin().indices().prepareCreate("test").addAlias(new Alias("alias")).addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("_routing").field("required", true).endObject().endObject().endObject()).execute().actionGet();
    ensureGreen();
    logger.info("--> indexing with id [1], and routing [0]");
    client().prepareIndex(indexOrAlias(), "type1", "1").setRouting("0").setSource("field", "value1").get();
    logger.info("--> indexing with id [2], and routing [0]");
    client().prepareIndex(indexOrAlias(), "type1", "2").setRouting("0").setSource("field", "value2").setRefreshPolicy(RefreshPolicy.IMMEDIATE).get();
    logger.info("--> verifying get with id [1] with routing [0], should succeed");
    assertThat(client().prepareGet(indexOrAlias(), "type1", "1").setRouting("0").execute().actionGet().isExists(), equalTo(true));
    logger.info("--> verifying get with id [1], with no routing, should fail");
    try {
        client().prepareGet(indexOrAlias(), "type1", "1").get();
        fail();
    } catch (RoutingMissingException e) {
        assertThat(e.getMessage(), equalTo("routing is required for [test]/[type1]/[1]"));
    }
    logger.info("--> verifying explain with id [2], with routing [0], should succeed");
    ExplainResponse explainResponse = client().prepareExplain(indexOrAlias(), "type1", "2").setQuery(QueryBuilders.matchAllQuery()).setRouting("0").get();
    assertThat(explainResponse.isExists(), equalTo(true));
    assertThat(explainResponse.isMatch(), equalTo(true));
    logger.info("--> verifying explain with id [2], with no routing, should fail");
    try {
        client().prepareExplain(indexOrAlias(), "type1", "2").setQuery(QueryBuilders.matchAllQuery()).get();
        fail();
    } catch (RoutingMissingException e) {
        assertThat(e.getMessage(), equalTo("routing is required for [test]/[type1]/[2]"));
    }
    logger.info("--> verifying term vector with id [1], with routing [0], should succeed");
    TermVectorsResponse termVectorsResponse = client().prepareTermVectors(indexOrAlias(), "type1", "1").setRouting("0").get();
    assertThat(termVectorsResponse.isExists(), equalTo(true));
    assertThat(termVectorsResponse.getId(), equalTo("1"));
    try {
        client().prepareTermVectors(indexOrAlias(), "type1", "1").get();
        fail();
    } catch (RoutingMissingException e) {
        assertThat(e.getMessage(), equalTo("routing is required for [test]/[type1]/[1]"));
    }
    UpdateResponse updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1").setRouting("0").setDoc(Requests.INDEX_CONTENT_TYPE, "field1", "value1").get();
    assertThat(updateResponse.getId(), equalTo("1"));
    assertThat(updateResponse.getVersion(), equalTo(2L));
    try {
        client().prepareUpdate(indexOrAlias(), "type1", "1").setDoc(Requests.INDEX_CONTENT_TYPE, "field1", "value1").get();
        fail();
    } catch (RoutingMissingException e) {
        assertThat(e.getMessage(), equalTo("routing is required for [test]/[type1]/[1]"));
    }
    logger.info("--> verifying mget with ids [1,2], with routing [0], should succeed");
    MultiGetResponse multiGetResponse = client().prepareMultiGet().add(new MultiGetRequest.Item(indexOrAlias(), "type1", "1").routing("0")).add(new MultiGetRequest.Item(indexOrAlias(), "type1", "2").routing("0")).get();
    assertThat(multiGetResponse.getResponses().length, equalTo(2));
    assertThat(multiGetResponse.getResponses()[0].isFailed(), equalTo(false));
    assertThat(multiGetResponse.getResponses()[0].getResponse().getId(), equalTo("1"));
    assertThat(multiGetResponse.getResponses()[1].isFailed(), equalTo(false));
    assertThat(multiGetResponse.getResponses()[1].getResponse().getId(), equalTo("2"));
    logger.info("--> verifying mget with ids [1,2], with no routing, should fail");
    multiGetResponse = client().prepareMultiGet().add(new MultiGetRequest.Item(indexOrAlias(), "type1", "1")).add(new MultiGetRequest.Item(indexOrAlias(), "type1", "2")).get();
    assertThat(multiGetResponse.getResponses().length, equalTo(2));
    assertThat(multiGetResponse.getResponses()[0].isFailed(), equalTo(true));
    assertThat(multiGetResponse.getResponses()[0].getFailure().getId(), equalTo("1"));
    assertThat(multiGetResponse.getResponses()[0].getFailure().getMessage(), equalTo("routing is required for [test]/[type1]/[1]"));
    assertThat(multiGetResponse.getResponses()[1].isFailed(), equalTo(true));
    assertThat(multiGetResponse.getResponses()[1].getFailure().getId(), equalTo("2"));
    assertThat(multiGetResponse.getResponses()[1].getFailure().getMessage(), equalTo("routing is required for [test]/[type1]/[2]"));
    MultiTermVectorsResponse multiTermVectorsResponse = client().prepareMultiTermVectors().add(new TermVectorsRequest(indexOrAlias(), "type1", "1").routing("0")).add(new TermVectorsRequest(indexOrAlias(), "type1", "2").routing("0")).get();
    assertThat(multiTermVectorsResponse.getResponses().length, equalTo(2));
    assertThat(multiTermVectorsResponse.getResponses()[0].getId(), equalTo("1"));
    assertThat(multiTermVectorsResponse.getResponses()[0].isFailed(), equalTo(false));
    assertThat(multiTermVectorsResponse.getResponses()[0].getResponse().getId(), equalTo("1"));
    assertThat(multiTermVectorsResponse.getResponses()[0].getResponse().isExists(), equalTo(true));
    assertThat(multiTermVectorsResponse.getResponses()[1].getId(), equalTo("2"));
    assertThat(multiTermVectorsResponse.getResponses()[1].isFailed(), equalTo(false));
    assertThat(multiTermVectorsResponse.getResponses()[1].getResponse().getId(), equalTo("2"));
    assertThat(multiTermVectorsResponse.getResponses()[1].getResponse().isExists(), equalTo(true));
    multiTermVectorsResponse = client().prepareMultiTermVectors().add(new TermVectorsRequest(indexOrAlias(), "type1", "1")).add(new TermVectorsRequest(indexOrAlias(), "type1", "2")).get();
    assertThat(multiTermVectorsResponse.getResponses().length, equalTo(2));
    assertThat(multiTermVectorsResponse.getResponses()[0].getId(), equalTo("1"));
    assertThat(multiTermVectorsResponse.getResponses()[0].isFailed(), equalTo(true));
    assertThat(multiTermVectorsResponse.getResponses()[0].getFailure().getCause().getMessage(), equalTo("routing is required for [test]/[type1]/[1]"));
    assertThat(multiTermVectorsResponse.getResponses()[0].getResponse(), nullValue());
    assertThat(multiTermVectorsResponse.getResponses()[1].getId(), equalTo("2"));
    assertThat(multiTermVectorsResponse.getResponses()[1].isFailed(), equalTo(true));
    assertThat(multiTermVectorsResponse.getResponses()[1].getResponse(), nullValue());
    assertThat(multiTermVectorsResponse.getResponses()[1].getFailure().getCause().getMessage(), equalTo("routing is required for [test]/[type1]/[2]"));
}
Also used : TermVectorsRequest(org.elasticsearch.action.termvectors.TermVectorsRequest) MultiTermVectorsResponse(org.elasticsearch.action.termvectors.MultiTermVectorsResponse) TermVectorsResponse(org.elasticsearch.action.termvectors.TermVectorsResponse) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) Alias(org.elasticsearch.action.admin.indices.alias.Alias) ExplainResponse(org.elasticsearch.action.explain.ExplainResponse) RoutingMissingException(org.elasticsearch.action.RoutingMissingException) MultiTermVectorsResponse(org.elasticsearch.action.termvectors.MultiTermVectorsResponse) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest)

Example 18 with MultiGetResponse

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

the class GetActionIT method testSimpleMultiGet.

public void testSimpleMultiGet() throws Exception {
    assertAcked(prepareCreate("test").addAlias(new Alias("alias")).addMapping("type1", "field", "type=keyword,store=true").setSettings(Settings.builder().put("index.refresh_interval", -1)));
    ensureGreen();
    MultiGetResponse response = client().prepareMultiGet().add(indexOrAlias(), "type1", "1").get();
    assertThat(response.getResponses().length, equalTo(1));
    assertThat(response.getResponses()[0].getResponse().isExists(), equalTo(false));
    for (int i = 0; i < 10; i++) {
        client().prepareIndex("test", "type1", Integer.toString(i)).setSource("field", "value" + i).get();
    }
    response = client().prepareMultiGet().add(indexOrAlias(), "type1", "1").add(indexOrAlias(), "type1", "15").add(indexOrAlias(), "type1", "3").add(indexOrAlias(), "type1", "9").add(indexOrAlias(), "type1", "11").get();
    assertThat(response.getResponses().length, equalTo(5));
    assertThat(response.getResponses()[0].getId(), equalTo("1"));
    assertThat(response.getResponses()[0].getIndex(), equalTo("test"));
    assertThat(response.getResponses()[0].getResponse().getIndex(), equalTo("test"));
    assertThat(response.getResponses()[0].getResponse().isExists(), equalTo(true));
    assertThat(response.getResponses()[0].getResponse().getSourceAsMap().get("field").toString(), equalTo("value1"));
    assertThat(response.getResponses()[1].getId(), equalTo("15"));
    assertThat(response.getResponses()[1].getIndex(), equalTo("test"));
    assertThat(response.getResponses()[1].getResponse().getIndex(), equalTo("test"));
    assertThat(response.getResponses()[1].getResponse().isExists(), equalTo(false));
    assertThat(response.getResponses()[2].getId(), equalTo("3"));
    assertThat(response.getResponses()[2].getIndex(), equalTo("test"));
    assertThat(response.getResponses()[2].getResponse().isExists(), equalTo(true));
    assertThat(response.getResponses()[3].getId(), equalTo("9"));
    assertThat(response.getResponses()[3].getIndex(), equalTo("test"));
    assertThat(response.getResponses()[3].getResponse().getIndex(), equalTo("test"));
    assertThat(response.getResponses()[3].getResponse().isExists(), equalTo(true));
    assertThat(response.getResponses()[4].getId(), equalTo("11"));
    assertThat(response.getResponses()[4].getIndex(), equalTo("test"));
    assertThat(response.getResponses()[4].getResponse().getIndex(), equalTo("test"));
    assertThat(response.getResponses()[4].getResponse().isExists(), equalTo(false));
    // multi get with specific field
    response = client().prepareMultiGet().add(new MultiGetRequest.Item(indexOrAlias(), "type1", "1").storedFields("field")).add(new MultiGetRequest.Item(indexOrAlias(), "type1", "3").storedFields("field")).get();
    assertThat(response.getResponses().length, equalTo(2));
    assertThat(response.getResponses()[0].getResponse().getSourceAsBytes(), nullValue());
    assertThat(response.getResponses()[0].getResponse().getField("field").getValues().get(0).toString(), equalTo("value1"));
}
Also used : MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) Alias(org.elasticsearch.action.admin.indices.alias.Alias) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest)

Example 19 with MultiGetResponse

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

the class GetActionIT method testMultiGetWithVersion.

public void testMultiGetWithVersion() throws Exception {
    assertAcked(prepareCreate("test").addAlias(new Alias("alias")).setSettings(Settings.builder().put("index.refresh_interval", -1)));
    ensureGreen();
    MultiGetResponse response = client().prepareMultiGet().add(indexOrAlias(), "type1", "1").get();
    assertThat(response.getResponses().length, equalTo(1));
    assertThat(response.getResponses()[0].getResponse().isExists(), equalTo(false));
    for (int i = 0; i < 3; i++) {
        client().prepareIndex("test", "type1", Integer.toString(i)).setSource("field", "value" + i).get();
    }
    // Version from translog
    response = client().prepareMultiGet().add(new MultiGetRequest.Item(indexOrAlias(), "type1", "1").version(Versions.MATCH_ANY)).add(new MultiGetRequest.Item(indexOrAlias(), "type1", "1").version(1)).add(new MultiGetRequest.Item(indexOrAlias(), "type1", "1").version(2)).get();
    assertThat(response.getResponses().length, equalTo(3));
    // [0] version doesn't matter, which is the default
    assertThat(response.getResponses()[0].getFailure(), nullValue());
    assertThat(response.getResponses()[0].getId(), equalTo("1"));
    assertThat(response.getResponses()[0].getIndex(), equalTo("test"));
    assertThat(response.getResponses()[0].getResponse().isExists(), equalTo(true));
    assertThat(response.getResponses()[0].getResponse().getSourceAsMap().get("field").toString(), equalTo("value1"));
    assertThat(response.getResponses()[1].getId(), equalTo("1"));
    assertThat(response.getResponses()[1].getIndex(), equalTo("test"));
    assertThat(response.getResponses()[1].getFailure(), nullValue());
    assertThat(response.getResponses()[1].getResponse().isExists(), equalTo(true));
    assertThat(response.getResponses()[1].getResponse().getSourceAsMap().get("field").toString(), equalTo("value1"));
    assertThat(response.getResponses()[2].getFailure(), notNullValue());
    assertThat(response.getResponses()[2].getFailure().getId(), equalTo("1"));
    assertThat(response.getResponses()[2].getFailure().getMessage(), startsWith("[type1][1]: version conflict"));
    assertThat(response.getResponses()[2].getFailure().getFailure(), instanceOf(VersionConflictEngineException.class));
    //Version from Lucene index
    refresh();
    response = client().prepareMultiGet().add(new MultiGetRequest.Item(indexOrAlias(), "type1", "1").version(Versions.MATCH_ANY)).add(new MultiGetRequest.Item(indexOrAlias(), "type1", "1").version(1)).add(new MultiGetRequest.Item(indexOrAlias(), "type1", "1").version(2)).setRealtime(false).get();
    assertThat(response.getResponses().length, equalTo(3));
    // [0] version doesn't matter, which is the default
    assertThat(response.getResponses()[0].getFailure(), nullValue());
    assertThat(response.getResponses()[0].getId(), equalTo("1"));
    assertThat(response.getResponses()[0].getResponse().isExists(), equalTo(true));
    assertThat(response.getResponses()[0].getResponse().getSourceAsMap().get("field").toString(), equalTo("value1"));
    assertThat(response.getResponses()[1].getId(), equalTo("1"));
    assertThat(response.getResponses()[1].getFailure(), nullValue());
    assertThat(response.getResponses()[1].getResponse().isExists(), equalTo(true));
    assertThat(response.getResponses()[1].getResponse().getSourceAsMap().get("field").toString(), equalTo("value1"));
    assertThat(response.getResponses()[2].getFailure(), notNullValue());
    assertThat(response.getResponses()[2].getFailure().getId(), equalTo("1"));
    assertThat(response.getResponses()[2].getFailure().getMessage(), startsWith("[type1][1]: version conflict"));
    assertThat(response.getResponses()[2].getFailure().getFailure(), instanceOf(VersionConflictEngineException.class));
    for (int i = 0; i < 3; i++) {
        client().prepareIndex("test", "type1", Integer.toString(i)).setSource("field", "value" + i).get();
    }
    // Version from translog
    response = client().prepareMultiGet().add(new MultiGetRequest.Item(indexOrAlias(), "type1", "2").version(Versions.MATCH_ANY)).add(new MultiGetRequest.Item(indexOrAlias(), "type1", "2").version(1)).add(new MultiGetRequest.Item(indexOrAlias(), "type1", "2").version(2)).get();
    assertThat(response.getResponses().length, equalTo(3));
    // [0] version doesn't matter, which is the default
    assertThat(response.getResponses()[0].getFailure(), nullValue());
    assertThat(response.getResponses()[0].getId(), equalTo("2"));
    assertThat(response.getResponses()[0].getIndex(), equalTo("test"));
    assertThat(response.getResponses()[0].getResponse().isExists(), equalTo(true));
    assertThat(response.getResponses()[0].getResponse().getSourceAsMap().get("field").toString(), equalTo("value2"));
    assertThat(response.getResponses()[1].getFailure(), notNullValue());
    assertThat(response.getResponses()[1].getFailure().getId(), equalTo("2"));
    assertThat(response.getResponses()[1].getIndex(), equalTo("test"));
    assertThat(response.getResponses()[1].getFailure().getMessage(), startsWith("[type1][2]: version conflict"));
    assertThat(response.getResponses()[2].getId(), equalTo("2"));
    assertThat(response.getResponses()[2].getIndex(), equalTo("test"));
    assertThat(response.getResponses()[2].getFailure(), nullValue());
    assertThat(response.getResponses()[2].getResponse().isExists(), equalTo(true));
    assertThat(response.getResponses()[2].getResponse().getSourceAsMap().get("field").toString(), equalTo("value2"));
    //Version from Lucene index
    refresh();
    response = client().prepareMultiGet().add(new MultiGetRequest.Item(indexOrAlias(), "type1", "2").version(Versions.MATCH_ANY)).add(new MultiGetRequest.Item(indexOrAlias(), "type1", "2").version(1)).add(new MultiGetRequest.Item(indexOrAlias(), "type1", "2").version(2)).setRealtime(false).get();
    assertThat(response.getResponses().length, equalTo(3));
    // [0] version doesn't matter, which is the default
    assertThat(response.getResponses()[0].getFailure(), nullValue());
    assertThat(response.getResponses()[0].getId(), equalTo("2"));
    assertThat(response.getResponses()[0].getIndex(), equalTo("test"));
    assertThat(response.getResponses()[0].getResponse().isExists(), equalTo(true));
    assertThat(response.getResponses()[0].getResponse().getSourceAsMap().get("field").toString(), equalTo("value2"));
    assertThat(response.getResponses()[1].getFailure(), notNullValue());
    assertThat(response.getResponses()[1].getFailure().getId(), equalTo("2"));
    assertThat(response.getResponses()[1].getIndex(), equalTo("test"));
    assertThat(response.getResponses()[1].getFailure().getMessage(), startsWith("[type1][2]: version conflict"));
    assertThat(response.getResponses()[2].getId(), equalTo("2"));
    assertThat(response.getResponses()[2].getIndex(), equalTo("test"));
    assertThat(response.getResponses()[2].getFailure(), nullValue());
    assertThat(response.getResponses()[2].getResponse().isExists(), equalTo(true));
    assertThat(response.getResponses()[2].getResponse().getSourceAsMap().get("field").toString(), equalTo("value2"));
}
Also used : MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) Alias(org.elasticsearch.action.admin.indices.alias.Alias) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest)

Example 20 with MultiGetResponse

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

the class GetActionIT method assertGetFieldException.

private void assertGetFieldException(String index, String type, String docId, String field) {
    try {
        client().prepareGet().setIndex(index).setType(type).setId(docId).setStoredFields(field);
        fail();
    } catch (ElasticsearchException e) {
        assertTrue(e.getMessage().contains("You can only get this field after refresh() has been called."));
    }
    MultiGetResponse multiGetResponse = client().prepareMultiGet().add(new MultiGetRequest.Item(index, type, docId).storedFields(field)).get();
    assertNull(multiGetResponse.getResponses()[0].getResponse());
    assertTrue(multiGetResponse.getResponses()[0].getFailure().getMessage().contains("You can only get this field after refresh() has been called."));
}
Also used : MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) ElasticsearchException(org.elasticsearch.ElasticsearchException) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest)

Aggregations

MultiGetResponse (org.elasticsearch.action.get.MultiGetResponse)30 MultiGetItemResponse (org.elasticsearch.action.get.MultiGetItemResponse)15 MultiGetRequestBuilder (org.elasticsearch.action.get.MultiGetRequestBuilder)13 MultiGetRequest (org.elasticsearch.action.get.MultiGetRequest)9 ElasticSearchClient (org.apache.skywalking.apm.collector.client.elasticsearch.ElasticSearchClient)8 DurationPoint (org.apache.skywalking.apm.collector.storage.utils.DurationPoint)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 GetResponse (org.elasticsearch.action.get.GetResponse)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 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