From 06741bf1feebd21c7f66ac91a6d6fe80cec218e6 Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Thu, 5 Dec 2024 11:57:43 +0000 Subject: [PATCH 1/2] feat : upgrade to spring boot 3.4.0 --- boot-mongodb-elasticsearch/pom.xml | 6 +- .../config/DataStoreConfiguration.java | 2 +- .../example/mongoes/config/Initializer.java | 6 +- .../example/mongoes/config/SwaggerConfig.java | 4 +- .../example/mongoes/config/WebFluxConfig.java | 11 +- .../com/example/mongoes/document/Address.java | 2 - .../CustomRestaurantESRepositoryImpl.java | 194 +++++++++--------- .../web/model/RestaurantRequestTest.java | 46 +++++ 8 files changed, 164 insertions(+), 107 deletions(-) create mode 100644 boot-mongodb-elasticsearch/src/test/java/com/example/mongoes/web/model/RestaurantRequestTest.java diff --git a/boot-mongodb-elasticsearch/pom.xml b/boot-mongodb-elasticsearch/pom.xml index 86191b5eb..8d8304073 100644 --- a/boot-mongodb-elasticsearch/pom.xml +++ b/boot-mongodb-elasticsearch/pom.xml @@ -5,7 +5,7 @@ org.springframework.boot spring-boot-starter-parent - 3.3.6 + 3.4.0 com.example.mongoes @@ -19,7 +19,7 @@ UTF-8 21 - 2.6.0 + 2.7.0 ${project.build.directory}/test-results 2.43.0 @@ -229,7 +229,7 @@ - 1.23.0 + 1.25.0 diff --git a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/config/DataStoreConfiguration.java b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/config/DataStoreConfiguration.java index cfb12a5d8..d66c29e90 100644 --- a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/config/DataStoreConfiguration.java +++ b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/config/DataStoreConfiguration.java @@ -25,7 +25,7 @@ @ComponentScan.Filter( type = FilterType.ASSIGNABLE_TYPE, value = ReactiveElasticsearchRepository.class)) -public class DataStoreConfiguration { +class DataStoreConfiguration { @Bean ReactiveMongoTransactionManager transactionManager(ReactiveMongoDatabaseFactory factory) { diff --git a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/config/Initializer.java b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/config/Initializer.java index e2239596b..bc3b9e417 100644 --- a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/config/Initializer.java +++ b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/config/Initializer.java @@ -3,7 +3,6 @@ import com.example.mongoes.utils.AppConstants; import com.example.mongoes.web.service.RestaurantService; import java.io.IOException; -import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.context.event.ApplicationStartedEvent; @@ -12,13 +11,16 @@ import org.springframework.stereotype.Component; @Component -@RequiredArgsConstructor @Slf4j @Profile(AppConstants.PROFILE_NOT_TEST) public class Initializer implements CommandLineRunner { private final RestaurantService restaurantService; + public Initializer(RestaurantService restaurantService) { + this.restaurantService = restaurantService; + } + @Override public void run(String... args) throws IOException { log.info("Running Initializer....."); diff --git a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/config/SwaggerConfig.java b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/config/SwaggerConfig.java index 90333d15e..9c2740590 100644 --- a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/config/SwaggerConfig.java +++ b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/config/SwaggerConfig.java @@ -7,9 +7,9 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -@Configuration +@Configuration(proxyBeanMethods = false) @OpenAPIDefinition(info = @Info(title = "spring-boot-mongodb-elasticsearch", version = "v1")) -public class SwaggerConfig { +class SwaggerConfig { @Bean OpenAPI openAPI() { diff --git a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/config/WebFluxConfig.java b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/config/WebFluxConfig.java index af97f6cd3..3ba08ee31 100644 --- a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/config/WebFluxConfig.java +++ b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/config/WebFluxConfig.java @@ -1,16 +1,21 @@ package com.example.mongoes.config; -import lombok.RequiredArgsConstructor; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.data.web.config.SpringDataJacksonConfiguration; import org.springframework.web.reactive.config.CorsRegistry; import org.springframework.web.reactive.config.WebFluxConfigurer; -@Configuration -@RequiredArgsConstructor +@Configuration(proxyBeanMethods = false) +@Import(SpringDataJacksonConfiguration.class) public class WebFluxConfig implements WebFluxConfigurer { private final ApplicationProperties properties; + public WebFluxConfig(ApplicationProperties properties) { + this.properties = properties; + } + @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping(properties.getCors().getPathPattern()) diff --git a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/document/Address.java b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/document/Address.java index 2fb4cb54f..b9a102821 100644 --- a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/document/Address.java +++ b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/document/Address.java @@ -1,6 +1,5 @@ package com.example.mongoes.document; -import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @@ -29,7 +28,6 @@ public class Address { * * */ - @JsonDeserialize(as = Point.class) @GeoSpatialIndexed(type = GeoSpatialIndexType.GEO_2DSPHERE) @GeoPointField @org.springframework.data.elasticsearch.annotations.Field("coord") diff --git a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/elasticsearch/repository/CustomRestaurantESRepositoryImpl.java b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/elasticsearch/repository/CustomRestaurantESRepositoryImpl.java index 9f88494c6..dbeebc086 100644 --- a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/elasticsearch/repository/CustomRestaurantESRepositoryImpl.java +++ b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/elasticsearch/repository/CustomRestaurantESRepositoryImpl.java @@ -7,11 +7,9 @@ import co.elastic.clients.elasticsearch._types.query_dsl.Operator; import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders; import co.elastic.clients.elasticsearch._types.query_dsl.TextQueryType; -import co.elastic.clients.json.JsonData; import com.example.mongoes.document.Restaurant; import java.util.ArrayList; import java.util.List; -import lombok.RequiredArgsConstructor; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; @@ -27,7 +25,6 @@ import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -@RequiredArgsConstructor public class CustomRestaurantESRepositoryImpl implements CustomRestaurantESRepository { private static final String BOROUGH = "borough"; @@ -35,6 +32,11 @@ public class CustomRestaurantESRepositoryImpl implements CustomRestaurantESRepos private static final int PAGE_SIZE = 1_000; private final ReactiveElasticsearchOperations reactiveElasticsearchOperations; + public CustomRestaurantESRepositoryImpl( + ReactiveElasticsearchOperations reactiveElasticsearchOperations) { + this.reactiveElasticsearchOperations = reactiveElasticsearchOperations; + } + @Override public Flux> searchWithin( GeoPoint geoPoint, Double distance, String unit) { @@ -232,10 +234,15 @@ public Mono> searchRestaurantIdRange( .withQuery( QueryBuilders.range( rangeBuilder -> - rangeBuilder - .field("restaurant_id") - .lte(JsonData.of(upperLimit)) - .gte(JsonData.of(lowerLimit)))) + rangeBuilder.term( + builder -> + builder.field("restaurant_id") + .lte( + String.valueOf( + upperLimit)) + .gte( + String.valueOf( + lowerLimit))))) .build(); query.setPageable(pageable); @@ -250,102 +257,101 @@ public Mono> searchDateRange( .withQuery( QueryBuilders.range( rangeBuilder -> - rangeBuilder - .field("grades.date") - .lte(JsonData.of(toDate)) - .gte(JsonData.of(fromDate)))) + rangeBuilder.date( + builder -> + builder.field("grades.date") + .lte(toDate) + .gte(fromDate)))) .build(); query.setPageable(pageable); return reactiveElasticsearchOperations.searchForPage(query, Restaurant.class); } - /** + * below is the console query * - * below is the console query {@snippet : - - """ POST /restaurant/_search?size=15&pretty - { - "query": { - "multi_match": { - "query": "Pizza", - "fields": [ - "restautant_name", - "borough", - "cuisine" - ], - "operator": "or" - } - }, - "aggs": { - "MyCuisine": { - "terms": { - "field": "cuisine", "size": 1000, "order": { - "_count": "desc" - } - } - }, - "MyBorough": { - "terms": { - "field": "borough" , "size": 1000 - } - }, - "MyDateRange": { - "date_range": { - "field": "grades.date", - "format": "dd-MM-yyy'T'hh:mm:ss", - "ranges": [ - { - "key": "Older", - "to": "now-12y-1d/y" - }, - { - "from": "now-12y/y", - "to": "now-11y/y" - }, - { - "from": "now-11y/y", - "to": "now-10y/y" - }, - { - "from": "now-10y/y", - "to": "now-9y/y" - }, - { - "from": "now-9y/y", - "to": "now-8y/y" - }, - { - "from": "now-8y/y", - "to": "now-7y/y" - }, - { - "from": "now-7y/y", - "to": "now-6y/y" - }, - { - "from": "now-6y/y", - "to": "now-5y/y" - }, - { - "from": "now-5y/y", - "to": "now-4y/y" - }, - { - "key": "Newer", - "from": "now-0y/y", - "to": "now/d" - } - ] - } - } - } - } - """; - } - * + * {@snippet : * + * """ POST /restaurant/_search?size=15&pretty + * { + * "query": { + * "multi_match": { + * "query": "Pizza", + * "fields": [ + * "restautant_name", + * "borough", + * "cuisine" + * ], + * "operator": "or" + * } + * }, + * "aggs": { + * "MyCuisine": { + * "terms": { + * "field": "cuisine", "size": 1000, "order": { + * "_count": "desc" + * } + * } + * }, + * "MyBorough": { + * "terms": { + * "field": "borough" , "size": 1000 + * } + * }, + * "MyDateRange": { + * "date_range": { + * "field": "grades.date", + * "format": "dd-MM-yyy'T'hh:mm:ss", + * "ranges": [ + * { + * "key": "Older", + * "to": "now-12y-1d/y" + * }, + * { + * "from": "now-12y/y", + * "to": "now-11y/y" + * }, + * { + * "from": "now-11y/y", + * "to": "now-10y/y" + * }, + * { + * "from": "now-10y/y", + * "to": "now-9y/y" + * }, + * { + * "from": "now-9y/y", + * "to": "now-8y/y" + * }, + * { + * "from": "now-8y/y", + * "to": "now-7y/y" + * }, + * { + * "from": "now-7y/y", + * "to": "now-6y/y" + * }, + * { + * "from": "now-6y/y", + * "to": "now-5y/y" + * }, + * { + * "from": "now-5y/y", + * "to": "now-4y/y" + * }, + * { + * "key": "Newer", + * "from": "now-0y/y", + * "to": "now/d" + * } + * ] + * } + * } + * } + * } + * """; + * } */ @Override public Mono> aggregateSearch( diff --git a/boot-mongodb-elasticsearch/src/test/java/com/example/mongoes/web/model/RestaurantRequestTest.java b/boot-mongodb-elasticsearch/src/test/java/com/example/mongoes/web/model/RestaurantRequestTest.java new file mode 100644 index 000000000..58d40c8b0 --- /dev/null +++ b/boot-mongodb-elasticsearch/src/test/java/com/example/mongoes/web/model/RestaurantRequestTest.java @@ -0,0 +1,46 @@ +package com.example.mongoes.web.model; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.example.mongoes.document.Address; +import com.example.mongoes.document.Grades; +import java.io.IOException; +import java.time.LocalDateTime; +import java.util.List; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.json.JsonTest; +import org.springframework.boot.test.json.JacksonTester; +import org.springframework.boot.test.json.JsonContent; +import org.springframework.context.annotation.Import; +import org.springframework.data.geo.Point; +import org.springframework.data.web.config.SpringDataJacksonConfiguration; + +@JsonTest +@Import(SpringDataJacksonConfiguration.class) +class RestaurantRequestTest { + + @Autowired private JacksonTester jacksonTester; + + @Test + void serializeInCorrectFormat() throws IOException { + Address address = new Address(); + address.setLocation(new Point(-73.9, 40.8)); + Grades grade = new Grades("A", LocalDateTime.of(2022, 1, 1, 1, 1, 1), 15); + Grades grade1 = new Grades("B", LocalDateTime.of(2022, 3, 31, 23, 59, 59), 15); + RestaurantRequest restaurantRequest = + new RestaurantRequest( + 1L, + "junitRestaurant", + "junitBorough", + "junitCuisine", + address, + List.of(grade, grade1)); + + JsonContent json = jacksonTester.write(restaurantRequest); + assertThat(json.getJson()).isNotNull(); + + RestaurantRequest orderRequest = jacksonTester.parseObject(json.getJson()); + assertThat(orderRequest).isNotNull(); + } +} From 0ad02e5c9da19cda7cfa6a0f19cf3343549742b0 Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Thu, 5 Dec 2024 12:28:28 +0000 Subject: [PATCH 2/2] feat : remove lombok --- boot-mongodb-elasticsearch/pom.xml | 12 +-- .../mongoes/config/ApplicationProperties.java | 51 ++++++++++- .../example/mongoes/config/Initializer.java | 5 +- .../com/example/mongoes/document/Address.java | 49 ++++++++-- .../mongoes/document/ChangeStreamResume.java | 20 ++++- .../com/example/mongoes/document/Grades.java | 48 ++++++++-- .../example/mongoes/document/Restaurant.java | 89 +++++++++++++++++-- .../CustomRestaurantESRepositoryImpl.java | 13 +-- ...ustomChangeStreamResumeRepositoryImpl.java | 6 +- .../example/mongoes/utils/DateUtility.java | 4 +- .../web/controller/RestaurantController.java | 6 +- .../web/controller/SearchController.java | 6 +- .../web/service/RestaurantService.java | 19 ++-- .../mongoes/web/service/SearchService.java | 8 +- 14 files changed, 275 insertions(+), 61 deletions(-) diff --git a/boot-mongodb-elasticsearch/pom.xml b/boot-mongodb-elasticsearch/pom.xml index 8d8304073..86ce2a611 100644 --- a/boot-mongodb-elasticsearch/pom.xml +++ b/boot-mongodb-elasticsearch/pom.xml @@ -53,11 +53,7 @@ org.springframework.boot spring-boot-starter-webflux - - org.glassfish.jaxb - jaxb-runtime - provided - + org.springframework.boot spring-boot-starter-data-mongodb-reactive @@ -78,11 +74,7 @@ spring-boot-configuration-processor true - - org.projectlombok - lombok - true - + org.springdoc diff --git a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/config/ApplicationProperties.java b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/config/ApplicationProperties.java index 508f35d24..45681657b 100644 --- a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/config/ApplicationProperties.java +++ b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/config/ApplicationProperties.java @@ -1,19 +1,64 @@ package com.example.mongoes.config; -import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; -@Data @ConfigurationProperties("application") public class ApplicationProperties { private Cors cors = new Cors(); - @Data + public Cors getCors() { + return cors; + } + + public void setCors(Cors cors) { + this.cors = cors; + } + public static class Cors { private String pathPattern = "/api/**"; private String allowedMethods = "*"; private String allowedHeaders = "*"; private String allowedOriginPatterns = "*"; private boolean allowCredentials = true; + + public String getPathPattern() { + return pathPattern; + } + + public void setPathPattern(String pathPattern) { + this.pathPattern = pathPattern; + } + + public String getAllowedMethods() { + return allowedMethods; + } + + public void setAllowedMethods(String allowedMethods) { + this.allowedMethods = allowedMethods; + } + + public String getAllowedHeaders() { + return allowedHeaders; + } + + public void setAllowedHeaders(String allowedHeaders) { + this.allowedHeaders = allowedHeaders; + } + + public String getAllowedOriginPatterns() { + return allowedOriginPatterns; + } + + public void setAllowedOriginPatterns(String allowedOriginPatterns) { + this.allowedOriginPatterns = allowedOriginPatterns; + } + + public boolean isAllowCredentials() { + return allowCredentials; + } + + public void setAllowCredentials(boolean allowCredentials) { + this.allowCredentials = allowCredentials; + } } } diff --git a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/config/Initializer.java b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/config/Initializer.java index bc3b9e417..a02cd9708 100644 --- a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/config/Initializer.java +++ b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/config/Initializer.java @@ -3,7 +3,8 @@ import com.example.mongoes.utils.AppConstants; import com.example.mongoes.web.service.RestaurantService; import java.io.IOException; -import lombok.extern.slf4j.Slf4j; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.context.annotation.Profile; @@ -11,10 +12,10 @@ import org.springframework.stereotype.Component; @Component -@Slf4j @Profile(AppConstants.PROFILE_NOT_TEST) public class Initializer implements CommandLineRunner { + private static final Logger log = LoggerFactory.getLogger(Initializer.class); private final RestaurantService restaurantService; public Initializer(RestaurantService restaurantService) { diff --git a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/document/Address.java b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/document/Address.java index b9a102821..d2e991859 100644 --- a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/document/Address.java +++ b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/document/Address.java @@ -1,17 +1,12 @@ package com.example.mongoes.document; -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; +import java.util.StringJoiner; import org.springframework.data.elasticsearch.annotations.GeoPointField; import org.springframework.data.geo.Point; import org.springframework.data.mongodb.core.index.GeoSpatialIndexType; import org.springframework.data.mongodb.core.index.GeoSpatialIndexed; import org.springframework.data.mongodb.core.mapping.Field; -@Data -@AllArgsConstructor -@NoArgsConstructor public class Address { private String building; @@ -37,4 +32,46 @@ public class Address { private String street; private Integer zipcode; + + public String getBuilding() { + return building; + } + + public void setBuilding(String building) { + this.building = building; + } + + public Point getLocation() { + return location; + } + + public void setLocation(Point location) { + this.location = location; + } + + public String getStreet() { + return street; + } + + public void setStreet(String street) { + this.street = street; + } + + public Integer getZipcode() { + return zipcode; + } + + public void setZipcode(Integer zipcode) { + this.zipcode = zipcode; + } + + @Override + public String toString() { + return new StringJoiner(", ", Address.class.getSimpleName() + "[", "]") + .add("building='" + building + "'") + .add("location=" + location) + .add("street='" + street + "'") + .add("zipcode=" + zipcode) + .toString(); + } } diff --git a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/document/ChangeStreamResume.java b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/document/ChangeStreamResume.java index 4cb847359..e58c4f508 100644 --- a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/document/ChangeStreamResume.java +++ b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/document/ChangeStreamResume.java @@ -1,17 +1,29 @@ package com.example.mongoes.document; -import lombok.Getter; -import lombok.Setter; import org.bson.BsonTimestamp; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; @Document -@Getter -@Setter public class ChangeStreamResume { @Id private String id; private BsonTimestamp resumeTimestamp; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public BsonTimestamp getResumeTimestamp() { + return resumeTimestamp; + } + + public void setResumeTimestamp(BsonTimestamp resumeTimestamp) { + this.resumeTimestamp = resumeTimestamp; + } } diff --git a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/document/Grades.java b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/document/Grades.java index 8b102b7d5..283c5c3e3 100644 --- a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/document/Grades.java +++ b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/document/Grades.java @@ -3,15 +3,10 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; import java.time.LocalDateTime; -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; +import java.util.StringJoiner; import org.springframework.data.elasticsearch.annotations.Field; import org.springframework.data.elasticsearch.annotations.FieldType; -@Data -@AllArgsConstructor -@NoArgsConstructor public class Grades { private String grade; @@ -23,4 +18,45 @@ public class Grades { private LocalDateTime date; private Integer score; + + public Grades() {} + + public Grades(String grade, LocalDateTime date, Integer score) { + this.grade = grade; + this.date = date; + this.score = score; + } + + public LocalDateTime getDate() { + return date; + } + + public void setDate(LocalDateTime date) { + this.date = date; + } + + public String getGrade() { + return grade; + } + + public void setGrade(String grade) { + this.grade = grade; + } + + public Integer getScore() { + return score; + } + + public void setScore(Integer score) { + this.score = score; + } + + @Override + public String toString() { + return new StringJoiner(", ", Grades.class.getSimpleName() + "[", "]") + .add("date=" + date) + .add("grade='" + grade + "'") + .add("score=" + score) + .toString(); + } } diff --git a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/document/Restaurant.java b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/document/Restaurant.java index f19f29450..64eeac843 100644 --- a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/document/Restaurant.java +++ b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/document/Restaurant.java @@ -4,10 +4,7 @@ import jakarta.validation.constraints.NotBlank; import java.util.ArrayList; import java.util.List; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; -import lombok.ToString; +import java.util.StringJoiner; import org.springframework.data.annotation.Id; import org.springframework.data.annotation.TypeAlias; import org.springframework.data.annotation.Version; @@ -17,14 +14,10 @@ import org.springframework.data.mongodb.core.index.Indexed; import org.springframework.data.mongodb.core.mapping.Document; -@Getter -@Setter @org.springframework.data.elasticsearch.annotations.Document( indexName = AppConstants.RESTAURANT_COLLECTION) @Document(collection = AppConstants.RESTAURANT_COLLECTION) -@NoArgsConstructor @TypeAlias(AppConstants.RESTAURANT_COLLECTION) -@ToString @Setting(shards = 3) public class Restaurant { @@ -53,4 +46,84 @@ public class Restaurant { private List grades = new ArrayList<>(); @Version private Long version; + + public Restaurant() {} + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Long getRestaurantId() { + return restaurantId; + } + + public void setRestaurantId(Long restaurantId) { + this.restaurantId = restaurantId; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Address getAddress() { + return address; + } + + public void setAddress(Address address) { + this.address = address; + } + + public String getBorough() { + return borough; + } + + public void setBorough(String borough) { + this.borough = borough; + } + + public String getCuisine() { + return cuisine; + } + + public void setCuisine(String cuisine) { + this.cuisine = cuisine; + } + + public List getGrades() { + return grades; + } + + public void setGrades(List grades) { + this.grades = grades; + } + + public Long getVersion() { + return version; + } + + public void setVersion(Long version) { + this.version = version; + } + + @Override + public String toString() { + return new StringJoiner(", ", Restaurant.class.getSimpleName() + "[", "]") + .add("id='" + id + "'") + .add("restaurantId=" + restaurantId) + .add("name='" + name + "'") + .add("address=" + address) + .add("borough='" + borough + "'") + .add("cuisine='" + cuisine + "'") + .add("grades=" + grades) + .add("version=" + version) + .toString(); + } } diff --git a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/elasticsearch/repository/CustomRestaurantESRepositoryImpl.java b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/elasticsearch/repository/CustomRestaurantESRepositoryImpl.java index dbeebc086..22f384c31 100644 --- a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/elasticsearch/repository/CustomRestaurantESRepositoryImpl.java +++ b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/elasticsearch/repository/CustomRestaurantESRepositoryImpl.java @@ -272,14 +272,14 @@ public Mono> searchDateRange( * below is the console query * * {@snippet : - * - * """ POST /restaurant/_search?size=15&pretty + * """ + * POST /restaurant/_search?size=15&pretty * { * "query": { * "multi_match": { * "query": "Pizza", * "fields": [ - * "restautant_name", + * "restaurant_name", * "borough", * "cuisine" * ], @@ -289,14 +289,17 @@ public Mono> searchDateRange( * "aggs": { * "MyCuisine": { * "terms": { - * "field": "cuisine", "size": 1000, "order": { + * "field": "cuisine", + * "size": 1000, + * "order": { * "_count": "desc" * } * } * }, * "MyBorough": { * "terms": { - * "field": "borough" , "size": 1000 + * "field": "borough", + * "size": 1000 * } * }, * "MyDateRange": { diff --git a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/mongodb/repository/CustomChangeStreamResumeRepositoryImpl.java b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/mongodb/repository/CustomChangeStreamResumeRepositoryImpl.java index 16e66961a..466bd8cea 100644 --- a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/mongodb/repository/CustomChangeStreamResumeRepositoryImpl.java +++ b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/mongodb/repository/CustomChangeStreamResumeRepositoryImpl.java @@ -2,20 +2,22 @@ import com.example.mongoes.document.ChangeStreamResume; import com.mongodb.client.result.UpdateResult; -import lombok.RequiredArgsConstructor; import org.bson.BsonTimestamp; import org.springframework.data.mongodb.core.ReactiveMongoTemplate; import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Update; import reactor.core.publisher.Mono; -@RequiredArgsConstructor public class CustomChangeStreamResumeRepositoryImpl implements CustomChangeStreamResumeRepository { private final ReactiveMongoTemplate reactiveMongoTemplate; private static final String FIELD_NAME = "resumeTimestamp"; + public CustomChangeStreamResumeRepositoryImpl(ReactiveMongoTemplate reactiveMongoTemplate) { + this.reactiveMongoTemplate = reactiveMongoTemplate; + } + @Override public Mono update(BsonTimestamp resumeTimestamp) { Query query = new Query(); diff --git a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/utils/DateUtility.java b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/utils/DateUtility.java index aea1bcaba..7d8216185 100644 --- a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/utils/DateUtility.java +++ b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/utils/DateUtility.java @@ -3,11 +3,9 @@ import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Date; -import lombok.experimental.UtilityClass; -@UtilityClass public class DateUtility { - public LocalDateTime convertToLocalDateViaInstant(Date dateToConvert) { + public static LocalDateTime convertToLocalDateViaInstant(Date dateToConvert) { return dateToConvert.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); } } diff --git a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/web/controller/RestaurantController.java b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/web/controller/RestaurantController.java index e4d1ac0ac..40ccdc8bc 100644 --- a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/web/controller/RestaurantController.java +++ b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/web/controller/RestaurantController.java @@ -9,7 +9,6 @@ import jakarta.validation.Valid; import jakarta.validation.constraints.Size; import java.net.URI; -import lombok.RequiredArgsConstructor; import org.springframework.data.elasticsearch.core.SearchPage; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; @@ -23,13 +22,16 @@ import reactor.core.publisher.Mono; @RestController -@RequiredArgsConstructor @Timed @RequestMapping("/api/restaurant") public class RestaurantController { private final RestaurantService restaurantService; + public RestaurantController(RestaurantService restaurantService) { + this.restaurantService = restaurantService; + } + @GetMapping public Mono>> findAllRestaurants( @Valid @RequestParam(defaultValue = "10") @Size(max = 999) int limit, diff --git a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/web/controller/SearchController.java b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/web/controller/SearchController.java index f24d08f39..d9871708f 100644 --- a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/web/controller/SearchController.java +++ b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/web/controller/SearchController.java @@ -6,7 +6,6 @@ import com.example.mongoes.web.service.SearchService; import io.micrometer.core.annotation.Timed; import java.util.List; -import lombok.RequiredArgsConstructor; import org.springframework.data.elasticsearch.core.SearchPage; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; @@ -17,11 +16,14 @@ @RestController @Timed -@RequiredArgsConstructor public class SearchController { private final SearchService searchService; + public SearchController(SearchService searchService) { + this.searchService = searchService; + } + @GetMapping("/search/borough") public Mono>> searchPhrase( @RequestParam String query, diff --git a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/web/service/RestaurantService.java b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/web/service/RestaurantService.java index d854b33a5..b6f64500d 100644 --- a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/web/service/RestaurantService.java +++ b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/web/service/RestaurantService.java @@ -20,10 +20,10 @@ import java.util.Date; import java.util.List; import java.util.Objects; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; import org.bson.BsonTimestamp; import org.bson.Document; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.data.domain.PageRequest; @@ -40,16 +40,25 @@ import reactor.core.publisher.Mono; @Service -@Slf4j -@RequiredArgsConstructor public class RestaurantService { + private static final Logger log = LoggerFactory.getLogger(RestaurantService.class); private final RestaurantRepository restaurantRepository; private final RestaurantESRepository restaurantESRepository; private final ChangeStreamResumeRepository changeStreamResumeRepository; - private final ReactiveMongoTemplate reactiveMongoTemplate; + public RestaurantService( + RestaurantRepository restaurantRepository, + RestaurantESRepository restaurantESRepository, + ChangeStreamResumeRepository changeStreamResumeRepository, + ReactiveMongoTemplate reactiveMongoTemplate) { + this.restaurantRepository = restaurantRepository; + this.restaurantESRepository = restaurantESRepository; + this.changeStreamResumeRepository = changeStreamResumeRepository; + this.reactiveMongoTemplate = reactiveMongoTemplate; + } + public Flux loadData() throws IOException { Resource input = new ClassPathResource("restaurants.json"); Path path = input.getFile().toPath(); diff --git a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/web/service/SearchService.java b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/web/service/SearchService.java index cde6d37cf..e65779c04 100644 --- a/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/web/service/SearchService.java +++ b/boot-mongodb-elasticsearch/src/main/java/com/example/mongoes/web/service/SearchService.java @@ -10,8 +10,6 @@ import java.util.List; import java.util.Map; import java.util.function.Function; -import lombok.RequiredArgsConstructor; -import org.apache.commons.lang3.StringUtils; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; @@ -20,15 +18,19 @@ import org.springframework.data.elasticsearch.core.SearchPage; import org.springframework.data.elasticsearch.core.geo.GeoPoint; import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @Service -@RequiredArgsConstructor public class SearchService { private final RestaurantESRepository restaurantESRepository; + public SearchService(RestaurantESRepository restaurantESRepository) { + this.restaurantESRepository = restaurantESRepository; + } + public Mono> searchMatchBorough(String query, Integer offset, Integer limit) { Pageable pageable = PageRequest.of(offset, limit); return Mono.just(restaurantESRepository.findByBorough(query, pageable));