diff --git a/README.md b/README.md index 6eb37776..130e4a46 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,7 @@ We have separate folders for the samples of individual modules: ## Spring Data Elasticsearch -* `example` - Example how to use basic text search, geo-spatial search and facets. -* `rest` - Example how to use the High Level REST Client backing template and repository. +* `example` - Example how to use basic text search, geo-spatial search and facets. It uses the High Level REST Client backing template and repository. * `reactive` - Example how to use reactive client, template and repository features. ## Spring Data for Apache Geode @@ -115,4 +114,4 @@ We have separate folders for the samples of individual modules: ## Note * The example projects make use of the [Lombok](https://projectlombok.org/) plugin. To get proper code navigation in your IDE, you must install it separately. -Lombok is available in the IntelliJ plugins repository and as a [download](https://projectlombok.org/download) for Eclipse-based IDEs. \ No newline at end of file +Lombok is available in the IntelliJ plugins repository and as a [download](https://projectlombok.org/download) for Eclipse-based IDEs. diff --git a/elasticsearch/example/README.md b/elasticsearch/example/README.md index 001c8bb7..1989dc77 100644 --- a/elasticsearch/example/README.md +++ b/elasticsearch/example/README.md @@ -1,8 +1,5 @@ # Spring Data Elasticsearch - Examples - -**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. - Requirements: * [Maven](http://maven.apache.org/download.cgi) - required. diff --git a/elasticsearch/example/pom.xml b/elasticsearch/example/pom.xml index 18a31bae..38528897 100644 --- a/elasticsearch/example/pom.xml +++ b/elasticsearch/example/pom.xml @@ -5,7 +5,7 @@ spring-data-elasticsearch-example - Spring Data Elasticsearch - Node Client Example + Spring Data Elasticsearch - High Level REST Client Example Sample projects for Spring Data Elasticsearch https://github.com/spring-projects/spring-data-elasticsearch diff --git a/elasticsearch/example/src/main/java/example/springdata/elasticsearch/conference/ApplicationConfiguration.java b/elasticsearch/example/src/main/java/example/springdata/elasticsearch/conference/ApplicationConfiguration.java index dcc4aaad..a1fab6fd 100644 --- a/elasticsearch/example/src/main/java/example/springdata/elasticsearch/conference/ApplicationConfiguration.java +++ b/elasticsearch/example/src/main/java/example/springdata/elasticsearch/conference/ApplicationConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2020 the original author or authors. + * Copyright 2020-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,25 +16,20 @@ package example.springdata.elasticsearch.conference; import java.util.Arrays; -import java.util.UUID; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; -import org.elasticsearch.client.Client; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.Bean; -import org.springframework.data.elasticsearch.client.NodeClientFactoryBean; import org.springframework.data.elasticsearch.core.ElasticsearchOperations; -import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.data.elasticsearch.core.geo.GeoPoint; /** * @author Artur Konczak * @author Oliver Gierke * @author Christoph Strobl + * @author Prakhar Gupta */ @SpringBootApplication class ApplicationConfiguration { @@ -42,23 +37,6 @@ class ApplicationConfiguration { @Autowired ElasticsearchOperations operations; @Autowired ConferenceRepository repository; - @Bean - public NodeClientFactoryBean client() { - - NodeClientFactoryBean bean = new NodeClientFactoryBean(true); - bean.setClusterName(UUID.randomUUID().toString()); - bean.setEnableHttp(false); - bean.setPathData("target/elasticsearchTestData"); - bean.setPathHome("src/test/resources/test-home-dir"); - - return bean; - } - - @Bean - public ElasticsearchTemplate elasticsearchTemplate(Client client) { - return new ElasticsearchTemplate(client); - } - @PreDestroy public void deleteIndex() { operations.indexOps(Conference.class).delete(); diff --git a/elasticsearch/pom.xml b/elasticsearch/pom.xml index 95dd2eb8..1f509d98 100644 --- a/elasticsearch/pom.xml +++ b/elasticsearch/pom.xml @@ -18,7 +18,6 @@ util example - rest reactive diff --git a/elasticsearch/reactive/src/main/java/example/springdata/elasticsearch/conference/ApplicationConfiguration.java b/elasticsearch/reactive/src/main/java/example/springdata/elasticsearch/conference/ApplicationConfiguration.java index 33102785..ac56e5c0 100644 --- a/elasticsearch/reactive/src/main/java/example/springdata/elasticsearch/conference/ApplicationConfiguration.java +++ b/elasticsearch/reactive/src/main/java/example/springdata/elasticsearch/conference/ApplicationConfiguration.java @@ -21,9 +21,11 @@ import java.io.IOException; import java.util.Arrays; import javax.annotation.PostConstruct; +import javax.annotation.PreDestroy; import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; +import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.client.RequestOptions; import org.springframework.beans.factory.annotation.Autowired; @@ -42,6 +44,16 @@ class ApplicationConfiguration { @Autowired ReactiveElasticsearchOperations operations; @Autowired ConferenceRepository repository; + @PreDestroy + public void deleteIndex() { + try { + RestClients.create(ClientConfiguration.localhost()).rest().indices() + .delete(new DeleteIndexRequest("conference-index"), RequestOptions.DEFAULT); + } catch (IOException | ElasticsearchStatusException e) { + // just ignore it + } + } + @PostConstruct public void insertDataSample() { diff --git a/elasticsearch/rest/README.md b/elasticsearch/rest/README.md deleted file mode 100644 index 8b6711d9..00000000 --- a/elasticsearch/rest/README.md +++ /dev/null @@ -1,48 +0,0 @@ -# Spring Data Elasticsearch - High Level REST Client Examples - -```java -@SpringBootApplication -class ApplicationConfiguration {} -``` - - -The `RestHighLevelClient` can be used with the `ElasticsearchOperations` and `ElasticsearchRepository`. - -```java -@Autowired ElasticsearchOperations operations; - -// ... - -CriteriaQuery query = new CriteriaQuery("keywords").contains("java"); - -List result = operations.find(query, Conference.class); -``` - -```java -interface ConferenceRepository extends ElasticsearchRepository { - - List findAllByKeywordsContains(String keyword); -} - -// ... - -@Autowired ConferenceRepository repository; - -// ... - -List result = repository.findAllByKeywordsContains("java"); -``` - - -**Requirements:** - - * [Maven](http://maven.apache.org/download.cgi) - * [Elasticsearch](https://www.elastic.co/de/downloads/elasticsearch) - -**Running Elasticsearch** - -```bash -$ cd elasticsearch -$ ./bin/elasticsearch -``` - diff --git a/elasticsearch/rest/pom.xml b/elasticsearch/rest/pom.xml deleted file mode 100644 index 97af6502..00000000 --- a/elasticsearch/rest/pom.xml +++ /dev/null @@ -1,37 +0,0 @@ - - - 4.0.0 - - spring-data-elasticsearch-rest-client-example - - Spring Data Elasticsearch - Rest Client Example - Sample projects for Spring Data Elasticsearch using the Rest High Level Client - https://github.com/spring-projects/spring-data-elasticsearch - - - org.springframework.data.examples - spring-data-elasticsearch-examples - 2.0.0.BUILD-SNAPSHOT - - - - - - org.springframework.data.examples - spring-data-elasticsearch-example-utils - - - - org.springframework.boot - spring-boot-starter-log4j2 - - - - org.springframework - spring-web - - - - - diff --git a/elasticsearch/rest/src/main/java/example/springdata/elasticsearch/conference/ApplicationConfiguration.java b/elasticsearch/rest/src/main/java/example/springdata/elasticsearch/conference/ApplicationConfiguration.java deleted file mode 100644 index 0b88e837..00000000 --- a/elasticsearch/rest/src/main/java/example/springdata/elasticsearch/conference/ApplicationConfiguration.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright 2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package example.springdata.elasticsearch.conference; - -import java.util.Arrays; - -import javax.annotation.PostConstruct; -import javax.annotation.PreDestroy; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.data.elasticsearch.core.ElasticsearchOperations; -import org.springframework.data.elasticsearch.core.geo.GeoPoint; - -/** - * @author Artur Konczak - * @author Oliver Gierke - * @author Christoph Strobl - */ -@SpringBootApplication -class ApplicationConfiguration { - - @Autowired ElasticsearchOperations elasticsearchOperations; - @Autowired ConferenceRepository repository; - - @PreDestroy - public void deleteIndex() { - elasticsearchOperations.indexOps(Conference.class).delete(); - } - - @PostConstruct - public void insertDataSample() { - - repository.deleteAll(); - elasticsearchOperations.indexOps(Conference.class).refresh(); - - // Save data sample - repository.save(Conference.builder().date("2014-11-06").name("Spring eXchange 2014 - London") - .keywords(Arrays.asList("java", "spring")).location(new GeoPoint(51.500152D, -0.126236D)).build()); - repository.save(Conference.builder().date("2014-12-07").name("Scala eXchange 2014 - London") - .keywords(Arrays.asList("scala", "play", "java")).location(new GeoPoint(51.500152D, -0.126236D)).build()); - repository.save(Conference.builder().date("2014-11-20").name("Elasticsearch 2014 - Berlin") - .keywords(Arrays.asList("java", "elasticsearch", "kibana")).location(new GeoPoint(52.5234051D, 13.4113999)) - .build()); - repository.save(Conference.builder().date("2014-11-12").name("AWS London 2014") - .keywords(Arrays.asList("cloud", "aws")).location(new GeoPoint(51.500152D, -0.126236D)).build()); - repository.save(Conference.builder().date("2014-10-04").name("JDD14 - Cracow") - .keywords(Arrays.asList("java", "spring")).location(new GeoPoint(50.0646501D, 19.9449799)).build()); - } -} diff --git a/elasticsearch/rest/src/main/java/example/springdata/elasticsearch/conference/Conference.java b/elasticsearch/rest/src/main/java/example/springdata/elasticsearch/conference/Conference.java deleted file mode 100644 index bce3ae94..00000000 --- a/elasticsearch/rest/src/main/java/example/springdata/elasticsearch/conference/Conference.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package example.springdata.elasticsearch.conference; - -import static org.springframework.data.elasticsearch.annotations.FieldType.*; - -import lombok.Builder; -import lombok.Data; - -import java.util.List; - -import org.springframework.data.annotation.Id; -import org.springframework.data.elasticsearch.annotations.Document; -import org.springframework.data.elasticsearch.annotations.Field; -import org.springframework.data.elasticsearch.core.geo.GeoPoint; - -/** - * @author Artur Konczak - * @author Oliver Gierke - * @author Christoph Strobl - */ -@Data -@Builder -@Document(indexName = "conference-index") -public class Conference { - - private @Id String id; - private String name; - private @Field(type = Date) String date; - private GeoPoint location; - private List keywords; - - // do not remove it - public Conference() {} - - // do not remove it - work around for lombok generated constructor for all params - public Conference(String id, String name, String date, GeoPoint location, List keywords) { - - this.id = id; - this.name = name; - this.date = date; - this.location = location; - this.keywords = keywords; - } -} diff --git a/elasticsearch/rest/src/main/java/example/springdata/elasticsearch/conference/ConferenceRepository.java b/elasticsearch/rest/src/main/java/example/springdata/elasticsearch/conference/ConferenceRepository.java deleted file mode 100644 index 21deb357..00000000 --- a/elasticsearch/rest/src/main/java/example/springdata/elasticsearch/conference/ConferenceRepository.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright 2019 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package example.springdata.elasticsearch.conference; - -import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; - -/** - * @author Christoph Strobl - */ -interface ConferenceRepository extends ElasticsearchRepository {} diff --git a/elasticsearch/rest/src/main/resources/application.properties b/elasticsearch/rest/src/main/resources/application.properties deleted file mode 100644 index b798c505..00000000 --- a/elasticsearch/rest/src/main/resources/application.properties +++ /dev/null @@ -1,2 +0,0 @@ -# Uncomment both entries to connect with local elasticsearch cluster -spring.elasticsearch.rest.uris=http://localhost:9200 diff --git a/elasticsearch/rest/src/test/java/example/springdata/elasticsearch/conference/ElasticsearchOperationsTest.java b/elasticsearch/rest/src/test/java/example/springdata/elasticsearch/conference/ElasticsearchOperationsTest.java deleted file mode 100644 index 455fa471..00000000 --- a/elasticsearch/rest/src/test/java/example/springdata/elasticsearch/conference/ElasticsearchOperationsTest.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright 2019-2021 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package example.springdata.elasticsearch.conference; - -import static org.assertj.core.api.Assertions.*; - -import example.springdata.elasticsearch.util.EnabledOnElasticsearch; - -import java.text.ParseException; -import java.text.SimpleDateFormat; - -import org.junit.jupiter.api.Test; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.data.elasticsearch.core.ElasticsearchOperations; -import org.springframework.data.elasticsearch.core.SearchHit; -import org.springframework.data.elasticsearch.core.SearchHits; -import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; -import org.springframework.data.elasticsearch.core.query.Criteria; -import org.springframework.data.elasticsearch.core.query.CriteriaQuery; - -/** - * Test case to show Spring Data Elasticsearch functionality. - * - * @author Christoph Strobl - * @author Prakhar Gupta - */ -@EnabledOnElasticsearch -@SpringBootTest(classes = ApplicationConfiguration.class) -class ElasticsearchOperationsTest { - - private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); - - @Autowired - ElasticsearchOperations operations; - - @Test - void textSearch() throws ParseException { - String expectedDate = "2014-10-29"; - String expectedWord = "java"; - CriteriaQuery query = new CriteriaQuery( - new Criteria("keywords").contains(expectedWord).and(new Criteria("date").greaterThanEqual(expectedDate))); - - SearchHits result = operations.search(query, Conference.class, IndexCoordinates.of("conference-index")); - - assertThat(result).hasSize(3); - - for (SearchHit conference : result) { - assertThat(conference.getContent().getKeywords()).contains(expectedWord); - assertThat(format.parse(conference.getContent().getDate())).isAfter(format.parse(expectedDate)); - } - } -}