Skip to content

Commit 38bbb0f

Browse files
committed
DATAES-52 - Support Get & Multi Get
Added support for MultiGetResultMapper for custom fields
1 parent c2a5371 commit 38bbb0f

File tree

7 files changed

+138
-12
lines changed

7 files changed

+138
-12
lines changed

src/main/java/org/springframework/data/elasticsearch/core/DefaultResultMapper.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@
2222
import java.nio.charset.Charset;
2323
import java.util.ArrayList;
2424
import java.util.Collection;
25+
import java.util.LinkedList;
2526
import java.util.List;
2627

2728
import org.elasticsearch.action.get.GetResponse;
29+
import org.elasticsearch.action.get.MultiGetItemResponse;
30+
import org.elasticsearch.action.get.MultiGetResponse;
2831
import org.elasticsearch.action.search.SearchResponse;
2932
import org.elasticsearch.common.base.Strings;
3033
import org.elasticsearch.common.jackson.core.JsonEncoding;
@@ -129,6 +132,18 @@ public <T> T mapResult(GetResponse response, Class<T> clazz) {
129132
return result;
130133
}
131134

135+
@Override
136+
public <T> LinkedList<T> mapResults(MultiGetResponse responses, Class<T> clazz) {
137+
LinkedList<T> list = new LinkedList<T>();
138+
for (MultiGetItemResponse response : responses.getResponses()) {
139+
if (!response.isFailed() && response.getResponse().isExists()) {
140+
T result = mapEntity(response.getResponse().getSourceAsString(), clazz);
141+
list.add(result);
142+
}
143+
}
144+
return list;
145+
}
146+
132147
private <T> void setPersistentEntityId(T result, String id, Class<T> clazz) {
133148
if (mappingContext != null && clazz.isAnnotationPresent(Document.class)) {
134149
PersistentProperty<ElasticsearchPersistentProperty> idProperty = mappingContext.getPersistentEntity(clazz).getIdProperty();

src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchOperations.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,24 @@ public interface ElasticsearchOperations {
183183
<T> long count(SearchQuery query, Class<T> clazz);
184184

185185
/**
186-
* Execute a multiget against elasticsearch for the given ids
186+
* Execute a multiGet against elasticsearch for the given ids
187187
*
188188
* @param searchQuery
189189
* @param clazz
190190
* @return
191191
*/
192192
<T> LinkedList<T> multiGet(SearchQuery searchQuery, Class<T> clazz);
193193

194+
/**
195+
* Execute a multiGet against elasticsearch for the given ids with MultiGetResultMapper
196+
*
197+
* @param searchQuery
198+
* @param clazz
199+
* @param multiGetResultMapper
200+
* @return
201+
*/
202+
<T> LinkedList<T> multiGet(SearchQuery searchQuery, Class<T> clazz, MultiGetResultMapper multiGetResultMapper);
203+
194204
/**
195205
* Index an object. Will do save or update
196206
*

src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@
3737
import org.elasticsearch.action.bulk.BulkRequestBuilder;
3838
import org.elasticsearch.action.bulk.BulkResponse;
3939
import org.elasticsearch.action.count.CountRequestBuilder;
40-
import org.elasticsearch.action.get.*;
40+
import org.elasticsearch.action.get.GetResponse;
41+
import org.elasticsearch.action.get.MultiGetRequest;
42+
import org.elasticsearch.action.get.MultiGetRequestBuilder;
43+
import org.elasticsearch.action.get.MultiGetResponse;
4144
import org.elasticsearch.action.index.IndexRequestBuilder;
4245
import org.elasticsearch.action.mlt.MoreLikeThisRequestBuilder;
4346
import org.elasticsearch.action.search.SearchRequestBuilder;
@@ -248,6 +251,10 @@ public <T> long count(SearchQuery query, Class<T> clazz) {
248251

249252
@Override
250253
public <T> LinkedList<T> multiGet(SearchQuery searchQuery, Class<T> clazz) {
254+
return resultsMapper.mapResults(getMultiResponse(searchQuery, clazz), clazz);
255+
}
256+
257+
private <T> MultiGetResponse getMultiResponse(SearchQuery searchQuery, Class<T> clazz) {
251258

252259
ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz);
253260

@@ -266,14 +273,12 @@ public <T> LinkedList<T> multiGet(SearchQuery searchQuery, Class<T> clazz) {
266273
}
267274
builder.add(item);
268275
}
269-
MultiGetResponse responses = builder.execute().actionGet();
270-
final LinkedList<T> result = new LinkedList<T>();
271-
for (MultiGetItemResponse response : responses.getResponses()) {
272-
if (!response.isFailed() && response.getResponse().isExists()) {
273-
result.add(resultsMapper.mapResult(response.getResponse(), clazz));
274-
}
275-
}
276-
return result;
276+
return builder.execute().actionGet();
277+
}
278+
279+
@Override
280+
public <T> LinkedList<T> multiGet(SearchQuery searchQuery, Class<T> clazz, MultiGetResultMapper getResultMapper) {
281+
return getResultMapper.mapResults(getMultiResponse(searchQuery, clazz), clazz);
277282
}
278283

279284
@Override
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.data.elasticsearch.core;
18+
19+
import java.util.LinkedList;
20+
21+
import org.elasticsearch.action.get.MultiGetResponse;
22+
23+
/**
24+
* @author Mohsin Husen
25+
*/
26+
public interface MultiGetResultMapper {
27+
28+
<T> LinkedList<T> mapResults(MultiGetResponse responses, Class<T> clazz);
29+
}

src/main/java/org/springframework/data/elasticsearch/core/ResultsMapper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,7 +23,7 @@
2323
* @author Artur Konczak
2424
*/
2525

26-
public interface ResultsMapper extends SearchResultMapper, GetResultMapper {
26+
public interface ResultsMapper extends SearchResultMapper, GetResultMapper, MultiGetResultMapper {
2727

2828
EntityMapper getEntityMapper();
2929
}

src/test/java/org/springframework/data/elasticsearch/core/CustomResultMapper.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core;
1717

18+
import java.util.LinkedList;
19+
1820
import org.elasticsearch.action.get.GetResponse;
21+
import org.elasticsearch.action.get.MultiGetResponse;
1922
import org.elasticsearch.action.search.SearchResponse;
2023
import org.springframework.data.domain.Pageable;
2124

@@ -46,4 +49,9 @@ public <T> T mapResult(GetResponse response, Class<T> clazz) {
4649
public <T> FacetedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
4750
return null; //To change body of implemented methods use File | Settings | File Templates.
4851
}
52+
53+
@Override
54+
public <T> LinkedList<T> mapResults(MultiGetResponse responses, Class<T> clazz) {
55+
return null;
56+
}
4957
}

src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
import java.util.*;
2525

26+
import org.elasticsearch.action.get.MultiGetItemResponse;
27+
import org.elasticsearch.action.get.MultiGetResponse;
2628
import org.elasticsearch.action.index.IndexRequest;
2729
import org.elasticsearch.action.search.SearchResponse;
2830
import org.elasticsearch.index.engine.DocumentMissingException;
@@ -153,6 +155,63 @@ public void shouldReturnObjectsForGivenIdsUsingMultiGet() {
153155
assertEquals(sampleEntities.get(1), sampleEntity2);
154156
}
155157

158+
@Test
159+
public void shouldReturnObjectsForGivenIdsUsingMultiGetWithFields() {
160+
// given
161+
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
162+
// first document
163+
String documentId = randomNumeric(5);
164+
SampleEntity sampleEntity1 = new SampleEntity();
165+
sampleEntity1.setId(documentId);
166+
sampleEntity1.setMessage("some message");
167+
sampleEntity1.setType("type1");
168+
sampleEntity1.setVersion(System.currentTimeMillis());
169+
170+
IndexQuery indexQuery1 = new IndexQuery();
171+
indexQuery1.setId(documentId);
172+
indexQuery1.setObject(sampleEntity1);
173+
indexQueries.add(indexQuery1);
174+
175+
// second document
176+
String documentId2 = randomNumeric(5);
177+
SampleEntity sampleEntity2 = new SampleEntity();
178+
sampleEntity2.setId(documentId2);
179+
sampleEntity2.setMessage("some message");
180+
sampleEntity2.setType("type2");
181+
sampleEntity2.setVersion(System.currentTimeMillis());
182+
183+
IndexQuery indexQuery2 = new IndexQuery();
184+
indexQuery2.setId(documentId2);
185+
indexQuery2.setObject(sampleEntity2);
186+
187+
indexQueries.add(indexQuery2);
188+
189+
elasticsearchTemplate.bulkIndex(indexQueries);
190+
elasticsearchTemplate.refresh(SampleEntity.class, true);
191+
192+
// when
193+
SearchQuery query = new NativeSearchQueryBuilder()
194+
.withIds(Arrays.asList(documentId, documentId2))
195+
.withFields("message", "type")
196+
.build();
197+
LinkedList<SampleEntity> sampleEntities = elasticsearchTemplate.multiGet(query, SampleEntity.class, new MultiGetResultMapper() {
198+
@Override
199+
public <T> LinkedList<T> mapResults(MultiGetResponse responses, Class<T> clazz) {
200+
LinkedList<T> list = new LinkedList<T>();
201+
for (MultiGetItemResponse response : responses.getResponses()) {
202+
SampleEntity entity = new SampleEntity();
203+
entity.setId(response.getResponse().getId());
204+
entity.setMessage((String) response.getResponse().getField("message").getValue());
205+
entity.setType((String) response.getResponse().getField("type").getValue());
206+
list.add((T) entity);
207+
}
208+
return list;
209+
}
210+
});
211+
// then
212+
assertThat(sampleEntities.size(), is(equalTo(2)));
213+
}
214+
156215
@Test
157216
public void shouldReturnPageForGivenSearchQuery() {
158217
// given

0 commit comments

Comments
 (0)