|
| 1 | +/* |
| 2 | + * Copyright 2014-2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package example.springdata.elasticsearch.conference; |
| 17 | + |
| 18 | +import java.util.Arrays; |
| 19 | + |
| 20 | +import javax.annotation.PostConstruct; |
| 21 | +import javax.annotation.PreDestroy; |
| 22 | + |
| 23 | +import org.elasticsearch.client.RestHighLevelClient; |
| 24 | +import org.springframework.beans.factory.annotation.Autowired; |
| 25 | +import org.springframework.context.annotation.Configuration; |
| 26 | +import org.springframework.data.elasticsearch.client.ClientConfiguration; |
| 27 | +import org.springframework.data.elasticsearch.client.RestClients; |
| 28 | +import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration; |
| 29 | +import org.springframework.data.elasticsearch.core.ElasticsearchOperations; |
| 30 | +import org.springframework.data.elasticsearch.core.geo.GeoPoint; |
| 31 | +import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; |
| 32 | + |
| 33 | +/** |
| 34 | + * @author Artur Konczak |
| 35 | + * @author Oliver Gierke |
| 36 | + * @author Christoph Strobl |
| 37 | + */ |
| 38 | +@Configuration |
| 39 | +@EnableElasticsearchRepositories |
| 40 | +class ApplicationConfiguration extends AbstractElasticsearchConfiguration { |
| 41 | + |
| 42 | + @Override |
| 43 | + public RestHighLevelClient elasticsearchClient() { |
| 44 | + return RestClients.create(ClientConfiguration.localhost()).rest(); |
| 45 | + } |
| 46 | + |
| 47 | + @Autowired ElasticsearchOperations elasticsearchOperations; |
| 48 | + @Autowired ConferenceRepository repository; |
| 49 | + |
| 50 | + @PreDestroy |
| 51 | + public void deleteIndex() { |
| 52 | + elasticsearchOperations.deleteIndex(Conference.class); |
| 53 | + } |
| 54 | + |
| 55 | + @PostConstruct |
| 56 | + public void insertDataSample() { |
| 57 | + |
| 58 | + repository.deleteAll(); |
| 59 | + elasticsearchOperations.refresh(Conference.class); |
| 60 | + |
| 61 | + // Save data sample |
| 62 | + repository.save(Conference.builder().date("2014-11-06").name("Spring eXchange 2014 - London") |
| 63 | + .keywords(Arrays.asList("java", "spring")).location(new GeoPoint(51.500152D, -0.126236D)).build()); |
| 64 | + repository.save(Conference.builder().date("2014-12-07").name("Scala eXchange 2014 - London") |
| 65 | + .keywords(Arrays.asList("scala", "play", "java")).location(new GeoPoint(51.500152D, -0.126236D)).build()); |
| 66 | + repository.save(Conference.builder().date("2014-11-20").name("Elasticsearch 2014 - Berlin") |
| 67 | + .keywords(Arrays.asList("java", "elasticsearch", "kibana")).location(new GeoPoint(52.5234051D, 13.4113999)) |
| 68 | + .build()); |
| 69 | + repository.save(Conference.builder().date("2014-11-12").name("AWS London 2014") |
| 70 | + .keywords(Arrays.asList("cloud", "aws")).location(new GeoPoint(51.500152D, -0.126236D)).build()); |
| 71 | + repository.save(Conference.builder().date("2014-10-04").name("JDD14 - Cracow") |
| 72 | + .keywords(Arrays.asList("java", "spring")).location(new GeoPoint(50.0646501D, 19.9449799)).build()); |
| 73 | + } |
| 74 | +} |
0 commit comments