Skip to content

Commit 3fb28de

Browse files
committed
DATAES-34 spring-projects#17 - documentation for custom result mapper
1 parent b31a9d1 commit 3fb28de

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,67 @@ Indexing multiple Document(bulk index) using Repository
166166
repository.save(sampleEntities);
167167
```
168168

169+
### Adding custom behaviors to ResultMapper - CustomResultMapper
170+
171+
Define new class implementing ResultMapper
172+
173+
```java
174+
public class CustomResultMapper implements ResultsMapper{
175+
176+
private EntityMapper entityMapper;
177+
178+
public CustomResultMapper(EntityMapper entityMapper) {
179+
this.entityMapper = entityMapper;
180+
}
181+
182+
@Override
183+
public EntityMapper getEntityMapper() {
184+
return entityMapper;
185+
}
186+
187+
@Override
188+
public <T> T mapResult(GetResponse response, Class<T> clazz) {
189+
return null; //Your implementation
190+
}
191+
192+
@Override
193+
public <T> FacetedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
194+
return null; //Your implementation
195+
}
196+
}
197+
198+
```
199+
200+
Inject your custom implementation to template
201+
202+
203+
```xml
204+
<?xml version="1.0" encoding="UTF-8"?>
205+
<beans xmlns="http://www.springframework.org/schema/beans"
206+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
207+
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
208+
xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd
209+
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
210+
211+
<elasticsearch:repositories base-package="com.xyz.acme"/>
212+
213+
<elasticsearch:transport-client id="client" cluster-nodes="localhost:9300,someip:9300" />
214+
215+
<bean name="elasticsearchTemplate"
216+
class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
217+
<constructor-arg name="client" ref="client"/>
218+
<constructor-arg name="resultsMapper" ref="resultMapper"/>
219+
</bean>
220+
221+
<bean name="entityMapper" class="org.springframework.data.elasticsearch.core.DefaultEntityMapper"/>
222+
223+
<bean name="resultMapper" class="org.springframework.data.elasticsearch.core.CustomResultMapper">
224+
<constructor-arg ref="entityMapper"/>
225+
</bean>
226+
227+
</beans>
228+
```
229+
169230
### Geo indexing and request
170231

171232
You can make request using geo_distance filter. This can be done using GeoPoint object.

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
import org.springframework.data.domain.Pageable;
66

77
/**
8-
* User: dead
9-
* Date: 11/11/13
10-
* Time: 17:37
8+
* @author Artur Konczak
119
*/
1210
public class CustomResultMapper implements ResultsMapper{
1311

0 commit comments

Comments
 (0)