private static PagedSearchResult<ProductProjection> executeSearchWithFacets(final FacetExpression<ProductProjection> facetExpr) { return executeSearch(ProductProjectionSearch.ofStaged().withFacets(facetExpr)); } }
@Test public void searchForReviewsWithAverageRatingGreaterThan2() { final List<FacetRange<BigDecimal>> facetRanges = IntStream.range(LOWEST_RATING, HIGHEST_RATING) .mapToObj(i -> FacetRange.of(new BigDecimal(i), new BigDecimal(i + 1))) .collect(toList()); assertThat(facetRanges.toString()).isEqualTo("[[0 to 1), [1 to 2), [2 to 3), [3 to 4), [4 to 5)]"); final ProductProjectionSearch searchRequest = ProductProjectionSearch.ofStaged()//in prod it would be current .withResultFilters(m -> m.reviewRatingStatistics().averageRating().isGreaterThanOrEqualTo(new BigDecimal(2))) .withFacets(m -> m.reviewRatingStatistics().averageRating().onlyRange(facetRanges)) .withSort(m -> m.reviewRatingStatistics().averageRating().desc()); assertEventually(Duration.ofSeconds(60), Duration.ofMillis(100), () -> { final PagedSearchResult<ProductProjection> result = client().executeBlocking(searchRequest); assertThat(result.getResults()).hasSize(2); final ProductProjection productProjection = result.getResults().get(0); assertThat(productProjection.getReviewRatingStatistics().getCount()).isEqualTo(REVIEWS_PER_PRODUCT); final RangeFacetResult facetResult = (RangeFacetResult) result.getFacetResult("reviewRatingStatistics.averageRating"); assertThat(facetResult.getRanges().get(2)).isEqualTo(RangeStats.of("2.0", "3.0", 2L, null, "2.2", "2.7", "4.9", 2.45)); }); }
@Test public void facetSearchByScopedPriceDiscounted() { withProductOfPrices(asList(PriceDraft.of(EURO_20), PriceDraft.of(EURO_30).withCountry(DE)), product1 -> { withProductOfPrices(asList(PriceDraft.of(EURO_30), PriceDraft.of(EURO_40).withCountry(DE)), product2 -> { withProductOfPrices(asList(PriceDraft.of(EURO_40), PriceDraft.of(EURO_25).withCountry(DE)), product3 -> { final ProductDiscountDraft productDiscountDraft1 = discountDraftOfAbsoluteValue(product1, EURO_5); withProductDiscount(client(), productDiscountDraft1, productDiscount1 -> { assertEventually(s -> { final TermFacetExpression<ProductProjection> facetExpression = ProductProjectionSearchModel.of() .facet().allVariants().scopedPriceDiscounted().allTerms(); final ProductProjectionSearch search = ProductProjectionSearch.ofStaged() .withPriceSelection(PriceSelection.of(EUR).withPriceCountry(DE)) .plusQueryFilters(m -> m.id().isIn(asList(product1.getId(), product2.getId(), product3.getId()))) .withFacets(facetExpression); final PagedSearchResult<ProductProjection> results = client().executeBlocking(search); assertThat(results.getResults()).hasSize(3); final List<TermStats> termStats = results.getFacetResult(facetExpression).getTerms(); s.assertThat(termStats).containsOnly( TermStats.of(BOOL_TRUE, 1L), TermStats.of(BOOL_FALSE, 2L)); }); }); }); }); }); }
@Test public void searchRequestIsAsExpected() { final ProductProjectionSearch actual = ProductProjectionSearchBuilder.ofCurrent() .facets(m -> m.allVariants().price().centAmount().allRanges()) .queryFilters(m -> m.allVariants().price().centAmount().isGreaterThanOrEqualTo(4L)) .sort(m -> m.createdAt().asc()) .expansionPaths(m -> m.categories()) .build(); final ProductProjectionSearch expected = ProductProjectionSearch.ofCurrent() .withFacets(m -> m.allVariants().price().centAmount().allRanges()) .withQueryFilters(m -> m.allVariants().price().centAmount().isGreaterThanOrEqualTo(4L)) .withSort(m -> m.createdAt().asc()) .withExpansionPaths(m -> m.categories()); assertThat(actual).isEqualTo(expected); } }