public void shouldReturnObjectsForGivenIdsUsingMultiGetWithFields() {
// given
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
// first document
String documentId = randomNumeric(5);
SampleEntity sampleEntity1 = new SampleEntityBuilder(documentId)
.message("some message")
.type("type1")
.version(System.currentTimeMillis()).build();
// second document
String documentId2 = randomNumeric(5);
SampleEntity sampleEntity2 = new SampleEntityBuilder(documentId2)
.message("some message")
.type("type2")
.version(System.currentTimeMillis()).build();
indexQueries = getIndexQueries(Arrays.asList(sampleEntity1, sampleEntity2));
elasticsearchTemplate.bulkIndex(indexQueries);
elasticsearchTemplate.refresh(SampleEntity.class, true);
// when
SearchQuery query = new NativeSearchQueryBuilder()
.withIds(Arrays.asList(documentId, documentId2))
.withFields("message", "type")
.build();
LinkedList<SampleEntity> sampleEntities = elasticsearchTemplate.multiGet(query, SampleEntity.class, new MultiGetResultMapper() {
@Override
public <T> LinkedList<T> mapResults(MultiGetResponse responses, Class<T> clazz) {
LinkedList<T> list = new LinkedList<T>();
for (MultiGetItemResponse response : responses.getResponses()) {
SampleEntity entity = new SampleEntity();
entity.setId(response.getResponse().getId());
entity.setMessage((String) response.getResponse().getField("message").getValue());
entity.setType((String) response.getResponse().getField("type").getValue());
list.add((T) entity);
}
return list;
}
});