#509 - Favor Spring Boot auto-configuration in Elasticsearch example.

This commit is contained in:
Mark Paluch
2020-06-16 15:16:29 +02:00
parent 3d88c50655
commit d92e7feabc
11 changed files with 37 additions and 75 deletions

View File

@@ -12,5 +12,5 @@ Requirements:
To use local elasticsearch cluster: To use local elasticsearch cluster:
* install elasticsearch * install elasticsearch
* uncomment both properties in file 'application.properties' * Adapt 'application.properties' to point to your elasticsearch cluster

View File

@@ -1,11 +1,11 @@
/* /*
* Copyright 2014-2018 the original author or authors. * Copyright 2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* https://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
@@ -22,22 +22,21 @@ import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy; import javax.annotation.PreDestroy;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.NodeClientFactoryBean; import org.springframework.data.elasticsearch.client.NodeClientFactoryBean;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations; import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.geo.GeoPoint; import org.springframework.data.elasticsearch.core.geo.GeoPoint;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
/** /**
* @author Artur Konczak * @author Artur Konczak
* @author Oliver Gierke * @author Oliver Gierke
* @author Christoph Strobl * @author Christoph Strobl
*/ */
@Configuration @SpringBootApplication
@EnableElasticsearchRepositories
class ApplicationConfiguration { class ApplicationConfiguration {
@Autowired ElasticsearchOperations operations; @Autowired ElasticsearchOperations operations;
@@ -56,7 +55,7 @@ class ApplicationConfiguration {
} }
@Bean @Bean
public ElasticsearchTemplate elasticsearchTemplate(Client client) throws Exception { public ElasticsearchTemplate elasticsearchTemplate(Client client) {
return new ElasticsearchTemplate(client); return new ElasticsearchTemplate(client);
} }

View File

@@ -1,3 +1 @@
# Uncomment both entries to connect with local elasticsearch cluster #spring.elasticsearch.rest.uris=http://localhost:9200
#spring.data.elasticsearch.clusterName=elasticsearch
#spring.data.elasticsearch.clusterNodes=localhost:9300

View File

@@ -1,11 +1,11 @@
/* /*
* Copyright 2014-2018 the original author or authors. * Copyright 2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* https://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
@@ -15,13 +15,14 @@
*/ */
package example.springdata.elasticsearch.conference; package example.springdata.elasticsearch.conference;
import static org.assertj.core.api.Assertions.*;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.List;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations; import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
@@ -33,8 +34,6 @@ import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery; import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.*;
/** /**
* Test case to show Spring Data Elasticsearch functionality. * Test case to show Spring Data Elasticsearch functionality.
* *

View File

@@ -1,19 +1,13 @@
# Spring Data Elasticsearch - Reactive Examples # Spring Data Elasticsearch - Reactive Examples
The `ReactiveElasticsearchClient` is a non official driver based on `WebClient`. The `ReactiveElasticsearchClient` is a client based on `WebClient`.
It uses the request/response objects provided by the Elasticsearch core project. It uses the request/response objects provided by the Elasticsearch core project.
Calls are directly operated on the reactive stack, not wrapping async (thread pool bound) responses into reactive types. Calls are directly operated on the reactive stack, not wrapping async (thread pool bound) responses into reactive types.
````java ```java
class ApplicationConfiguration extends AbstractReactiveElasticsearchConfiguration { @SpringBootApplication
class ApplicationConfiguration {}
@Bean ```
@Override
public ReactiveElasticsearchClient reactiveElasticsearchClient() {
return ReactiveRestClients.create(ClientConfiguration.localhost());
}
}
````
The `ReactiveElasticsearchClient` can be used with the `ReactiveElasticsearchOperations` and `ReactiveElasticsearchRepository`. The `ReactiveElasticsearchClient` can be used with the `ReactiveElasticsearchOperations` and `ReactiveElasticsearchRepository`.

View File

@@ -1,11 +1,11 @@
/* /*
* Copyright 2019 the original author or authors. * Copyright 2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* https://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
@@ -27,33 +27,21 @@ import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RequestOptions;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration; import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients; import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient;
import org.springframework.data.elasticsearch.client.reactive.ReactiveRestClients;
import org.springframework.data.elasticsearch.config.AbstractReactiveElasticsearchConfiguration;
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations; import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations;
import org.springframework.data.elasticsearch.core.geo.GeoPoint; import org.springframework.data.elasticsearch.core.geo.GeoPoint;
import org.springframework.data.elasticsearch.repository.config.EnableReactiveElasticsearchRepositories;
/** /**
* @author Christoph Strobl * @author Christoph Strobl
*/ */
@Configuration @SpringBootApplication
@EnableReactiveElasticsearchRepositories class ApplicationConfiguration {
class ApplicationConfiguration extends AbstractReactiveElasticsearchConfiguration {
@Autowired ReactiveElasticsearchOperations operations; @Autowired ReactiveElasticsearchOperations operations;
@Autowired ConferenceRepository repository; @Autowired ConferenceRepository repository;
@Bean
@Override
public ReactiveElasticsearchClient reactiveElasticsearchClient() {
return ReactiveRestClients.create(ClientConfiguration.localhost());
}
@PostConstruct @PostConstruct
public void insertDataSample() { public void insertDataSample() {

View File

@@ -1,3 +1 @@
# Uncomment both entries to connect with local elasticsearch cluster spring.data.elasticsearch.client.reactive.endpoints=localhost:9200
#spring.data.elasticsearch.clusterName=elasticsearch
#spring.data.elasticsearch.clusterNodes=localhost:9300

View File

@@ -1,11 +1,11 @@
/* /*
* Copyright 2019 the original author or authors. * Copyright 2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* https://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
@@ -32,13 +32,14 @@ import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperatio
import org.springframework.data.elasticsearch.core.query.Criteria; import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery; import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;
/** /**
* Test case to show Spring Data Elasticsearch functionality. * Test case to show Spring Data Elasticsearch functionality.
* *
* @author Christoph Strobl * @author Christoph Strobl
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(classes = ApplicationConfiguration.class) @SpringBootTest(classes = ApplicationConfiguration.class)
public class ReactiveElasticsearchOperationsTest { public class ReactiveElasticsearchOperationsTest {

View File

@@ -1,15 +1,10 @@
# Spring Data Elasticsearch - High Level REST Client Examples # Spring Data Elasticsearch - High Level REST Client Examples
````java ```java
class ApplicationConfiguration extends AbstractElasticsearchConfiguration { @SpringBootApplication
class ApplicationConfiguration {}
```
@Bean
@Override
public RestHighLevelClient elasticsearchClient() {
return RestClients.create(ClientConfiguration.localhost()).rest();
}
}
````
The `RestHighLevelClient` can be used with the `ElasticsearchOperations` and `ElasticsearchRepository`. The `RestHighLevelClient` can be used with the `ElasticsearchOperations` and `ElasticsearchRepository`.

View File

@@ -1,11 +1,11 @@
/* /*
* Copyright 2014-2018 the original author or authors. * Copyright 2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* https://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
@@ -20,12 +20,9 @@ import java.util.Arrays;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy; import javax.annotation.PreDestroy;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations; import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.geo.GeoPoint; import org.springframework.data.elasticsearch.core.geo.GeoPoint;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
@@ -35,14 +32,8 @@ import org.springframework.data.elasticsearch.repository.config.EnableElasticsea
* @author Oliver Gierke * @author Oliver Gierke
* @author Christoph Strobl * @author Christoph Strobl
*/ */
@Configuration @SpringBootApplication
@EnableElasticsearchRepositories class ApplicationConfiguration {
class ApplicationConfiguration extends AbstractElasticsearchConfiguration {
@Override
public RestHighLevelClient elasticsearchClient() {
return RestClients.create(ClientConfiguration.localhost()).rest();
}
@Autowired ElasticsearchOperations elasticsearchOperations; @Autowired ElasticsearchOperations elasticsearchOperations;
@Autowired ConferenceRepository repository; @Autowired ConferenceRepository repository;

View File

@@ -1,3 +1,2 @@
# Uncomment both entries to connect with local elasticsearch cluster # Uncomment both entries to connect with local elasticsearch cluster
#spring.data.elasticsearch.clusterName=elasticsearch spring.elasticsearch.rest.uris=http://localhost:9200
#spring.data.elasticsearch.clusterNodes=localhost:9300