Skip to content

Commit 2bf08bf

Browse files
christophstroblodrotbohm
authored andcommitted
#497 - Add Example for Elasticsearch High Level REST Client.
1 parent 97a4574 commit 2bf08bf

File tree

13 files changed

+415
-4
lines changed

13 files changed

+415
-4
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ We have separate folders for the samples of individual modules:
5959
## Spring Data Elasticsearch
6060

6161
* `example` - Example how to use basic text search, geo-spatial search and facets.
62+
* `rest` - Example how to use the High Level REST Client backing template and repository.
6263
* `reactive` - Example how to use reactive client, template and repository features.
6364

6465
## Spring Data Neo4j

elasticsearch/example/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Spring Data Elasticsearch - Examples
22

3+
4+
**NOTE**: Elastic recommends usage of the [High Level REST Client](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high.html). Please check out the [rest](https://github.com/spring-projects/spring-data-examples/tree/master/elasticsearch/rest) example.
5+
36
Requirements:
47

58
* [Maven](http://maven.apache.org/download.cgi) - required.
@@ -11,4 +14,3 @@ To use local elasticsearch cluster:
1114
* install elasticsearch
1215
* uncomment both properties in file 'application.properties'
1316

14-

elasticsearch/example/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<groupId>org.springframework</groupId>
77
<artifactId>spring-data-elasticsearch-example</artifactId>
88

9-
<name>Spring Data Elasticsearch - Example</name>
9+
<name>Spring Data Elasticsearch - Node Client Example</name>
1010
<description>Sample projects for Spring Data Elasticsearch</description>
1111
<url>https://github.com/spring-projects/spring-data-elasticsearch</url>
1212

elasticsearch/pom.xml

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
<modules>
1919
<module>example</module>
20+
<module>rest</module>
2021
<module>reactive</module>
2122
</modules>
2223

@@ -31,13 +32,13 @@
3132
<profile>
3233
<id>spring-data-next-releasetrain</id>
3334
<properties>
34-
<elasticsearch.version>6.6.1</elasticsearch.version>
35+
<elasticsearch.version>6.8.2</elasticsearch.version>
3536
</properties>
3637
</profile>
3738
<profile>
3839
<id>spring-data-next-releasetrain-released</id>
3940
<properties>
40-
<elasticsearch.version>6.6.1</elasticsearch.version>
41+
<elasticsearch.version>6.8.2</elasticsearch.version>
4142
</properties>
4243
</profile>
4344
</profiles>

elasticsearch/rest/.gitignore

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.project
2+
.classpath
3+
.springBeans
4+
.settings/
5+
target/
6+
7+
#IntelliJ Stuff
8+
.idea
9+
*.iml
10+
11+
##ignore local node data files for unit tests
12+
/data
13+
/.DS_Store

elasticsearch/rest/README.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Spring Data Elasticsearch - High Level REST Client Examples
2+
3+
````java
4+
class ApplicationConfiguration extends AbstractElasticsearchConfiguration {
5+
6+
@Bean
7+
@Override
8+
public RestHighLevelClient elasticsearchClient() {
9+
return RestClients.create(ClientConfiguration.localhost()).rest();
10+
}
11+
}
12+
````
13+
14+
The `RestHighLevelClient` can be used with the `ElasticsearchOperations` and `ElasticsearchRepository`.
15+
16+
```java
17+
@Autowired ElasticsearchOperations operations;
18+
19+
// ...
20+
21+
CriteriaQuery query = new CriteriaQuery("keywords").contains("java");
22+
23+
List<Conference> result = operations.find(query, Conference.class);
24+
```
25+
26+
```java
27+
interface ConferenceRepository extends ElasticsearchRepository<Conference, String> {
28+
29+
List<Conference> findAllByKeywordsContains(String keyword);
30+
}
31+
32+
// ...
33+
34+
@Autowired ConferenceRepository repository;
35+
36+
// ...
37+
38+
List<Conference> result = repository.findAllByKeywordsContains("java");
39+
```
40+
41+
42+
**Requirements:**
43+
44+
* [Maven](http://maven.apache.org/download.cgi)
45+
* [Elasticsearch](https://www.elastic.co/de/downloads/elasticsearch)
46+
47+
**Running Elasticsearch**
48+
49+
```bash
50+
$ cd elasticsearch
51+
$ ./bin/elasticsearch
52+
```
53+

elasticsearch/rest/pom.xml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>org.springframework</groupId>
7+
<artifactId>spring-data-elasticsearch-rest-client-example</artifactId>
8+
9+
<name>Spring Data Elasticsearch - Rest Client Example</name>
10+
<description>Sample projects for Spring Data Elasticsearch using the Rest High Level Client</description>
11+
<url>https://github.com/spring-projects/spring-data-elasticsearch</url>
12+
13+
<parent>
14+
<groupId>org.springframework.data.examples</groupId>
15+
<artifactId>spring-data-elasticsearch-examples</artifactId>
16+
<version>2.0.0.BUILD-SNAPSHOT</version>
17+
</parent>
18+
19+
<dependencies>
20+
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
24+
<exclusions>
25+
<exclusion>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-logging</artifactId>
28+
</exclusion>
29+
</exclusions>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-log4j2</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework</groupId>
38+
<artifactId>spring-web</artifactId>
39+
</dependency>
40+
41+
</dependencies>
42+
43+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2014-2018 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+
* https://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+
package example.springdata.elasticsearch.conference;
17+
18+
import java.util.Arrays;
19+
20+
import javax.annotation.PostConstruct;
21+
import javax.annotation.PreDestroy;
22+
23+
import org.elasticsearch.client.RestHighLevelClient;
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.context.annotation.Configuration;
26+
import org.springframework.data.elasticsearch.client.ClientConfiguration;
27+
import org.springframework.data.elasticsearch.client.RestClients;
28+
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
29+
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
30+
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
31+
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
32+
33+
/**
34+
* @author Artur Konczak
35+
* @author Oliver Gierke
36+
* @author Christoph Strobl
37+
*/
38+
@Configuration
39+
@EnableElasticsearchRepositories
40+
class ApplicationConfiguration extends AbstractElasticsearchConfiguration {
41+
42+
@Override
43+
public RestHighLevelClient elasticsearchClient() {
44+
return RestClients.create(ClientConfiguration.localhost()).rest();
45+
}
46+
47+
@Autowired ElasticsearchOperations elasticsearchOperations;
48+
@Autowired ConferenceRepository repository;
49+
50+
@PreDestroy
51+
public void deleteIndex() {
52+
elasticsearchOperations.deleteIndex(Conference.class);
53+
}
54+
55+
@PostConstruct
56+
public void insertDataSample() {
57+
58+
repository.deleteAll();
59+
elasticsearchOperations.refresh(Conference.class);
60+
61+
// Save data sample
62+
repository.save(Conference.builder().date("2014-11-06").name("Spring eXchange 2014 - London")
63+
.keywords(Arrays.asList("java", "spring")).location(new GeoPoint(51.500152D, -0.126236D)).build());
64+
repository.save(Conference.builder().date("2014-12-07").name("Scala eXchange 2014 - London")
65+
.keywords(Arrays.asList("scala", "play", "java")).location(new GeoPoint(51.500152D, -0.126236D)).build());
66+
repository.save(Conference.builder().date("2014-11-20").name("Elasticsearch 2014 - Berlin")
67+
.keywords(Arrays.asList("java", "elasticsearch", "kibana")).location(new GeoPoint(52.5234051D, 13.4113999))
68+
.build());
69+
repository.save(Conference.builder().date("2014-11-12").name("AWS London 2014")
70+
.keywords(Arrays.asList("cloud", "aws")).location(new GeoPoint(51.500152D, -0.126236D)).build());
71+
repository.save(Conference.builder().date("2014-10-04").name("JDD14 - Cracow")
72+
.keywords(Arrays.asList("java", "spring")).location(new GeoPoint(50.0646501D, 19.9449799)).build());
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2014-2018 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+
* https://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+
package example.springdata.elasticsearch.conference;
17+
18+
import static org.springframework.data.elasticsearch.annotations.FieldType.*;
19+
20+
import lombok.Builder;
21+
import lombok.Data;
22+
23+
import java.util.List;
24+
25+
import org.springframework.data.annotation.Id;
26+
import org.springframework.data.elasticsearch.annotations.Document;
27+
import org.springframework.data.elasticsearch.annotations.Field;
28+
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
29+
30+
/**
31+
* @author Artur Konczak
32+
* @author Oliver Gierke
33+
* @auhtor Christoph Strobl
34+
*/
35+
@Data
36+
@Builder
37+
@Document(indexName = "conference-index", type = "geo-class-point-type", shards = 1, replicas = 0,
38+
refreshInterval = "-1")
39+
public class Conference {
40+
41+
private @Id String id;
42+
private String name;
43+
private @Field(type = Date) String date;
44+
private GeoPoint location;
45+
private List<String> keywords;
46+
47+
// do not remove it
48+
public Conference() {}
49+
50+
// do not remove it - work around for lombok generated constructor for all params
51+
public Conference(String id, String name, String date, GeoPoint location, List<String> keywords) {
52+
53+
this.id = id;
54+
this.name = name;
55+
this.date = date;
56+
this.location = location;
57+
this.keywords = keywords;
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2019 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+
* https://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+
package example.springdata.elasticsearch.conference;
17+
18+
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
19+
20+
/**
21+
* @author Christoph Strobl
22+
*/
23+
interface ConferenceRepository extends ElasticsearchRepository<Conference, String> {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Uncomment both entries to connect with local elasticsearch cluster
2+
#spring.data.elasticsearch.clusterName=elasticsearch
3+
#spring.data.elasticsearch.clusterNodes=localhost:9300

0 commit comments

Comments
 (0)