-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds documentation and polish (#1557)
* feat : upgrade to spring boot 3.4.0 * feat : remove lombok * feat : polish Code * adds java doc * adds code review comments Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * adds swagger documentation Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * add documentation * feat : convert to nonblocking io * feat : handle exception * fix : issue with loading all data * implement code review comments --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
- Loading branch information
1 parent
7ce45a3
commit e8245e9
Showing
9 changed files
with
256 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...ongodb-elasticsearch/src/main/java/com/example/mongoes/config/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.example.mongoes.config; | ||
|
||
import com.example.mongoes.response.GenericMessage; | ||
import com.example.mongoes.web.exception.DuplicateRestaurantException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@RestControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
@ExceptionHandler(DuplicateRestaurantException.class) | ||
public ResponseEntity<GenericMessage> handleDuplicateRestaurantException( | ||
DuplicateRestaurantException ex) { | ||
return ResponseEntity.status(HttpStatus.CONFLICT).body(new GenericMessage(ex.getMessage())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...csearch/src/main/java/com/example/mongoes/web/exception/DuplicateRestaurantException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.example.mongoes.web.exception; | ||
|
||
public class DuplicateRestaurantException extends RuntimeException { | ||
|
||
public DuplicateRestaurantException(String message) { | ||
super(message); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...odb-elasticsearch/src/main/java/com/example/mongoes/web/service/AggregationProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.example.mongoes.web.service; | ||
|
||
import co.elastic.clients.elasticsearch._types.aggregations.Aggregate; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.data.elasticsearch.client.elc.ElasticsearchAggregation; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** | ||
* Processes Elasticsearch aggregations and transforms them into a structured map format. Supports | ||
* 'terms' and 'dateRange' aggregation types. | ||
* | ||
* <p>Example output format: { "termAggregation": {"term1": 10, "term2": 20}, | ||
* "dateRangeAggregation": {"2023-01-01 - 2023-12-31": 100} } | ||
*/ | ||
@Service | ||
class AggregationProcessor { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(AggregationProcessor.class); | ||
|
||
/** | ||
* Processes Elasticsearch aggregations and returns a structured map of results. | ||
* | ||
* @param aggregationMap Map of aggregation key to ElasticsearchAggregation | ||
* @return Map of aggregation key to counts, where counts is a map of bucket key to document | ||
* count | ||
* @throws IllegalArgumentException if aggregationMap is null | ||
*/ | ||
public Map<String, Map<String, Long>> processAggregations( | ||
Map<String, ElasticsearchAggregation> aggregationMap) { | ||
if (aggregationMap == null) { | ||
throw new IllegalArgumentException("aggregationMap must not be null"); | ||
} | ||
Map<String, Map<String, Long>> resultMap = new HashMap<>(); | ||
aggregationMap.forEach( | ||
(String aggregateKey, ElasticsearchAggregation aggregation) -> { | ||
Map<String, Long> countMap = new HashMap<>(); | ||
Aggregate aggregate = aggregation.aggregation().getAggregate(); | ||
processAggregate(aggregate, countMap); | ||
resultMap.put(aggregateKey, countMap); | ||
}); | ||
return resultMap; | ||
} | ||
|
||
private void processAggregate(Aggregate aggregate, Map<String, Long> countMap) { | ||
if (aggregate.isSterms()) { | ||
processTermsAggregate(aggregate, countMap); | ||
} else if (aggregate.isDateRange()) { | ||
processDateRangeAggregate(aggregate, countMap); | ||
} else { | ||
log.debug( | ||
"Unsupported aggregation type encountered: {}", | ||
aggregate.getClass().getSimpleName()); | ||
} | ||
} | ||
|
||
private void processTermsAggregate(Aggregate aggregate, Map<String, Long> countMap) { | ||
aggregate | ||
.sterms() | ||
.buckets() | ||
.array() | ||
.forEach(bucket -> countMap.put(bucket.key().stringValue(), bucket.docCount())); | ||
} | ||
|
||
private void processDateRangeAggregate(Aggregate aggregate, Map<String, Long> countMap) { | ||
aggregate.dateRange().buckets().array().stream() | ||
.filter(bucket -> bucket.docCount() != 0) | ||
.forEach( | ||
bucket -> | ||
countMap.put( | ||
bucket.fromAsString() + " - " + bucket.toAsString(), | ||
bucket.docCount())); | ||
} | ||
} |
Oops, something went wrong.