From a7d67eb82a55ade700b7790c097bdfa684e7f920 Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Thu, 21 Sep 2023 09:31:55 +0000 Subject: [PATCH] adds more dateranges and polish --- .../CustomRestaurantESRepositoryImpl.java | 148 +++++++++++++++--- 1 file changed, 123 insertions(+), 25 deletions(-) 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 5a800cffb..9f88494c6 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 @@ -9,6 +9,7 @@ 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; @@ -259,6 +260,93 @@ public Mono> searchDateRange( return reactiveElasticsearchOperations.searchForPage(query, Restaurant.class); } + + /** + * + * 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" + } + ] + } + } + } + } + """; + } + * + * + */ @Override public Mono> aggregateSearch( String searchKeyword, @@ -277,31 +365,9 @@ public Mono> aggregateSearch( AggregationBuilders.dateRange( builder -> builder.field("grades.date") - .format("MM-yyyy") - .ranges( - DateRangeExpression.of( - dateRanageExpressionBuilder -> - dateRanageExpressionBuilder - .key("Older") - .to( - builder1 -> - builder1 - .expr( - "now-120M/M"))), - DateRangeExpression.of( - dateRanageExpressionBuilder -> - dateRanageExpressionBuilder - .from( - builder1 -> - builder1 - .expr( - "now-120M/M")) - .to( - builder1 -> - builder1 - .expr( - "now/M"))))); - // addDateRange(dateRangeBuilder); + .format("dd-MM-yyy") + .timeZone("UTC") + .ranges(getDateRanges())); Query query = NativeQuery.builder() @@ -321,6 +387,38 @@ public Mono> aggregateSearch( return this.reactiveElasticsearchOperations.searchForPage(query, Restaurant.class); } + private List getDateRanges() { + List dateList = new ArrayList<>(); + + // Create the "Older" DateRangeExpression + dateList.add( + DateRangeExpression.of( + builder -> + builder.key("Older").to(toBuilder -> toBuilder.expr("now-12y/y")))); + + // Create DateRangeExpressions for the middle years + for (int i = 12; i > 0; i--) { + dateList.add(createDateRangeExpression(i)); + } + + // Create the "Newer" DateRangeExpression + dateList.add( + DateRangeExpression.of( + builder -> + builder.key("Newer") + .from(fromBuilder -> fromBuilder.expr("now-0y/y")) + .to(toBuilder -> toBuilder.expr("now/d")))); + + return dateList; + } + + private DateRangeExpression createDateRangeExpression(int yearsAgo) { + return new DateRangeExpression.Builder() + .from(fromBuilder -> fromBuilder.expr("now-%dy/y".formatted(yearsAgo))) + .to(toBuilder -> toBuilder.expr("now-%dy/y".formatted(yearsAgo - 1))) + .build(); + } + @Override public Mono> findAll(Pageable pageable) { Query query = new CriteriaQuery(Criteria.where("_id").exists());