#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:
* 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");
* 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
* 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,
@@ -22,22 +22,21 @@ 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.context.annotation.Configuration;
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;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
/**
* @author Artur Konczak
* @author Oliver Gierke
* @author Christoph Strobl
*/
@Configuration
@EnableElasticsearchRepositories
@SpringBootApplication
class ApplicationConfiguration {
@Autowired ElasticsearchOperations operations;
@@ -56,7 +55,7 @@ class ApplicationConfiguration {
}
@Bean
public ElasticsearchTemplate elasticsearchTemplate(Client client) throws Exception {
public ElasticsearchTemplate elasticsearchTemplate(Client client) {
return new ElasticsearchTemplate(client);
}

View File

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

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");
* 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
* 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,
@@ -15,13 +15,14 @@
*/
package example.springdata.elasticsearch.conference;
import static org.assertj.core.api.Assertions.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
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.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.*;
/**
* Test case to show Spring Data Elasticsearch functionality.
*

View File

@@ -1,19 +1,13 @@
# 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.
Calls are directly operated on the reactive stack, not wrapping async (thread pool bound) responses into reactive types.
````java
class ApplicationConfiguration extends AbstractReactiveElasticsearchConfiguration {
@Bean
@Override
public ReactiveElasticsearchClient reactiveElasticsearchClient() {
return ReactiveRestClients.create(ClientConfiguration.localhost());
}
}
````
```java
@SpringBootApplication
class ApplicationConfiguration {}
```
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");
* 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
* 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,
@@ -27,33 +27,21 @@ import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
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.geo.GeoPoint;
import org.springframework.data.elasticsearch.repository.config.EnableReactiveElasticsearchRepositories;
/**
* @author Christoph Strobl
*/
@Configuration
@EnableReactiveElasticsearchRepositories
class ApplicationConfiguration extends AbstractReactiveElasticsearchConfiguration {
@SpringBootApplication
class ApplicationConfiguration {
@Autowired ReactiveElasticsearchOperations operations;
@Autowired ConferenceRepository repository;
@Bean
@Override
public ReactiveElasticsearchClient reactiveElasticsearchClient() {
return ReactiveRestClients.create(ClientConfiguration.localhost());
}
@PostConstruct
public void insertDataSample() {

View File

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

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");
* 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
* 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,
@@ -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.CriteriaQuery;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Test case to show Spring Data Elasticsearch functionality.
*
* @author Christoph Strobl
*/
@RunWith(SpringJUnit4ClassRunner.class)
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ApplicationConfiguration.class)
public class ReactiveElasticsearchOperationsTest {

View File

@@ -1,15 +1,10 @@
# Spring Data Elasticsearch - High Level REST Client Examples
````java
class ApplicationConfiguration extends AbstractElasticsearchConfiguration {
```java
@SpringBootApplication
class ApplicationConfiguration {}
```
@Bean
@Override
public RestHighLevelClient elasticsearchClient() {
return RestClients.create(ClientConfiguration.localhost()).rest();
}
}
````
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");
* 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
* 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,
@@ -20,12 +20,9 @@ import java.util.Arrays;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
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.RestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
@@ -35,14 +32,8 @@ import org.springframework.data.elasticsearch.repository.config.EnableElasticsea
* @author Oliver Gierke
* @author Christoph Strobl
*/
@Configuration
@EnableElasticsearchRepositories
class ApplicationConfiguration extends AbstractElasticsearchConfiguration {
@Override
public RestHighLevelClient elasticsearchClient() {
return RestClients.create(ClientConfiguration.localhost()).rest();
}
@SpringBootApplication
class ApplicationConfiguration {
@Autowired ElasticsearchOperations elasticsearchOperations;
@Autowired ConferenceRepository repository;

View File

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