private <T> MultiGetResponse getMultiResponse(Query searchQuery, Class<T> clazz) { String indexName = isNotEmpty(searchQuery.getIndices()) ? searchQuery.getIndices().get(0) : getPersistentEntityFor(clazz).getIndexName(); String type = isNotEmpty(searchQuery.getTypes()) ? searchQuery.getTypes().get(0) : getPersistentEntityFor(clazz).getIndexType(); Assert.notNull(indexName, "No index defined for Query"); Assert.notNull(type, "No type define for Query"); Assert.notEmpty(searchQuery.getIds(), "No Id define for Query"); MultiGetRequestBuilder builder = client.prepareMultiGet(); for (String id : searchQuery.getIds()) { MultiGetRequest.Item item = new MultiGetRequest.Item(indexName, type, id); if (searchQuery.getRoute() != null) { item = item.routing(searchQuery.getRoute()); } if (searchQuery.getFields() != null && !searchQuery.getFields().isEmpty()) { item = item.fields(toArray(searchQuery.getFields())); } builder.add(item); } return builder.execute().actionGet(); }
protected <T extends ESEntity> MultiGetRequestBuilder buildMultiGetRequest( Class<T> entityClass, long... osmIds) { ESEntityType type = ESEntityType.valueOf(entityClass); MultiGetRequestBuilder request = client.prepareMultiGet(); for (long osmId : osmIds) { request.add( new Item(indexName, type.getIndiceName(), String.valueOf(osmId)) .fields("centroid", "lengthKm", "areaKm2", "shape", "tags")); } return request; }
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; }
protected <T extends ESEntity> List<T> executeMultiGetRequest( Class<T> entityClass, MultiGetRequestBuilder request) { MultiGetResponse responses = request.execute().actionGet(); List<T> entities = new ArrayList<T>(); for (MultiGetItemResponse item : responses) { entities.add(buildEntityFromGetResponse(entityClass, item)); } return Collections.unmodifiableList(entities); }
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 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; }