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)); } }
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())); } }
@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; }