diff --git a/SpringProject/MainApplication/src/it/java/com/gradproject2019/ElasticsearchIT.java b/SpringProject/MainApplication/src/it/java/com/gradproject2019/ElasticsearchIT.java new file mode 100644 index 0000000..73e4fe1 --- /dev/null +++ b/SpringProject/MainApplication/src/it/java/com/gradproject2019/ElasticsearchIT.java @@ -0,0 +1,141 @@ +package com.gradproject2019; + +import gradproject2019.conferences.data.ConferenceResponseDto; +import gradproject2019.conferences.service.ConferenceService; +import gradproject2019.elasticsearch.persistence.EsConference; +import gradproject2019.elasticsearch.repository.ConferenceSearchRepository; +import org.junit.Assert; +import org.junit.Before; +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.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.test.context.junit4.SpringRunner; + +import java.net.URI; +import java.net.URISyntaxException; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT) +public class ElasticsearchIT { + + @LocalServerPort + int testServerPort; + private String baseUri; + + @Before + public void setUp() { + baseUri = "http://localhost:" + testServerPort + "/conferences"; + } + + @Autowired + public TestRestTemplate restTemplate; + + @Autowired + private ConferenceService conferenceService; + + @Autowired + ConferenceSearchRepository repository; + +// @Autowired +// ElasticsearchTemplate template; + +// @Test +// public void shouldSaveEsConferenceToSearchRepository() { +// EsConference esConference = new EsConference(); +// esConference.setId(1L); +// esConference.setName("SophiaCon"); +// //esConference.setDateTime(Instant.now()); +// esConference.setCity("London"); +// esConference.setDescription("A conference"); +// esConference.setTopic("Sophia"); +// +// EsConference savedConference = repository.save(esConference); +// +// assertThat(savedConference).isSameAs(esConference); +// } +// +// @Test +// public void testFindAll() { +// Iterable esConference = repository.findAll(); +// Assert.assertTrue(esConference.iterator().hasNext()); +// } +// +// @Test +// public void testFindByTopic() { +// Page esConference = repository.findByTopic("Sophia", PageRequest.of(1, 1)); +// Assert.assertTrue(esConference.getTotalPages() > 0); +// +// } +// +// @Test +// public void shouldReturn200AndEmptyListWhenNoConferences() throws URISyntaxException { +// URI uri = new URI(baseUri); +// +// ResponseEntity> response = getConferenceList(uri); +// +// Assert.assertEquals(200, response.getStatusCodeValue()); +// Assert.assertEquals(true, response.getBody().isEmpty()); +// } +// +// @Test +// public void shouldReturn200AndListOfConferencesWhenConferenceExists() throws URISyntaxException { +// URI uri = new URI(baseUri); +// Conference addedConference = conferenceRepository.saveAndFlush(conference); +// +// ResponseEntity> response = getConferenceList(uri); +// +// Assert.assertEquals(200, response.getStatusCodeValue()); +// Assert.assertEquals(conference.getName(), response.getBody().get(0).getName()); +// Assert.assertEquals(addedConference.getId(),response.getBody().get(0).getId()); +// } + + @Test + public void testFindByTopic() throws URISyntaxException { + EsConference esConference = new EsConference(); + esConference.setId(1L); + esConference.setName("SophiaCon"); + //esConference.setDateTime(Instant.now()); + esConference.setCity("London"); + esConference.setDescription("A conference"); + esConference.setTopic("Sophia"); + + EsConference esConference2 = new EsConference(); + esConference2.setId(2L); + esConference2.setName("SophiaCon2"); + //esConference.setDateTime(Instant.now()); + esConference2.setCity("London2"); + esConference2.setDescription("A conference2"); + esConference2.setTopic("Sophia2"); + + + restTemplate.getRestTemplate().setRequestFactory(new HttpComponentsClientHttpRequestFactory()); + + EsConference savedConference = repository.save(esConference); + //EsConference savedConference2 = repository.save(esConference2); + URI uri = new URI(baseUri + "?topic=Sophia&page=1&size=10"); + //URI uri2 = new URI(baseUri ); + + + //List conferences = conferenceService.findByConferenceTopic("Sophia", 1, 1); + + ResponseEntity response = getEsConferenceList(uri); + + Assert.assertNotNull(response); + Assert.assertEquals(200, response.getStatusCodeValue()); + //Assert.assertEquals(1, response.size()); + //Assert.assertEquals(esConference.getName(), response.getBody().get(0).getName()); + } + + private ResponseEntity getEsConferenceList(URI uri) { + return restTemplate.exchange(uri, HttpMethod.GET, null, new ParameterizedTypeReference() {}); + } + +} + diff --git a/SpringProject/MainApplication/src/it/java/com/gradproject2019/auth/AuthControllerIT.java b/SpringProject/MainApplication/src/it/java/com/gradproject2019/auth/AuthControllerIT.java index 63e1b1c..cce72e1 100644 --- a/SpringProject/MainApplication/src/it/java/com/gradproject2019/auth/AuthControllerIT.java +++ b/SpringProject/MainApplication/src/it/java/com/gradproject2019/auth/AuthControllerIT.java @@ -1,7 +1,7 @@ package com.gradproject2019.auth; -import com.gradproject2019.auth.data.LoginDto; -import com.gradproject2019.auth.persistence.Token; +import gradproject2019.auth.data.LoginDto; +import gradproject2019.auth.persistence.Token; import com.gradproject2019.utils.ErrorEntity; import com.gradproject2019.utils.TestUtils; import org.junit.After; diff --git a/SpringProject/MainApplication/src/it/java/com/gradproject2019/conferences/ConferenceControllerIT.java b/SpringProject/MainApplication/src/it/java/com/gradproject2019/conferences/ConferenceControllerIT.java index 3b008c6..0755f64 100644 --- a/SpringProject/MainApplication/src/it/java/com/gradproject2019/conferences/ConferenceControllerIT.java +++ b/SpringProject/MainApplication/src/it/java/com/gradproject2019/conferences/ConferenceControllerIT.java @@ -1,9 +1,9 @@ package com.gradproject2019.conferences; -import com.gradproject2019.conferences.data.ConferencePatchRequestDto; -import com.gradproject2019.conferences.data.ConferenceRequestDto; -import com.gradproject2019.conferences.data.ConferenceResponseDto; -import com.gradproject2019.conferences.persistence.Conference; +import gradproject2019.conferences.data.ConferencePatchRequestDto; +import gradproject2019.conferences.data.ConferenceRequestDto; +import gradproject2019.conferences.data.ConferenceResponseDto; +import gradproject2019.conferences.persistence.Conference; import com.gradproject2019.utils.ErrorEntity; import com.gradproject2019.utils.TestUtils; import org.junit.After; diff --git a/SpringProject/MainApplication/src/it/java/com/gradproject2019/user/UserControllerIT.java b/SpringProject/MainApplication/src/it/java/com/gradproject2019/user/UserControllerIT.java index 5a28aaf..d5c3082 100644 --- a/SpringProject/MainApplication/src/it/java/com/gradproject2019/user/UserControllerIT.java +++ b/SpringProject/MainApplication/src/it/java/com/gradproject2019/user/UserControllerIT.java @@ -1,10 +1,10 @@ package com.gradproject2019.user; -import com.gradproject2019.auth.persistence.Token; -import com.gradproject2019.users.data.UserPatchRequestDto; -import com.gradproject2019.users.data.UserRequestDto; -import com.gradproject2019.users.data.UserResponseDto; -import com.gradproject2019.users.persistence.User; +import gradproject2019.auth.persistence.Token; +import gradproject2019.users.data.UserPatchRequestDto; +import gradproject2019.users.data.UserRequestDto; +import gradproject2019.users.data.UserResponseDto; +import gradproject2019.users.persistence.User; import com.gradproject2019.utils.ErrorEntity; import com.gradproject2019.utils.TestUtils; import org.junit.After; diff --git a/SpringProject/MainApplication/src/it/java/com/gradproject2019/userConference/UserConferenceControllerIT.java b/SpringProject/MainApplication/src/it/java/com/gradproject2019/userConference/UserConferenceControllerIT.java index e7fc33f..9291f52 100644 --- a/SpringProject/MainApplication/src/it/java/com/gradproject2019/userConference/UserConferenceControllerIT.java +++ b/SpringProject/MainApplication/src/it/java/com/gradproject2019/userConference/UserConferenceControllerIT.java @@ -1,9 +1,9 @@ package com.gradproject2019.userConference; -import com.gradproject2019.conferences.data.ConferenceResponseDto; -import com.gradproject2019.userConference.data.UserConferenceRequestDto; -import com.gradproject2019.userConference.data.UserConferenceResponseDto; -import com.gradproject2019.userConference.persistence.UserConference; +import gradproject2019.conferences.data.ConferenceResponseDto; +import gradproject2019.userConference.data.UserConferenceRequestDto; +import gradproject2019.userConference.data.UserConferenceResponseDto; +import gradproject2019.userConference.persistence.UserConference; import com.gradproject2019.utils.ErrorEntity; import com.gradproject2019.utils.TestUtils; import org.junit.After; @@ -22,7 +22,7 @@ import java.net.URISyntaxException; import java.util.List; -import static com.gradproject2019.userConference.data.UserConferenceRequestDto.UserConferenceRequestDtoBuilder.anUserConferenceRequestDto; +import static gradproject2019.userConference.data.UserConferenceRequestDto.UserConferenceRequestDtoBuilder.anUserConferenceRequestDto; import static org.springframework.http.HttpMethod.GET; import static org.springframework.http.HttpMethod.POST; diff --git a/SpringProject/MainApplication/src/it/java/com/gradproject2019/utils/TestUtils.java b/SpringProject/MainApplication/src/it/java/com/gradproject2019/utils/TestUtils.java index b79cfbd..4d5a44f 100644 --- a/SpringProject/MainApplication/src/it/java/com/gradproject2019/utils/TestUtils.java +++ b/SpringProject/MainApplication/src/it/java/com/gradproject2019/utils/TestUtils.java @@ -1,15 +1,16 @@ package com.gradproject2019.utils; -import com.gradproject2019.auth.persistence.Token; -import com.gradproject2019.auth.repository.AuthRepository; -import com.gradproject2019.auth.service.AuthService; -import com.gradproject2019.conferences.persistence.Conference; -import com.gradproject2019.conferences.repository.ConferenceRepository; -import com.gradproject2019.userConference.persistence.UserConference; -import com.gradproject2019.userConference.repository.UserConferenceRepository; -import com.gradproject2019.userConference.service.UserConferenceService; -import com.gradproject2019.users.persistence.User; -import com.gradproject2019.users.repository.UserRepository; +import gradproject2019.auth.persistence.Token; +import gradproject2019.auth.repository.AuthRepository; +import gradproject2019.auth.service.AuthService; +import gradproject2019.conferences.persistence.Conference; +import gradproject2019.conferences.repository.ConferenceRepository; +import gradproject2019.userConference.persistence.UserConference; +import gradproject2019.userConference.repository.UserConferenceRepository; +import gradproject2019.userConference.service.UserConferenceService; +import gradproject2019.users.persistence.User; +import gradproject2019.users.repository.UserRepository; +import gradproject2019.utils.AuthUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.http.HttpHeaders; diff --git a/SpringProject/MainApplication/src/main/curl/Index.json b/SpringProject/MainApplication/src/main/curl/Index.json new file mode 100644 index 0000000..c14a9f4 --- /dev/null +++ b/SpringProject/MainApplication/src/main/curl/Index.json @@ -0,0 +1,44 @@ + { + "settings": { + "index": { + "number_of_shards": 3, + "number_of_replicas": 2 + }, + "analysis": { + "normalizer": { + "standard_lowercase": { + "type": "custom", + "filter": ["lowercase"] + } + } + } + }, + "mappings": { + "properties": { + "name": { + "type": "text" + }, + "city": { + "type": "keyword" + }, + "description": { + "type": "text" + }, + "topic": { + "type": "text", + "analyser": "simple", + "fields": { + "keyword":{ + "type": "keyword", + "normalizer": "standard_lowercase" + } + } + }, + "dateTime": { + "type": "date" + } + } + } +} + + diff --git a/SpringProject/MainApplication/src/main/curl/Index.sh b/SpringProject/MainApplication/src/main/curl/Index.sh new file mode 100644 index 0000000..2e85562 --- /dev/null +++ b/SpringProject/MainApplication/src/main/curl/Index.sh @@ -0,0 +1 @@ +curl -X PUT "http://localhost:9200/mock_conferences" -H "Content-Type: application/json" --data-binary @index.json diff --git a/SpringProject/MainApplication/src/main/curl/MockConferences.json b/SpringProject/MainApplication/src/main/curl/MockConferences.json new file mode 100644 index 0000000..520c282 --- /dev/null +++ b/SpringProject/MainApplication/src/main/curl/MockConferences.json @@ -0,0 +1,22 @@ +[ + { + "name": "Sophia's Conference", + "city": "London", + "description": "A conference about Sophia", + "topic": "People" + }, + + { + "name": "Grace's Conference", + "city": "Sheffield", + "description": "All about why Sheffield is the best city in the world", + "topic": "Travel" + }, + + { + "name": "Karen's Conference", + "city": "Rome", + "description": "Its-a conference about-a pasta", + "topic": "Food" + } +] diff --git a/SpringProject/MainApplication/src/main/java/gradproject2019/conferences/controller/ConferenceController.java b/SpringProject/MainApplication/src/main/java/gradproject2019/conferences/controller/ConferenceController.java index 1e9550a..d9f084a 100644 --- a/SpringProject/MainApplication/src/main/java/gradproject2019/conferences/controller/ConferenceController.java +++ b/SpringProject/MainApplication/src/main/java/gradproject2019/conferences/controller/ConferenceController.java @@ -3,6 +3,7 @@ import gradproject2019.conferences.data.ConferencePatchRequestDto; import gradproject2019.conferences.data.ConferenceRequestDto; import gradproject2019.conferences.data.ConferenceResponseDto; + import gradproject2019.conferences.service.ConferenceService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; @@ -13,6 +14,8 @@ import java.util.List; import java.util.UUID; +import static org.hibernate.annotations.common.util.StringHelper.isNotEmpty; + @CrossOrigin(origins = "http://localhost:3000") @Controller @RequestMapping("/conferences") @@ -22,8 +25,32 @@ public class ConferenceController { private ConferenceService conferenceService; @GetMapping - public ResponseEntity> getAllConferences() { - List conferences = conferenceService.getAllConferences(); + public ResponseEntity> getAllConferences( + @RequestParam(required = false) String name, + @RequestParam(required = false) String city, + @RequestParam(required = false) String description, + @RequestParam(required = false) String topic, + @RequestParam(required = false, defaultValue = "0") Integer page, + @RequestParam(required = false, defaultValue = "500") Integer size) { + + List conferences; + + if (isNotEmpty(topic)) { + conferences = conferenceService.findByConferenceTopic(topic, page, size); + } + else if (isNotEmpty(name)) { + conferences = conferenceService.findByConferenceName(name, page, size); + } + else if (isNotEmpty(city)) { + conferences = conferenceService.findByConferenceCity(city, page, size); + } + else if (isNotEmpty(description)) { + conferences = conferenceService.findByConferenceDescription(description, page, size); + } + else { + conferences = conferenceService.getAllConferences(); + } + return ResponseEntity.ok(conferences); } diff --git a/SpringProject/MainApplication/src/main/java/gradproject2019/conferences/data/ConferenceResponseDto.java b/SpringProject/MainApplication/src/main/java/gradproject2019/conferences/data/ConferenceResponseDto.java index 803ecb8..2ee9194 100644 --- a/SpringProject/MainApplication/src/main/java/gradproject2019/conferences/data/ConferenceResponseDto.java +++ b/SpringProject/MainApplication/src/main/java/gradproject2019/conferences/data/ConferenceResponseDto.java @@ -1,6 +1,7 @@ package gradproject2019.conferences.data; import gradproject2019.conferences.persistence.Conference; +import gradproject2019.elasticsearch.persistence.EsConference; import org.springframework.validation.annotation.Validated; import javax.validation.constraints.NotNull; @@ -64,6 +65,17 @@ public ConferenceResponseDto from(Conference conference) { .build(); } + public ConferenceResponseDto from(EsConference esConference) { + return new ConferenceResponseDtoBuilder() + .withId(esConference.getId()) + .withName(esConference.getName()) +// .withDateTime(esConference.getDateTime()) //FIXME: add to EsConference + .withCity(esConference.getCity()) + .withDescription(esConference.getDescription()) + .withTopic(esConference.getTopic()) + .build(); + } + public static final class ConferenceResponseDtoBuilder { private Long id; private String name; diff --git a/SpringProject/MainApplication/src/main/java/gradproject2019/conferences/service/ConferenceService.java b/SpringProject/MainApplication/src/main/java/gradproject2019/conferences/service/ConferenceService.java index 11992c2..321ae57 100644 --- a/SpringProject/MainApplication/src/main/java/gradproject2019/conferences/service/ConferenceService.java +++ b/SpringProject/MainApplication/src/main/java/gradproject2019/conferences/service/ConferenceService.java @@ -18,4 +18,12 @@ public interface ConferenceService { void deleteConference(UUID token, Long conferenceId); ConferenceResponseDto editConference(UUID token, Long conferenceId, ConferencePatchRequestDto conferencePatchRequestDto); + + List findByConferenceName(String name, Integer page, Integer size); + + List findByConferenceCity(String city, Integer page, Integer size); + + List findByConferenceDescription(String description, Integer page, Integer size); + + List findByConferenceTopic(String topic, Integer page, Integer size); } \ No newline at end of file diff --git a/SpringProject/MainApplication/src/main/java/gradproject2019/conferences/service/ConferenceServiceImpl.java b/SpringProject/MainApplication/src/main/java/gradproject2019/conferences/service/ConferenceServiceImpl.java index ebacbe7..a26bb58 100644 --- a/SpringProject/MainApplication/src/main/java/gradproject2019/conferences/service/ConferenceServiceImpl.java +++ b/SpringProject/MainApplication/src/main/java/gradproject2019/conferences/service/ConferenceServiceImpl.java @@ -10,6 +10,10 @@ import gradproject2019.conferences.persistence.Conference; import gradproject2019.conferences.repository.ConferenceRepository; import gradproject2019.userConference.service.UserConferenceServiceImpl; +import gradproject2019.elasticsearch.repository.ConferenceSearchRepository; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; @@ -27,13 +31,15 @@ public class ConferenceServiceImpl implements ConferenceService { private ConferenceRepository conferenceRepository; private AuthServiceImpl authServiceImpl; + private ConferenceSearchRepository conferenceSearchRepository; private UserConferenceServiceImpl userConferenceServiceImpl; - public ConferenceServiceImpl(ConferenceRepository conferenceRepository, AuthServiceImpl authServiceImpl, @Lazy UserConferenceServiceImpl userConferenceServiceImpl) { + public ConferenceServiceImpl(ConferenceRepository conferenceRepository, AuthServiceImpl authServiceImpl, ConferenceSearchRepository conferenceSearchRepository, @Lazy UserConferenceServiceImpl userConferenceServiceImpl) { this.conferenceRepository = conferenceRepository; this.authServiceImpl = authServiceImpl; this.userConferenceServiceImpl = userConferenceServiceImpl; + this.conferenceSearchRepository = conferenceSearchRepository; } @Override @@ -79,6 +85,39 @@ public ConferenceResponseDto saveConference(UUID token, ConferenceRequestDto con } + @Override + + public List findByConferenceName(String name, Integer page, Integer size) { + return conferenceSearchRepository.findByName(name,PageRequest.of(page, size)) + .get() + .map(c -> new ConferenceResponseDto().from(c)) + .collect(Collectors.toList()); + } + + @Override + public List findByConferenceCity(String city, Integer page, Integer size) { + return conferenceSearchRepository.findByCity(city, PageRequest.of(page, size)) + .get() + .map(c -> new ConferenceResponseDto().from(c)) + .collect(Collectors.toList()); + } + + @Override + public List findByConferenceDescription(String description, Integer page, Integer size) { + return conferenceSearchRepository.findByDescription(description, PageRequest.of(page, size)) + .get() + .map(c -> new ConferenceResponseDto().from(c)) + .collect(Collectors.toList()); + } + + @Override + public List findByConferenceTopic(String topic, Integer page, Integer size) { + return conferenceSearchRepository.findByTopic(topic, PageRequest.of(page, size)) + .get() + .map(c -> new ConferenceResponseDto().from(c)) + .collect(Collectors.toList()); + } + private void checkNotInPast(Instant dateTime) { if (dateTime == null) { return; diff --git a/SpringProject/MainApplication/src/main/java/gradproject2019/elasticsearch/persistence/EsConference.java b/SpringProject/MainApplication/src/main/java/gradproject2019/elasticsearch/persistence/EsConference.java new file mode 100644 index 0000000..0907064 --- /dev/null +++ b/SpringProject/MainApplication/src/main/java/gradproject2019/elasticsearch/persistence/EsConference.java @@ -0,0 +1,81 @@ +package gradproject2019.elasticsearch.persistence; + +import org.springframework.data.annotation.Id; +import org.springframework.data.elasticsearch.annotations.Document; + +@Document(indexName = "conferences", type = "_doc") +public class EsConference { + @Id + private Long id; + private String name; + + //@JsonFormat(shape = JsonFormat.Shape.STRING, pattern ="yyyy-MM-dd'T'HH:mm:ss.SSSZZ", timezone = "UTC") + //private Instant dateTime; + + private String city; + private String description; + private String topic; + + public EsConference() { + } + + public EsConference(Long id, String name, String city, String description, String topic) { + this.id = id; + this.name = name; + //this.dateTime = dateTime; + this.city = city; + this.description = description; + this.topic = topic; + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + //public Instant getDateTime() { + //return dateTime; + //} + + public String getCity() { + return city; + } + + public String getDescription() { + return description; + } + + public String getTopic() { + return topic; + } + + public void setId(Long id) { + this.id = id; + } + + public void setName(String name) { + this.name = name; + } + +// public void setDateTime(Instant dateTime) { +// this.dateTime = dateTime; +// } + + public void setCity(String city) { + this.city = city; + } + + public void setDescription(String description) { + this.description = description; + } + + public void setTopic(String topic) { + this.topic = topic; + } + +} + + diff --git a/SpringProject/MainApplication/src/main/java/gradproject2019/elasticsearch/persistence/SearchConfig.java b/SpringProject/MainApplication/src/main/java/gradproject2019/elasticsearch/persistence/SearchConfig.java new file mode 100644 index 0000000..b4c53cc --- /dev/null +++ b/SpringProject/MainApplication/src/main/java/gradproject2019/elasticsearch/persistence/SearchConfig.java @@ -0,0 +1,119 @@ +package gradproject2019.elasticsearch.persistence; + + +import org.elasticsearch.client.RestHighLevelClient; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +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.core.ElasticsearchOperations; +import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; +import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; + + + +@Configuration +@EnableElasticsearchRepositories(basePackages = "com.gradproject2019.conferences.repository") +@ComponentScan(basePackages = "com.gradproject2019.conferences.service") +public class SearchConfig { + @Value("${spring.data.elasticsearch.cluster-nodes}") + private String esHost; + + @Bean + public RestHighLevelClient client() { + ClientConfiguration clientConfiguration = ClientConfiguration.builder() + .connectedTo(esHost) + .build(); + + return RestClients.create(clientConfiguration).rest(); + } + + @Bean + public ElasticsearchOperations elasticsearchTemplate() { + return new ElasticsearchRestTemplate(client()); + } + } + + + +// @Bean +// @Override +// public EntityMapper EntityMapper() { +// ElasticsearchEntityMapper entityMapper = new ElasticsearchEntityMapper( +// elasticsearchMappingContext(), new DefaultConversionService()); +// entityMapper.setConversions(elasticsearchCustomConversions()); +// +// return entityMapper; +// } +// +// @Bean +// @Override +// public ElasticsearchCustomConversions elasticsearchCustomConversions() { +// return new ElasticsearchCustomConversions( +// Arrays.asList(new AddressToMap(), new MapToAddress())); +// } +// +// @WritingConverter +// static class AddressToMap implements Converter> { +// +// @Override +// public Map convert(Address source) { +// +// LinkedHashMap target = new LinkedHashMap<>(); +// target.put("ciudad", source.getCity()); +// // ... +// +// return target; +// } +// } +// +// @ReadingConverter +// static class MapToAddress implements Converter, Address> { +// +// @Override +// public Address convert(Map source) { +// +// // ... +// return address; +// } +// } +// +// +// +// +// +// @Bean +// @Primary +// public ElasticsearchOperations elasticsearchTemplate(final JestClient jestClient, +// final ElasticsearchConverter elasticsearchConverter, +// final SimpleElasticsearchMappingContext simpleElasticsearchMappingContext, EntityMapper mapper) { +// return new JestElasticsearchTemplate(jestClient, elasticsearchConverter, +// new DefaultJestResultsMapper(simpleElasticsearchMappingContext, mapper)); +// } +// +// public class CustomEntityMapper implements EntityMapper { +// +// private ObjectMapper mapper; +// +// @Autowired +// public CustomEntityMapper() { +// this.mapper = new ObjectMapper(); +// mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); +// mapper.disable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); +// mapper.registerModule(new JavaTimeModule()); +// } +// +// @Override +// public String mapToString(Object object) throws IOException { +// return mapper.writeValueAsString(object); +// } +// +// @Override +// public T mapToObject(String source, Class clazz) throws IOException { +// return mapper.readValue(source, clazz); +// } +// +// } +//} diff --git a/SpringProject/MainApplication/src/main/java/gradproject2019/elasticsearch/repository/ConferenceSearchRepository.java b/SpringProject/MainApplication/src/main/java/gradproject2019/elasticsearch/repository/ConferenceSearchRepository.java new file mode 100644 index 0000000..50f5fb8 --- /dev/null +++ b/SpringProject/MainApplication/src/main/java/gradproject2019/elasticsearch/repository/ConferenceSearchRepository.java @@ -0,0 +1,18 @@ +package gradproject2019.elasticsearch.repository; + +import gradproject2019.elasticsearch.persistence.EsConference; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; + +public interface ConferenceSearchRepository extends ElasticsearchRepository { + + Page findByName(String name, Pageable pageable); + + Page findByCity(String city, Pageable pageable); + + Page findByDescription(String description, Pageable pageable); + + Page findByTopic(String topic, Pageable pageable); + +} diff --git a/SpringProject/MainApplication/src/main/resources/application.yml b/SpringProject/MainApplication/src/main/resources/application.yml index f23f009..51ae13f 100644 --- a/SpringProject/MainApplication/src/main/resources/application.yml +++ b/SpringProject/MainApplication/src/main/resources/application.yml @@ -17,6 +17,11 @@ spring: pattern: console: '%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n' + data: + elasticsearch: + cluster-name: docker-cluster + cluster-nodes: localhost:9200 + jpa: hibernate: ddl-auto: create-drop diff --git a/SpringProject/docker-compose.yml b/SpringProject/docker-compose.yml new file mode 100644 index 0000000..5a50e56 --- /dev/null +++ b/SpringProject/docker-compose.yml @@ -0,0 +1,16 @@ +version: '2.0' +services: + elasticsearch: + image: docker.elastic.co/elasticsearch/elasticsearch:7.1.0 + container_name: com.gradproject2019.elasticsearch + environment: + - node.name=es + - cluster.name=docker-cluster + - discovery.type=single-node + - "ES_JAVA_OPTS=-Xms512m -Xmx512m" + ports: + - "9200:9200" + - "9300:9300" + expose: + - "9200" + - "9300" \ No newline at end of file diff --git a/SpringProject/pom.xml b/SpringProject/pom.xml index 5b35969..fb16e87 100644 --- a/SpringProject/pom.xml +++ b/SpringProject/pom.xml @@ -1,26 +1,21 @@ - 4.0.0 - pom - - com.gradproject2019 - SpringProject - 0.0.1-SNAPSHOT - SpringProject - Project for Spring Boot - - - ElasticSearch - MainApplication - - - - org.springframework.boot - spring-boot-starter-parent - 2.1.9.RELEASE - - - - - \ No newline at end of file + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + pom + com.gradproject2019 + SpringProject + 0.0.1-SNAPSHOT + SpringProject + Project for Spring Boot + + ElasticSearch + MainApplication + + + org.springframework.boot + spring-boot-starter-parent + 2.1.9.RELEASE + + +