public static <T> RangeFacetResult<T> of(final List<RangeStats<T>> ranges) { return new RangeFacetResult<>(ranges); } }
public SimpleRangeStats getRangeStatsOfAllRanges(final RangeFacetExpression<T> facetExpression) { final String facetResultPath = facetExpression.resultPath(); final boolean facetIsOfTypeAllRanges = Optional.ofNullable(facetExpression.value()) .map(v -> v.trim().equals(":range(* to \"0\"),(\"0\" to *)")) .orElse(false); if (facetIsOfTypeAllRanges) { final RangeFacetResult facetResult = getRangeFacetResult(facetResultPath); return getSimpleRangeStats(facetResult.getRanges()); } else { throw new IllegalArgumentException("Facet result is not of type RangeFacetResult for all ranges, i.e. (* to \"0\"),(\"0\" to *): " + facetResultPath); } }
private void testPagedSearchResultWithAllRanges(final List<RangeStats> rangeStats, final Consumer<SimpleRangeStats> test) { final RangeFacetExpression<Object> facetExpression = RangeFacetExpression.of("foo:range(* to \"0\"),(\"0\" to *) as allRangesFacet"); final Map<String, FacetResult> facets = new HashMap<>(); facets.put(facetExpression.resultPath(), RangeFacetResult.of(rangeStats)); final PagedSearchResult<Object> result = pagedSearchResult(facets); test.accept(result.getRangeStatsOfAllRanges(facetExpression)); }
@Test public void parsesRangeFacetResultsProductCounts() throws Exception { final RangeFacetResult rangeFacet = searchResult.getRangeFacetResult(RANGE_FACET_COUNTING_PRODUCTS_KEY); assertThat(rangeFacet.getRanges()).hasSize(2); assertThat(rangeFacet.getRanges().get(0).getProductCount()).isEqualTo(2302); assertThat(rangeFacet.getRanges().get(1).getProductCount()).isEqualTo(978); } }
public static RangeFacetResult of(final List<RangeStats> ranges) { return new RangeFacetResult(ranges); } }
private static void testRangeStats(final RangeFacetExpression<ProductProjection> facetExpr, final Consumer<RangeStats> test) { final List<RangeStats> ranges = executeSearchWithFacets(facetExpr).getFacetResult(facetExpr).getRanges(); assertThat(ranges).hasSize(1); test.accept(ranges.get(0)); }
@Test public void parsesRangeFacetResults() throws Exception { final RangeFacetResult rangeFacet = searchResult.getRangeFacetResult(RANGE_FACET_KEY); assertThat(rangeFacet.getRanges()).hasSize(2); final RangeStats rangeStats = rangeFacet.getRanges().get(1); assertThat(rangeStats.getLowerEndpoint()).isEqualTo("5001"); assertThat(rangeStats.getUpperEndpoint()).isNull(); assertThat(rangeStats.getCount()).isEqualTo(1799L); assertThat(rangeStats.getMin()).isEqualTo("5100"); assertThat(rangeStats.getMax()).isEqualTo("590000"); assertThat(rangeStats.getSum()).isEqualTo("92868378"); assertThat(rangeStats.getMean()).isEqualTo(51622.222345747636); }
@Test public void rangeFacetsAreParsed() throws Exception { final RangeFacetExpression<ProductProjection> facetExpr = PRODUCT_MODEL.allVariants().attribute().ofNumber(ATTR_NAME_SIZE).onlyGreaterThanOrEqualTo(ZERO); final ProductProjectionSearch search = ProductProjectionSearch.ofStaged().plusFacets(facetExpr); testResult(search, result -> { final RangeStats rangeStats = result.getFacetResult(facetExpr).getRanges().get(0); assertThat(rangeStats.getLowerEndpoint()).isEqualTo("0.0"); assertThat(rangeStats.getUpperEndpoint()).isNull(); assertThat(rangeStats.getCount()).isEqualTo(6L); assertThat(rangeStats.getMin()).isEqualTo("36.0"); assertThat(rangeStats.getMax()).isEqualTo("46.0"); assertThat(rangeStats.getSum()).isEqualTo("246.0"); assertThat(rangeStats.getMean()).isEqualTo(41.0); }); }
@Test public void rangeFacetsSupportsAlias() throws Exception { final String alias = "my-facet"; final RangeFacetExpression<ProductProjection> facetExpr = PRODUCT_MODEL.allVariants().attribute().ofNumber(ATTR_NAME_SIZE).withAlias(alias).onlyGreaterThanOrEqualTo(ZERO); final ProductProjectionSearch search = ProductProjectionSearch.ofStaged().plusFacets(facetExpr); testResult(search, result -> { assertThat(facetExpr.resultPath()).isEqualTo(alias); final RangeStats rangeStats = result.getFacetResult(facetExpr).getRanges().get(0); assertThat(rangeStats.getLowerEndpoint()).isEqualTo("0.0"); assertThat(rangeStats.getUpperEndpoint()).isNull(); assertThat(rangeStats.getCount()).isEqualTo(6L); assertThat(rangeStats.getMin()).isEqualTo("36.0"); assertThat(rangeStats.getMax()).isEqualTo("46.0"); assertThat(rangeStats.getSum()).isEqualTo("246.0"); assertThat(rangeStats.getMean()).isEqualTo(41.0); }); }
@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 responseContainsRangeFacetsForAttributes() throws Exception { final RangeFacetExpression<ProductProjection> facetExpr = PRODUCT_MODEL.allVariants().price().centAmount().onlyGreaterThanOrEqualTo(0L); final ProductProjectionSearch search = ProductProjectionSearch.ofStaged().plusFacets(facetExpr); testResult(search, result -> assertThat(result.getFacetResult(facetExpr).getRanges().get(0).getCount()).isGreaterThan(0)); }
@Test public void searchForAvailableQuantityRanges() { withProductOfStock(client(), 10, productWith10Items -> { withProductOfStock(client(), 5, productWith5Items -> { final RangeFacetExpression<ProductProjection> productProjectionRangeFacetExpression = ProductProjectionSearchModel.of() .facet().allVariants().availability().availableQuantity().onlyGreaterThanOrEqualTo(new BigDecimal(1)); final ProductProjectionSearch request = ProductProjectionSearch.ofStaged() .plusQueryFilters(m -> m.id().isIn(asList(productWith10Items.getId(), productWith5Items.getId()))) .plusQueryFilters(m -> m.allVariants().availability().availableQuantity().isGreaterThanOrEqualTo(new BigDecimal(6))) .plusFacets(productProjectionRangeFacetExpression); assertEventually(() -> { final PagedSearchResult<ProductProjection> res = client().executeBlocking(request); assertThat(res.getResults()).hasSize(1); assertThat(res.getResults().get(0).getId()) .as("finds only the product with the sufficient amount stocked") .isEqualTo(productWith10Items.getId()); final RangeFacetResult facetResult = res.getFacetResult(productProjectionRangeFacetExpression); assertThat(facetResult.getRanges().get(0).getMax()) .isEqualTo("10.0"); }); }); }); }
@Test public void searchByCount() { final ProductProjectionSearch projectionSearch = ProductProjectionSearch.ofStaged()//in prod it would be current .withResultFilters(m -> m.reviewRatingStatistics().count().isGreaterThanOrEqualTo(new BigDecimal(2))) .plusResultFilters(m -> m.reviewRatingStatistics().highestRating().isGreaterThanOrEqualTo(new BigDecimal(2))) .plusResultFilters(m -> m.reviewRatingStatistics().lowestRating().isGreaterThanOrEqualTo(new BigDecimal(0))) .plusFacets(m -> m.reviewRatingStatistics().count().allRanges()) .plusFacets(m -> m.reviewRatingStatistics().highestRating().allRanges()) .plusFacets(m -> m.reviewRatingStatistics().lowestRating().allRanges()) ; assertEventually(Duration.ofSeconds(60), Duration.ofMillis(100), () -> { softAssert(soft -> { final PagedSearchResult<ProductProjection> res = client().executeBlocking(projectionSearch); soft.assertThat(res.getCount()).isGreaterThanOrEqualTo(3); final RangeFacetResult countFacets = (RangeFacetResult) res.getFacetResult("reviewRatingStatistics.count"); soft.assertThat(countFacets.getRanges().get(1).getSum()).as("count facets").isEqualTo("60.0"); final RangeFacetResult lowestRatingFacets = (RangeFacetResult) res.getFacetResult("reviewRatingStatistics.lowestRating"); soft.assertThat(lowestRatingFacets.getRanges().get(1).getSum()).as("lowestRating facets").isEqualTo("0.0"); final RangeFacetResult highestRatingFacets = (RangeFacetResult) res.getFacetResult("reviewRatingStatistics.highestRating"); soft.assertThat(highestRatingFacets.getRanges().get(1).getSum()).as("highestRating facets").isEqualTo("14.0"); }); }); }
@Test public void searchForAvailableQuantityRangesInChannels() { withChannelOfRole(client(), ChannelRole.INVENTORY_SUPPLY, channel -> { withProductOfStockAndChannel(client(), 10, channel, productWith10Items -> { withProductOfStockAndChannel(client(), 5, channel, productWith5Items -> { final RangeFacetExpression<ProductProjection> facet = ProductProjectionSearchModel.of() .facet().allVariants().availability().channels().channelId(channel.getId()) .availableQuantity().onlyGreaterThanOrEqualTo(new BigDecimal(1)); final ProductProjectionSearch request = ProductProjectionSearch.ofStaged() .plusQueryFilters(m -> m.id().isIn(asList(productWith10Items.getId(), productWith5Items.getId()))) .plusQueryFilters(m -> m.allVariants().availability().channels().channelId(channel.getId()) .availableQuantity().isGreaterThanOrEqualTo(new BigDecimal(6))) .plusFacets(facet); assertEventually(() -> { final PagedSearchResult<ProductProjection> res = client().executeBlocking(request); assertThat(res.getResults()).hasSize(1); assertThat(res.getResults().get(0).getId()) .as("finds only the product with the sufficient amount stocked") .isEqualTo(productWith10Items.getId()); final RangeFacetResult facetResult = res.getFacetResult(facet); assertThat(facetResult.getRanges().get(0).getMax()) .isEqualTo("10.0"); }); }); }); }); }