@@ -15,24 +15,47 @@ Guide
15
15
* [ API Documentation] ( http://docs.spring.io/spring-data/elasticsearch/docs/1.0.0.M1/api/ )
16
16
* [ Spring Data Project] ( http://www.springsource.org/spring-data )
17
17
* [ Sample Test Application] ( https://github.com/BioMedCentralLtd/spring-data-elasticsearch-sample-application )
18
+ * for more detailed questions, use the [ forum] ( http://forum.springsource.org/forumdisplay.php?f=80 ) .
18
19
19
20
20
21
Quick Start
21
22
-----------
22
23
Wiki page for [ Getting Started] (https://github.com/spring-projects/spring-data-elasticsearch/wiki/How-to-start-with-spring-data-elasticsearch )
23
24
24
- ### Dependency
25
- ``` java
25
+ ### Maven configuration
26
+
27
+ Add the Maven dependency:
28
+
29
+ ``` xml
26
30
<dependency >
27
31
<groupId >org.springframework.data</groupId >
28
32
<artifactId >spring-data-elasticsearch</artifactId >
29
33
<version >1.0.0.M1</version >
30
- < / dependency>
34
+ </dependency >
35
+
36
+ <repository >
37
+ <id >spring-libs-milestone</id >
38
+ <name >Spring Milestone Repository</name >
39
+ <url >http://repo.springsource.org/libs-milestone</url >
40
+ </repository >
31
41
```
32
42
33
- ### ElasticsearchTemplate
34
- ElasticsearchTemplate is the central support class for elasticsearch operations .
43
+ If you'd rather like the latest snapshots of the upcoming major version, use our Maven snapshot repository and declare
44
+ the appropriate dependency version .
35
45
46
+ ``` xml
47
+ <dependency >
48
+ <groupId >org.springframework.data</groupId >
49
+ <artifactId >spring-data-elasticsearch</artifactId >
50
+ <version >1.0.0.BUILD-SNAPSHOT</version >
51
+ </dependency >
52
+
53
+ <repository >
54
+ <id >spring-libs-snapshot</id >
55
+ <name >Spring Snapshot Repository</name >
56
+ <url >http://repo.springsource.org/libs-snapshot</url >
57
+ </repository >
58
+ ```
36
59
37
60
### ElasticsearchRepository
38
61
A default implementation of ElasticsearchRepository, aligning to the generic Repository Interfaces, is provided. Spring can do the Repository implementation for you depending on method names in the interface definition.
@@ -49,87 +72,22 @@ Extending ElasticsearchRepository for custom methods
49
72
``` java
50
73
public interface BookRepository extends Repository<Book , String > {
51
74
52
- // Equivalent Json Query will be "{ "bool" : { "must" :[{ "field" : {"name" : "?"} },{ "field" : {"price" : "?"} }]} }"
53
75
List<Book > findByNameAndPrice (String name , Integer price );
54
76
55
- // Equivalent Json Query will be "{"bool" : {"should" : [ {"field" : "name" : "?"}}, {"field" : {"price" : "?"}} ]}}"
56
77
List<Book > findByNameOrPrice (String name , Integer price );
57
78
58
- // Equivalent Json Query will be "{"bool" : {"must" : {"field" : {"name" : "?"}}}}"
59
79
Page<Book > findByName (String name ,Pageable page );
60
80
61
- // Equivalent Json Query will be "{"bool" : {"must_not" : {"field" : {"name" : "?"}}}}"
62
81
Page<Book > findByNameNot (String name ,Pageable page );
63
82
64
- // Equivalent Json Query will be "{"bool" : {"must" : {"range" : {"price" : {"from" : ?,"to" : ?,"include_lower" : true,"include_upper" : true}}}}}"
65
83
Page<Book > findByPriceBetween (int price ,Pageable page );
66
84
67
-
68
- // Equivalent Json Query will be "{"bool" : {"must" : {"field" : {"name" : {"query" : "?*","analyze_wildcard" : true}}}}"
69
85
Page<Book > findByNameLike (String name ,Pageable page );
70
86
71
-
72
- @Query (" {\" bool\" : {\" must\" : {\" field\" : {\" message\" : \" ?0\" }}}}" )
87
+ @Query (" {\" bool\" : {\" must\" : {\" term\" : {\" message\" : \" ?0\" }}}}" )
73
88
Page<Book > findByMessage (String message , Pageable pageable );
74
89
}
75
90
```
76
-
77
- Indexing a single document using Elasticsearch Template
78
-
79
- ``` java
80
- String documentId = " 123456" ;
81
- SampleEntity sampleEntity = new SampleEntity ();
82
- sampleEntity. setId(documentId);
83
- sampleEntity. setMessage(" some message" );
84
- IndexQuery indexQuery = new IndexQuery ();
85
- indexQuery. setId(documentId);
86
- indexQuery. setObject(sampleEntity);
87
- elasticsearchTemplate. index(indexQuery);
88
- ```
89
-
90
- Indexing multiple Document(bulk index) using Elasticsearch Template
91
-
92
- ``` java
93
- @Autowired
94
- private ElasticsearchTemplate elasticsearchTemplate;
95
-
96
- List<IndexQuery > indexQueries = new ArrayList<IndexQuery > ();
97
- // first document
98
- String documentId = " 123456" ;
99
- SampleEntity sampleEntity1 = new SampleEntity ();
100
- sampleEntity1. setId(documentId);
101
- sampleEntity1. setMessage(" some message" );
102
-
103
- IndexQuery indexQuery1 = new IndexQuery ();
104
- indexQuery1. setId(documentId);
105
- indexQuery1. setObject(sampleEntity1);
106
- indexQueries. add(indexQuery1);
107
-
108
- // second document
109
- String documentId2 = " 123457" ;
110
- SampleEntity sampleEntity2 = new SampleEntity ();
111
- sampleEntity2. setId(documentId2);
112
- sampleEntity2. setMessage(" some message" );
113
- IndexQuery indexQuery2 = new IndexQuery ();
114
- indexQuery2. setId(documentId2);
115
- indexQuery2. setObject(sampleEntity2);
116
- indexQueries. add(indexQuery2);
117
- // bulk index
118
- elasticsearchTemplate. bulkIndex(indexQueries);
119
- ```
120
-
121
- Searching entities using Elasticsearch Template
122
-
123
- ``` java
124
- @Autowired
125
- private ElasticsearchTemplate elasticsearchTemplate;
126
-
127
- SearchQuery searchQuery = new NativeSearchQueryBuilder ()
128
- .withQuery(fieldQuery(" id" , documentId))
129
- .build();
130
- Page<SampleEntity > sampleEntities = elasticsearchTemplate. queryForPage(searchQuery,SampleEntity . class);
131
- ```
132
-
133
91
Indexing a single document with Repository
134
92
135
93
``` java
@@ -166,156 +124,63 @@ Indexing multiple Document(bulk index) using Repository
166
124
repository. save(sampleEntities);
167
125
```
168
126
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
127
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 >
128
+ ### ElasticsearchTemplate
220
129
221
- < bean name = " entityMapper " class = " org.springframework.data. elasticsearch.core.DefaultEntityMapper " />
130
+ ElasticsearchTemplate is the central support class for elasticsearch operations.
222
131
223
- <bean name =" resultMapper" class =" org.springframework.data.elasticsearch.core.CustomResultMapper" >
224
- <constructor-arg ref =" entityMapper" />
225
- </bean >
132
+ Indexing a single document using Elasticsearch Template
226
133
227
- </beans >
134
+ ``` java
135
+ String documentId = " 123456" ;
136
+ SampleEntity sampleEntity = new SampleEntity ();
137
+ sampleEntity. setId(documentId);
138
+ sampleEntity. setMessage(" some message" );
139
+ IndexQuery indexQuery = new IndexQueryBuilder (). withId(sampleEntity. getId()). withObject(sampleEntity). build();
140
+ elasticsearchTemplate. index(indexQuery);
228
141
```
229
142
230
- ### Geo indexing and request
231
-
232
- You can make request using geo_distance filter. This can be done using GeoPoint object.
233
-
234
- First, here is a sample of an entity with a GeoPoint field (location)
143
+ Indexing multiple Document(bulk index) using Elasticsearch Template
235
144
236
145
``` java
237
- @Document (indexName = " test-geo-index" , type = " geo-class-point-type" )
238
- public class AuthorMarkerEntity {
239
-
240
- @Id
241
- private String id;
242
- private String name;
243
-
244
- private GeoPoint location;
245
-
246
- private AuthorMarkerEntity (){
247
- }
248
-
249
- public AuthorMarkerEntity (String id ){
250
- this . id = id;
251
- }
252
-
253
- public String getId () {
254
- return id;
255
- }
256
-
257
- public void setId (String id ) {
258
- this . id = id;
259
- }
146
+ @Autowired
147
+ private ElasticsearchTemplate elasticsearchTemplate;
260
148
261
- public String getName () {
262
- return name;
263
- }
149
+ List<IndexQuery > indexQueries = new ArrayList<IndexQuery > ();
150
+ // first document
151
+ String documentId = " 123456" ;
152
+ SampleEntity sampleEntity1 = new SampleEntity ();
153
+ sampleEntity1. setId(documentId);
154
+ sampleEntity1. setMessage(" some message" );
264
155
265
- public void setName (String name ) {
266
- this . name = name;
267
- }
156
+ IndexQuery indexQuery1 = new IndexQueryBuilder (). withId(sampleEntity1. getId()). withObject(sampleEntity1). build();
157
+ indexQueries. add(indexQuery1);
268
158
269
- public GeoPoint getLocation () {
270
- return location;
271
- }
159
+ // second document
160
+ String documentId2 = " 123457" ;
161
+ SampleEntity sampleEntity2 = new SampleEntity ();
162
+ sampleEntity2. setId(documentId2);
163
+ sampleEntity2. setMessage(" some message" );
272
164
273
- public void setLocation (GeoPoint location ) {
274
- this . location = location;
275
- }
276
- }
277
- ```
165
+ IndexQuery indexQuery2 = new IndexQueryBuilder (). withId(sampleEntity2. getId()). withObject(sampleEntity2). build()
166
+ indexQueries. add(indexQuery2);
278
167
279
- Indexing your entity with a geo-point :
280
- ``` java
281
- elasticsearchTemplate. createIndex(AuthorMarkerEntity . class);
282
- elasticsearchTemplate. refresh(AuthorMarkerEntity . class, true );
283
- elasticsearchTemplate. putMapping(AuthorMarkerEntity . class);
284
-
285
- List<IndexQuery > indexQueries = new ArrayList<IndexQuery > ();
286
- indexQueries. add(new AuthorMarkerEntityBuilder (" 1" ). name(" Franck Marchand" ). location(45.7806d , 3.0875d ). buildIndex());
287
- indexQueries. add(new AuthorMarkerEntityBuilder (" 2" ). name(" Mohsin Husen" ). location(51.5171d , 0.1062d ). buildIndex());
288
- indexQueries. add(new AuthorMarkerEntityBuilder (" 3" ). name(" Rizwan Idrees" ). location(51.5171d , 0.1062d ). buildIndex());
289
- elasticsearchTemplate. bulkIndex(indexQueries);
168
+ // bulk index
169
+ elasticsearchTemplate. bulkIndex(indexQueries);
290
170
```
291
171
292
- For your information :
293
- - Clermont-Ferrand : 45.7806, 3.0875
294
- - London : 51.5171, 0.1062
295
-
296
- So, if you want to search for authors who are located within 20 kilometers around Clermont-Ferrand, here is the way to build your query :
172
+ Searching entities using Elasticsearch Template
297
173
298
174
``` java
299
- CriteriaQuery geoLocationCriteriaQuery = new CriteriaQuery (
300
- new Criteria (" location" ). within(new GeoPoint (45.7806d , 3.0875d ), " 20km" ));
301
- List<AuthorMarkerEntity > geoAuthorsForGeoCriteria = elasticsearchTemplate. queryForList(geoLocationCriteriaQuery, AuthorMarkerEntity . class);
302
- ```
303
-
304
- This example should only return one author named "Franck Marchand".
305
-
306
- You can even combine with other criteria (e.g. author name) :
307
-
308
- Here, we're looking for authors located within 20 kilometers around London AND named "Mohsin Husen" :
175
+ @Autowired
176
+ private ElasticsearchTemplate elasticsearchTemplate;
309
177
310
- ``` java
311
- CriteriaQuery geoLocationCriteriaQuery2 = new CriteriaQuery (
312
- new Criteria ( " name " ) . is( " Mohsin Husen " ) . and( " location " ) . within( new GeoPoint ( 51.5171d , 0.1062d ), " 20km " ) );
313
- List< AuthorMarkerEntity > geoAuthorsForGeoCriteria2 = elasticsearchTemplate. queryForList(geoLocationCriteriaQuery2, AuthorMarkerEntity . class);
178
+ SearchQuery searchQuery = new NativeSearchQueryBuilder ()
179
+ .withQuery(queryString(documentId) . field( " id " ))
180
+ .build( );
181
+ Page< SampleEntity > sampleEntities = elasticsearchTemplate. queryForPage(searchQuery, SampleEntity . class);
314
182
```
315
183
316
- This example should only return one author named "Mohsin Husen".
317
-
318
-
319
184
### XML Namespace
320
185
321
186
You can set up repository scanning via xml configuration, which will happily create your repositories.
@@ -351,14 +216,19 @@ Using Transport Client
351
216
352
217
<elasticsearch : repositories base-package =" com.xyz.acme" />
353
218
354
- <elasticsearch : transport-client id =" client" cluster-nodes =" localhost :9300,someip :9300" />
219
+ <elasticsearch : transport-client id =" client" cluster-nodes =" ip :9300,ip :9300" cluster-name = " elasticsearch " />
355
220
356
221
<bean name =" elasticsearchTemplate" class =" org.springframework.data.elasticsearch.core.ElasticsearchTemplate" >
357
222
<constructor-arg name =" client" ref =" client" />
358
223
</bean >
359
224
360
225
</beans >
361
- ```
226
+ ```
227
+
228
+ ## Help Pages
229
+
230
+ * [ Geo distance and location search] ( https://github.com/spring-projects/spring-data-elasticsearch/wiki/Geo-indexing-and-request )
231
+ * [ Custom object mapper] ( https://github.com/spring-projects/spring-data-elasticsearch/wiki/Custom-ObjectMapper )
362
232
363
233
## Contributing to Spring Data
364
234
@@ -381,4 +251,4 @@ Code formatting for [Eclipse and Intellij](https://github.com/spring-projects/sp
381
251
* Rizwan Idrees (
[email protected] )
382
252
* Abdul Waheed (
[email protected] )
383
253
* Mohsin Husen (
[email protected] )
384
- * Artur Konczak(
[email protected] )
254
+ * Artur Konczak(
[email protected] )
0 commit comments