#164 - Fix Elasticsearch example

Remove Boot Autoconfiguration, in-memory index and facets.
GeoPoint instead of String for location.

Original pull request: #165.
This commit is contained in:
Christoph Strobl
2016-03-21 08:05:59 +01:00
committed by Mark Paluch
parent fc5da90453
commit 09697b84f2
7 changed files with 187 additions and 78 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2015 the original author or authors.
* Copyright 2014-2016 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,26 +16,50 @@
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.EnableAutoConfiguration;
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
@EnableAutoConfiguration
@EnableElasticsearchRepositories
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) throws Exception {
return new ElasticsearchTemplate(client);
}
@PreDestroy
public void deleteIndex() {
operations.deleteIndex(Conference.class);
@@ -46,18 +70,19 @@ class ApplicationConfiguration {
// Remove all documents
repository.deleteAll();
operations.refresh(Conference.class, true);
operations.refresh(Conference.class);
// Save data sample
repository.save(Conference.builder().date("2014-11-06").name("Spring eXchange 2014 - London")
.keywords(Arrays.asList("java", "spring")).location("51.500152, -0.126236").build());
.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("51.500152, -0.126236").build());
.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("52.5234051, 13.4113999").build());
.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("51.500152, -0.126236").build());
.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("50.0646501, 19.9449799").build());
.keywords(Arrays.asList("java", "spring")).location(new GeoPoint(50.0646501D, 19.9449799)).build());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2015 the original author or authors.
* Copyright 2014-2016 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.
@@ -19,34 +19,36 @@ import static org.springframework.data.elasticsearch.annotations.FieldType.*;
import java.util.List;
import lombok.Data;
import lombok.experimental.Builder;
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.annotations.GeoPointField;
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
import lombok.Data;
import lombok.experimental.Builder;
/**
* @author Artur Konczak
* @author Oliver Gierke
* @auhtor Christoph Strobl
*/
@Data
@Builder
@Document(indexName = "conference-index", shards = 1, replicas = 0, indexStoreType = "memory", refreshInterval = "-1")
@Document(indexName = "conference-index", type = "geo-class-point-type", shards = 1, replicas = 0,
refreshInterval = "-1")
public class Conference {
private @Id String id;
private String name;
private @Field(type = Date) String date;
private @GeoPointField String location;
private GeoPoint location;
private List<String> 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, String location, List<String> keywords) {
public Conference(String id, String name, String date, GeoPoint location, List<String> keywords) {
this.id = id;
this.name = name;