Skip to content

Commit 3c0c942

Browse files
committed
DATAES-65 - Overhaul README.md
1 parent ea45aba commit 3c0c942

File tree

1 file changed

+74
-204
lines changed

1 file changed

+74
-204
lines changed

README.md

Lines changed: 74 additions & 204 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,47 @@ Guide
1515
* [API Documentation](http://docs.spring.io/spring-data/elasticsearch/docs/1.0.0.M1/api/)
1616
* [Spring Data Project](http://www.springsource.org/spring-data)
1717
* [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).
1819

1920

2021
Quick Start
2122
-----------
2223
Wiki page for [Getting Started] (https://github.com/spring-projects/spring-data-elasticsearch/wiki/How-to-start-with-spring-data-elasticsearch)
2324

24-
### Dependency
25-
```java
25+
### Maven configuration
26+
27+
Add the Maven dependency:
28+
29+
```xml
2630
<dependency>
2731
<groupId>org.springframework.data</groupId>
2832
<artifactId>spring-data-elasticsearch</artifactId>
2933
<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>
3141
```
3242

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.
3545

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+
```
3659

3760
### ElasticsearchRepository
3861
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
4972
```java
5073
public interface BookRepository extends Repository<Book, String> {
5174

52-
//Equivalent Json Query will be "{ "bool" : { "must" :[{ "field" : {"name" : "?"} },{ "field" : {"price" : "?"} }]} }"
5375
List<Book> findByNameAndPrice(String name, Integer price);
5476

55-
//Equivalent Json Query will be "{"bool" : {"should" : [ {"field" : "name" : "?"}}, {"field" : {"price" : "?"}} ]}}"
5677
List<Book> findByNameOrPrice(String name, Integer price);
5778

58-
//Equivalent Json Query will be "{"bool" : {"must" : {"field" : {"name" : "?"}}}}"
5979
Page<Book> findByName(String name,Pageable page);
6080

61-
//Equivalent Json Query will be "{"bool" : {"must_not" : {"field" : {"name" : "?"}}}}"
6281
Page<Book> findByNameNot(String name,Pageable page);
6382

64-
//Equivalent Json Query will be "{"bool" : {"must" : {"range" : {"price" : {"from" : ?,"to" : ?,"include_lower" : true,"include_upper" : true}}}}}"
6583
Page<Book> findByPriceBetween(int price,Pageable page);
6684

67-
68-
//Equivalent Json Query will be "{"bool" : {"must" : {"field" : {"name" : {"query" : "?*","analyze_wildcard" : true}}}}"
6985
Page<Book> findByNameLike(String name,Pageable page);
7086

71-
72-
@Query("{\"bool\" : {\"must\" : {\"field\" : {\"message\" : \"?0\"}}}}")
87+
@Query("{\"bool\" : {\"must\" : {\"term\" : {\"message\" : \"?0\"}}}}")
7388
Page<Book> findByMessage(String message, Pageable pageable);
7489
}
7590
```
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-
13391
Indexing a single document with Repository
13492

13593
```java
@@ -166,156 +124,63 @@ Indexing multiple Document(bulk index) using Repository
166124
repository.save(sampleEntities);
167125
```
168126

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" />
214127

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
220129

221-
<bean name="entityMapper" class="org.springframework.data.elasticsearch.core.DefaultEntityMapper"/>
130+
ElasticsearchTemplate is the central support class for elasticsearch operations.
222131

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
226133

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);
228141
```
229142

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
235144

236145
```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;
260148

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");
264155

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);
268158

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");
272164

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);
278167

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);
290170
```
291171

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
297173

298174
```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;
309177

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);
314182
```
315183

316-
This example should only return one author named "Mohsin Husen".
317-
318-
319184
### XML Namespace
320185

321186
You can set up repository scanning via xml configuration, which will happily create your repositories.
@@ -351,14 +216,19 @@ Using Transport Client
351216

352217
<elasticsearch:repositories base-package="com.xyz.acme"/>
353218

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" />
355220

356221
<bean name="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
357222
<constructor-arg name="client" ref="client"/>
358223
</bean>
359224

360225
</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)
362232

363233
## Contributing to Spring Data
364234

@@ -381,4 +251,4 @@ Code formatting for [Eclipse and Intellij](https://github.com/spring-projects/sp
381251
* Rizwan Idrees ([email protected])
382252
* Abdul Waheed ([email protected])
383253
* Mohsin Husen ([email protected])
384-
* Artur Konczak([email protected])
254+
* Artur Konczak([email protected])

0 commit comments

Comments
 (0)