public final ImmutableList<ModelEntryT> getModelsByIds(
     final ImmutableList<ModelIdT> ids,
     final Logger logger,
     final Marker logMarker,
     final ModelFactory<ModelEntryT> modelFactory)
     throws IoExceptionT {
   if (ids.isEmpty()) {
     logger.info(logMarker, "no model ids requested from index {}", indexName);
     return ImmutableList.of();
   }
   logger.debug(logMarker, "getting models {} from index {}", ids, indexName);
   try {
     final MultiGetRequestBuilder requestBuilder = client.prepareMultiGet();
     for (final ModelId id : ids) {
       requestBuilder.add(indexName, documentType, id.toString());
     }
     final MultiGetResponse responses = requestBuilder.execute().actionGet();
     final ImmutableList.Builder<ModelEntryT> modelsBuilder = ImmutableList.builder();
     for (final MultiGetItemResponse response : responses.getResponses()) {
       if (response.isFailed()) {
         continue;
       }
       BytesReference sourceAsBytesRef;
       try {
         sourceAsBytesRef = response.getResponse().getSourceAsBytesRef();
       } catch (final NullPointerException e) {
         logger.debug(
             logMarker,
             "model {} actually missing, but hipsters don't know that",
             response.getId());
         continue;
       }
       try {
         modelsBuilder.add(modelFactory.createModelEntry(response.getId(), sourceAsBytesRef));
       } catch (final InvalidModelException e) {
         logger.error(logMarker, "invalid model {} from index, ignoring: ", response.getId(), e);
         continue;
       }
     }
     return modelsBuilder.build();
   } catch (final IndexNotFoundException e) {
     logger.warn(logMarker, "tried to get models {} from missing index {}", ids, indexName);
     return ImmutableList.of();
   } catch (final ElasticsearchException e) {
     throw exceptionFactory.newIoException(
         e, String.format("error getting models %s from index %s", ids, indexName));
   }
 }
 protected Iterator<MultiGetItemResponse> getNodeItems(List<Way> ways) {
   MultiGetRequestBuilder request = client.prepareMultiGet();
   for (Way way : ways) {
     for (WayNode wayNode : way.getWayNodes()) {
       request.add(
           new Item(
                   indexName,
                   ESEntityType.NODE.getIndiceName(),
                   String.valueOf(wayNode.getNodeId()))
               .fields("shape"));
     }
   }
   MultiGetResponse responses = request.execute().actionGet();
   Iterator<MultiGetItemResponse> iterator = responses.iterator();
   return iterator;
 }
  public void testMultiGet() throws ExecutionException, InterruptedException {
    createIndexWithAlias();
    ensureYellow("test");

    int numDocs = iterations(10, 50);
    IndexRequestBuilder[] indexRequestBuilders = new IndexRequestBuilder[numDocs];
    for (int i = 0; i < numDocs; i++) {
      indexRequestBuilders[i] =
          client()
              .prepareIndex("test", "type", Integer.toString(i))
              .setSource("field", "value" + Integer.toString(i));
    }
    indexRandom(false, indexRequestBuilders);

    int iterations = iterations(1, numDocs);
    MultiGetRequestBuilder multiGetRequestBuilder = client().prepareMultiGet();
    for (int i = 0; i < iterations; i++) {
      multiGetRequestBuilder.add(
          new MultiGetRequest.Item(
              indexOrAlias(), "type", Integer.toString(randomInt(numDocs - 1))));
    }
    MultiGetResponse multiGetResponse = multiGetRequestBuilder.get();
    assertThat(multiGetResponse.getResponses().length, equalTo(iterations));
    for (int i = 0; i < multiGetResponse.getResponses().length; i++) {
      MultiGetItemResponse multiGetItemResponse = multiGetResponse.getResponses()[i];
      assertThat(multiGetItemResponse.isFailed(), equalTo(false));
      assertThat(multiGetItemResponse.getIndex(), equalTo("test"));
      assertThat(multiGetItemResponse.getType(), equalTo("type"));
      assertThat(
          multiGetItemResponse.getId(),
          equalTo(multiGetRequestBuilder.request().getItems().get(i).id()));
      assertThat(multiGetItemResponse.getResponse().isExists(), equalTo(true));
      assertThat(multiGetItemResponse.getResponse().getIndex(), equalTo("test"));
      assertThat(multiGetItemResponse.getResponse().getType(), equalTo("type"));
      assertThat(
          multiGetItemResponse.getResponse().getId(),
          equalTo(multiGetRequestBuilder.request().getItems().get(i).id()));
    }
  }
Exemple #4
0
  public List<DOMAIN> getByKeys(Collection<KEY> keys) {
    if (keys.isEmpty()) {
      return Collections.emptyList();
    }
    List<DOMAIN> results = new ArrayList<>();
    MultiGetRequestBuilder request = client.prepareMultiGet().setPreference("_local");
    for (KEY key : keys) {
      request.add(
          new MultiGetRequest.Item(getIndexName(), getIndexType(), getKeyValue(key))
              .routing(getKeyValue(key))
              .fetchSourceContext(FetchSourceContext.FETCH_SOURCE));
    }

    MultiGetResponse response = request.get();
    if (response.getResponses() != null) {
      for (MultiGetItemResponse item : response.getResponses()) {
        Map<String, Object> source = item.getResponse().getSource();
        if (source != null) {
          results.add(toDoc(source));
        }
      }
    }
    return results;
  }
  @Test
  public void simpleMultiGetTests() throws Exception {
    try {
      client.admin().indices().prepareDelete("test").execute().actionGet();
    } catch (Exception e) {
      // fine
    }
    client
        .admin()
        .indices()
        .prepareCreate("test")
        .setSettings(ImmutableSettings.settingsBuilder().put("index.refresh_interval", -1))
        .execute()
        .actionGet();

    ClusterHealthResponse clusterHealth =
        client.admin().cluster().health(clusterHealthRequest().waitForGreenStatus()).actionGet();
    assertThat(clusterHealth.isTimedOut(), equalTo(false));
    assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN));

    MultiGetResponse response =
        client.prepareMultiGet().add("test", "type1", "1").execute().actionGet();
    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)
          .execute()
          .actionGet();
    }

    response =
        client
            .prepareMultiGet()
            .add("test", "type1", "1")
            .add("test", "type1", "15")
            .add("test", "type1", "3")
            .add("test", "type1", "9")
            .add("test", "type1", "11")
            .execute()
            .actionGet();
    assertThat(response.getResponses().length, equalTo(5));
    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("15"));
    assertThat(response.getResponses()[1].getResponse().isExists(), equalTo(false));
    assertThat(response.getResponses()[2].getId(), equalTo("3"));
    assertThat(response.getResponses()[2].getResponse().isExists(), equalTo(true));
    assertThat(response.getResponses()[3].getId(), equalTo("9"));
    assertThat(response.getResponses()[3].getResponse().isExists(), equalTo(true));
    assertThat(response.getResponses()[4].getId(), equalTo("11"));
    assertThat(response.getResponses()[4].getResponse().isExists(), equalTo(false));

    // multi get with specific field
    response =
        client
            .prepareMultiGet()
            .add(new MultiGetRequest.Item("test", "type1", "1").fields("field"))
            .add(new MultiGetRequest.Item("test", "type1", "3").fields("field"))
            .execute()
            .actionGet();

    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"));
  }
  @Test
  public void testRequiredRoutingMapping_variousAPIs() 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")
        .setRefresh(true)
        .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");
    TermVectorResponse termVectorResponse =
        client().prepareTermVector(indexOrAlias(), "type1", "1").setRouting("0").get();
    assertThat(termVectorResponse.isExists(), equalTo(true));
    assertThat(termVectorResponse.getId(), equalTo("1"));

    try {
      client().prepareTermVector(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("field1", "value1")
            .get();
    assertThat(updateResponse.getId(), equalTo("1"));
    assertThat(updateResponse.getVersion(), equalTo(2l));

    try {
      client().prepareUpdate(indexOrAlias(), "type1", "1").setDoc("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 TermVectorRequest(indexOrAlias(), "type1", "1").routing("0"))
            .add(new TermVectorRequest(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 TermVectorRequest(indexOrAlias(), "type1", "1"))
            .add(new TermVectorRequest(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().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().getMessage(),
        equalTo("routing is required for [test]/[type1]/[2]"));
  }