Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1209 [Elasticsearch] Implement rating filter with Elasticsearch #1212

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class ProductField {
public static final String CATEGORIES = "categories";
public static final String ATTRIBUTES = "attributes";
public static final String CREATE_ON = "createdOn";
public static final String RATING = "rating";

private ProductField() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ public ResponseEntity<ProductListGetVm> findProductAdvance(@RequestParam(default
@RequestParam(required = false) String attribute,
@RequestParam(required = false) Double minPrice,
@RequestParam(required = false) Double maxPrice,
@RequestParam(required = false) Double minRating,
@RequestParam(required = false) Double maxRating,
@RequestParam(defaultValue = "DEFAULT")
SortType sortType) {
return ResponseEntity.ok(productService.findProductAdvance(
new ProductCriteriaDto(keyword, page, size, brand, category, attribute, minPrice, maxPrice, sortType)
new ProductCriteriaDto(
keyword, page, size, brand, category, attribute, minPrice, maxPrice, minRating, maxRating, sortType
)
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ public record ProductCriteriaDto(String keyword,
String attribute,
Double minPrice,
Double maxPrice,
Double minRating,
Double maxRating,
SortType sortType) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public ProductListGetVm findProductAdvance(ProductCriteriaDto productCriteria) {
extractedTermsFilter(productCriteria.brand(), ProductField.BRAND, b);
extractedTermsFilter(productCriteria.category(), ProductField.CATEGORIES, b);
extractedTermsFilter(productCriteria.attribute(), ProductField.ATTRIBUTES, b);
extractedRange(productCriteria.minPrice(), productCriteria.maxPrice(), b);
extractedRange(productCriteria.minPrice(), productCriteria.maxPrice(), ProductField.PRICE, b);
extractedRange(productCriteria.minRating(), productCriteria.maxRating(), ProductField.RATING, b);
return b;
})
);
Expand Down Expand Up @@ -116,11 +117,11 @@ private void extractedTermsFilter(String fieldValues, String productField, BoolQ
});
}

private void extractedRange(Number min, Number max, BoolQuery.Builder bool) {
private void extractedRange(Number min, Number max, String field, BoolQuery.Builder bool) {
if (min != null || max != null) {
bool.must(m -> m
.range(r -> r
.field(ProductField.PRICE)
.field(field)
.from(min != null ? min.toString() : null)
.to(max != null ? max.toString() : null)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void testFindProductAdvance_whenSortTypeIsPriceAsc_ReturnProductListGetVm() {

ProductCriteriaDto criteriaDto = new ProductCriteriaDto(
"test", 0, 10, "testBrand", "testCategory",
"testAttribute", 10.0, 100.0, SortType.PRICE_ASC);
"testAttribute", 10.0, 100.0, 1.0, 2.0, SortType.PRICE_ASC);
HnKnA marked this conversation as resolved.
Show resolved Hide resolved
ProductListGetVm result = productService.findProductAdvance(criteriaDto);

verify(elasticsearchOperations, times(1))
Expand Down Expand Up @@ -105,7 +105,7 @@ void testFindProductAdvance_whenSortTypeIsPriceDesc_ReturnProductListGetVm() {
when(elasticsearchOperations.search(any(NativeQuery.class), eq(Product.class))).thenReturn(searchHits);

ProductCriteriaDto criteriaDto = new ProductCriteriaDto("test", 0, 10, "testBrand", "testCategory",
"testAttribute", 10.0, 100.0, SortType.PRICE_DESC);
"testAttribute", 10.0, 100.0, 1.0, 2.0, SortType.PRICE_DESC);
productService.findProductAdvance(criteriaDto);

verify(elasticsearchOperations, times(1))
Expand Down Expand Up @@ -133,7 +133,7 @@ void testFindProductAdvance_whenSortTypeIsDefault_ReturnProductListGetVm() {

ProductCriteriaDto criteriaDto = new ProductCriteriaDto(
"test", 0, 10, "testBrand", "testCategory",
"testAttribute", 10.0, 100.0, SortType.DEFAULT);
"testAttribute", 10.0, 100.0, 1.0, 2.0, SortType.DEFAULT);
productService.findProductAdvance(criteriaDto);

verify(elasticsearchOperations, times(1))
Expand Down
Loading