#553 - Adapt to Spring Data Elasticsearch.
This commit is contained in:
@@ -62,13 +62,13 @@ class ApplicationConfiguration {
|
||||
|
||||
@PreDestroy
|
||||
public void deleteIndex() {
|
||||
operations.deleteIndex(Conference.class);
|
||||
operations.getIndexOperations().deleteIndex(Conference.class);
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void insertDataSample() {
|
||||
|
||||
operations.refresh(Conference.class);
|
||||
operations.getIndexOperations().refresh(Conference.class);
|
||||
|
||||
// Save data sample
|
||||
repository.save(Conference.builder().date("2014-11-06").name("Spring eXchange 2014 - London")
|
||||
|
||||
@@ -15,8 +15,6 @@
|
||||
*/
|
||||
package example.springdata.elasticsearch.conference;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
@@ -27,11 +25,16 @@ 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;
|
||||
import org.springframework.data.elasticsearch.core.SearchHit;
|
||||
import org.springframework.data.elasticsearch.core.SearchHits;
|
||||
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
|
||||
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
|
||||
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.
|
||||
*
|
||||
@@ -55,13 +58,13 @@ public class ElasticsearchOperationsTest {
|
||||
CriteriaQuery query = new CriteriaQuery(
|
||||
new Criteria("keywords").contains(expectedWord).and(new Criteria("date").greaterThanEqual(expectedDate)));
|
||||
|
||||
List<Conference> result = operations.queryForList(query, Conference.class);
|
||||
SearchHits<Conference> result = operations.search(query, Conference.class, IndexCoordinates.of("conference-index"));
|
||||
|
||||
assertThat(result, hasSize(3));
|
||||
assertThat(result).hasSize(3);
|
||||
|
||||
for (Conference conference : result) {
|
||||
assertThat(conference.getKeywords(), hasItem(expectedWord));
|
||||
assertThat(format.parse(conference.getDate()), greaterThan(format.parse(expectedDate)));
|
||||
for (SearchHit<Conference> conference : result) {
|
||||
assertThat(conference.getContent().getKeywords()).contains(expectedWord);
|
||||
assertThat(format.parse(conference.getContent().getDate())).isAfter(format.parse(expectedDate));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,8 +75,8 @@ public class ElasticsearchOperationsTest {
|
||||
String range = "330mi"; // or 530km
|
||||
CriteriaQuery query = new CriteriaQuery(new Criteria("location").within(startLocation, range));
|
||||
|
||||
List<Conference> result = operations.queryForList(query, Conference.class);
|
||||
SearchHits<Conference> result = operations.search(query, Conference.class, IndexCoordinates.of("conference-index"));
|
||||
|
||||
assertThat(result, hasSize(2));
|
||||
assertThat(result).hasSize(2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,28 +15,28 @@
|
||||
*/
|
||||
package example.springdata.elasticsearch.conference;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.elasticsearch.ElasticsearchStatusException;
|
||||
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.core.convert.support.DefaultConversionService;
|
||||
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.ElasticsearchEntityMapper;
|
||||
import org.springframework.data.elasticsearch.core.EntityMapper;
|
||||
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations;
|
||||
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
|
||||
import org.springframework.data.elasticsearch.repository.config.EnableReactiveElasticsearchRepositories;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
@@ -54,17 +54,12 @@ class ApplicationConfiguration extends AbstractReactiveElasticsearchConfiguratio
|
||||
return ReactiveRestClients.create(ClientConfiguration.localhost());
|
||||
}
|
||||
|
||||
@Override
|
||||
public EntityMapper entityMapper() {
|
||||
return new ElasticsearchEntityMapper(elasticsearchMappingContext(),
|
||||
new DefaultConversionService());
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void insertDataSample() {
|
||||
|
||||
try {
|
||||
RestClients.create(ClientConfiguration.localhost()).rest().indices().create(new CreateIndexRequest("conference-index"), RequestOptions.DEFAULT);
|
||||
RestClients.create(ClientConfiguration.localhost()).rest().indices()
|
||||
.create(new CreateIndexRequest("conference-index"), RequestOptions.DEFAULT);
|
||||
} catch (IOException | ElasticsearchStatusException e) {
|
||||
// just ignore it
|
||||
}
|
||||
@@ -73,16 +68,22 @@ class ApplicationConfiguration extends AbstractReactiveElasticsearchConfiguratio
|
||||
repository.deleteAll().subscribe();
|
||||
|
||||
// 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()).then().as(StepVerifier::create).verifyComplete();
|
||||
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()).then().as(StepVerifier::create).verifyComplete();
|
||||
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())
|
||||
.then().as(StepVerifier::create).verifyComplete();
|
||||
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())
|
||||
.then().as(StepVerifier::create).verifyComplete();
|
||||
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()).then().as(StepVerifier::create).verifyComplete();
|
||||
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()).then().as(StepVerifier::create).verifyComplete();
|
||||
.keywords(Arrays.asList("cloud", "aws")).location(new GeoPoint(51.500152D, -0.126236D)).build()).then()
|
||||
.as(StepVerifier::create).verifyComplete();
|
||||
repository.save(Conference.builder().date("2014-10-04").name("JDD14 - Cracow")
|
||||
.keywords(Arrays.asList("java", "spring")).location(new GeoPoint(50.0646501D, 19.9449799)).build()).then().as(StepVerifier::create).verifyComplete();
|
||||
.keywords(Arrays.asList("java", "spring")).location(new GeoPoint(50.0646501D, 19.9449799)).build()).then()
|
||||
.as(StepVerifier::create).verifyComplete();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,13 +29,14 @@ import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
/**
|
||||
* Test case to show reactive Spring Data Elasticsearch repository functionality.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = ApplicationConfiguration.class)
|
||||
public class ReactiveElasticsearchRepositoryTest {
|
||||
|
||||
|
||||
@@ -49,14 +49,14 @@ class ApplicationConfiguration extends AbstractElasticsearchConfiguration {
|
||||
|
||||
@PreDestroy
|
||||
public void deleteIndex() {
|
||||
elasticsearchOperations.deleteIndex(Conference.class);
|
||||
elasticsearchOperations.getIndexOperations().refresh(Conference.class);
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void insertDataSample() {
|
||||
|
||||
repository.deleteAll();
|
||||
elasticsearchOperations.refresh(Conference.class);
|
||||
elasticsearchOperations.getIndexOperations().refresh(Conference.class);
|
||||
|
||||
// Save data sample
|
||||
repository.save(Conference.builder().date("2014-11-06").name("Spring eXchange 2014 - London")
|
||||
|
||||
@@ -15,21 +15,23 @@
|
||||
*/
|
||||
package example.springdata.elasticsearch.conference;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import example.springdata.elasticsearch.util.ElasticsearchAvailable;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.ClassRule;
|
||||
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;
|
||||
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;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
@@ -57,13 +59,13 @@ public class ElasticsearchOperationsTest {
|
||||
CriteriaQuery query = new CriteriaQuery(
|
||||
new Criteria("keywords").contains(expectedWord).and(new Criteria("date").greaterThanEqual(expectedDate)));
|
||||
|
||||
List<Conference> result = operations.queryForList(query, Conference.class);
|
||||
SearchHits<Conference> result = operations.search(query, Conference.class, IndexCoordinates.of("conference-index"));
|
||||
|
||||
assertThat(result, hasSize(3));
|
||||
assertThat(result).hasSize(3);
|
||||
|
||||
for (Conference conference : result) {
|
||||
assertThat(conference.getKeywords(), hasItem(expectedWord));
|
||||
assertThat(format.parse(conference.getDate()), greaterThan(format.parse(expectedDate)));
|
||||
for (SearchHit<Conference> conference : result) {
|
||||
assertThat(conference.getContent().getKeywords()).contains(expectedWord);
|
||||
assertThat(format.parse(conference.getContent().getDate())).isAfter(format.parse(expectedDate));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user