Beispiel #1
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;
  }
 @Override
 public Map<String, Table> loadAll(Collection<String> keys) {
   logger.info("Load all called for multiple keys");
   MultiGetResponse response =
       elasticsearchConnection
           .getClient()
           .prepareMultiGet()
           .add(TABLE_META_INDEX, TABLE_META_TYPE, keys)
           .execute()
           .actionGet();
   Map<String, Table> tables = Maps.newHashMap();
   for (MultiGetItemResponse multiGetItemResponse : response) {
     try {
       Table table =
           objectMapper.readValue(
               multiGetItemResponse.getResponse().getSourceAsString(), Table.class);
       tables.put(table.getName(), table);
     } catch (Exception e) {
       throw new RuntimeException("Error getting data for table: " + multiGetItemResponse.getId());
     }
   }
   logger.info("Loaded value count: " + tables.size());
   return tables;
 }
 @SuppressWarnings("unchecked")
 protected <T extends ESEntity> T buildEntityFromGetResponse(
     Class<T> entityClass, MultiGetItemResponse item) {
   GetResponse response = item.getResponse();
   if (!response.isExists())
     throw new DaoException(
         String.format(
             "Entity %s does not exist in %s/%s",
             response.getId(), response.getIndex(), response.getType()));
   if (entityClass == null) throw new IllegalArgumentException("Provided Entity class is null");
   else if (entityClass.equals(ESNode.class))
     return (T) ESNode.Builder.buildFromGetReponse(response);
   else if (entityClass.equals(ESWay.class))
     return (T) ESWay.Builder.buildFromGetReponse(response);
   else throw new IllegalArgumentException(entityClass.getSimpleName() + " is not a known Entity");
 }
  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()));
    }
  }
 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));
   }
 }