default ProductProjectionSearch bySku(final List<String> skus) { return withQueryFilters(m -> m.allVariants().sku().isIn(skus)); }
default ProductProjectionSearch bySku(final List<String> skus) { return withQueryFilters(m -> m.allVariants().sku().isIn(skus)); }
default ProductProjectionSearch bySku(final String sku) { return withQueryFilters(m -> m.allVariants().sku().is(sku)); }
default ProductProjectionSearch bySlug(final Locale locale, final String slug) { return withQueryFilters(m -> m.slug().locale(locale).is(slug)); }
default ProductProjectionSearch bySlug(final Locale locale, final String slug) { return withQueryFilters(m -> m.slug().locale(locale).is(slug)); }
default ProductProjectionSearch bySku(final String sku) { return withQueryFilters(m -> m.allVariants().sku().is(sku)); }
@Test public void categorySubtree() { final List<String> expressionsWithoutSubtrees = ProductProjectionSearch.ofStaged().withQueryFilters(m -> m.categories().id().isIn(asList("id1", "id2"))).queryFilters().stream().map(e -> e.expression()).collect(Collectors.toList()); assertThat(expressionsWithoutSubtrees.get(0)).isEqualTo("categories.id:\"id1\",\"id2\""); final List<FilterExpression<ProductProjection>> filterExpressions = ProductProjectionSearch.ofStaged().withQueryFilters(m -> m.categories().id().isInSubtreeOrInCategory(asList("id1", "id2"), asList("id3", "id4"))).queryFilters(); final List<String> collect = filterExpressions.stream().map(e -> e.expression()).collect(Collectors.toList()); final String expected = "categories.id:subtree(\"id1\"),subtree(\"id2\"),\"id3\",\"id4\""; assertThat(collect).hasSize(1); assertThat(collect.get(0)).isEqualTo(expected); }
private static void testProductIds(final List<FilterExpression<ProductProjection>> filterExpr, final Consumer<List<String>> test) { final ProductProjectionSearch search = ProductProjectionSearch.ofStaged().withQueryFilters(filterExpr); final List<ProductProjection> results = executeSearch(search).getResults(); test.accept(toIds(results)); } }
private void checkSorting(final Function<ProductProjectionSortSearchModel, SortExpression<ProductProjection>> sortExpressionFunction, final Consumer<ProductProjection> asserter) { final ProductProjectionSearch productProjectionSearch = ProductProjectionSearch.ofStaged() .withQueryFilters(m -> m.reviewRatingStatistics().count().isGreaterThanOrEqualTo(BigDecimal.ZERO)) .withSort(sortExpressionFunction); assertEventually(Duration.ofSeconds(15), Duration.ofMillis(200), () -> { final List<ProductProjection> results = client().executeBlocking(productProjectionSearch).getResults(); assertThat(results.size()).isGreaterThanOrEqualTo(3); final ProductProjection first = results.get(0); asserter.accept(first); }); } }
@Test public void findMatchingVariantByQueryFilter() throws Exception { testBlueFilter(filters -> ProductProjectionSearch.ofStaged().withMarkingMatchingVariants(true).withQueryFilters(filters)); }
@Test public void fuzzySearch() { final ProductProjectionSearch search = ProductProjectionSearch.ofStaged() .withText(ENGLISH, "short") .withQueryFilters(productModel -> productModel.productType().id().is(productType.getId())); assertEventually(Duration.ofSeconds(45), Duration.ofMillis(200), () -> assertThat(client().executeBlocking(search).getResults()).matches(containsIdentifiable(product2).negate(), "not included")); assertEventually(Duration.ofSeconds(45), Duration.ofMillis(200), () -> assertThat(client().executeBlocking(search.withFuzzy(true)).getResults()).matches(containsIdentifiable(product2), "included")); }
private Price searchWith(final Product product, final PriceSelection selection) { final ProductProjectionSearch searchRequest = ProductProjectionSearch.ofStaged() .withQueryFilters(m -> m.id().is(product.getId()))//to limit the test scope .withPriceSelection(selection);//price selection config final PagedSearchResult<ProductProjection> searchResult = client().executeBlocking(searchRequest); assertThat(searchResult.getCount()).isEqualTo(1); return searchResult.head().get().getMasterVariant().getPrice(); }
@Test public void score() { final ProductProjectionSearch baseRequest = ProductProjectionSearch.ofStaged() .withQueryFilters(m -> m.id().isIn(getAllIds())); final ProductProjectionSearch asc = baseRequest.withSort(m -> m.score().asc()); assertEventually(() -> { final PagedSearchResult<ProductProjection> ascResult = client().executeBlocking(asc); assertThat(ascResult.getTotal()).isEqualTo(getAllIds().size()); }); }
@Test public void id() { final ProductProjectionSearch baseRequest = ProductProjectionSearch.ofStaged() .withQueryFilters(m -> m.id().isIn(getAllIds())); final ProductProjectionSearch asc = baseRequest.withSort(m -> m.id().asc()); final ProductProjectionSearch desc = baseRequest.withSort(m -> m.id().desc()); assertEventually(() -> { final PagedSearchResult<ProductProjection> ascResult = client().executeBlocking(asc); assertThat(ascResult.getTotal()).isEqualTo(getAllIds().size()); final PagedSearchResult<ProductProjection> descResult = client().executeBlocking(desc); assertThat(descResult.getTotal()).isEqualTo(getAllIds().size()); final LinkedList<ProductProjection> reversedDesc = new LinkedList<>(descResult.getResults()); Collections.reverse(reversedDesc); assertThat(ascResult.getResults()).isEqualTo(reversedDesc); }); }
@Test public void selectAPriceByCurrency() { final List<PriceDraft> prices = asList(PriceDraft.of(EURO_30), PriceDraft.of(USD_20)); withProductOfPrices(prices, product -> { final ProductProjectionSearch searchRequest = ProductProjectionSearch.ofStaged() .withQueryFilters(m -> m.id().is(product.getId()))//to limit the test scope .withPriceSelection(PriceSelection.of(EUR));//price selection config assertEventually(() -> { final PagedSearchResult<ProductProjection> result = client().executeBlocking(searchRequest); assertThat(result.getCount()).isEqualTo(1); final ProductVariant masterVariant = result.getResults().get(0).getMasterVariant(); assertThat(masterVariant.getPrice()).isNotNull().has(price(PriceDraft.of(EURO_30))); }); }); }
/** * Gets the products belonging to the given categories, up to {@code numProducts}, most expensive products first. * @param categoryIds the category IDs to get products from * @param numProducts the number of products the returned set should contain * @return the products related to these categories */ private CompletionStage<Set<ProductProjection>> productsFromCategoryIds(final Iterable<String> categoryIds, final int numProducts) { final ProductProjectionSearch request = ProductProjectionSearch.ofCurrent() .withLimit(numProducts) .withQueryFilters(product -> product.categories().id().containsAny(categoryIds)) .withSort(product -> product.allVariants().price().desc()) .withPriceSelection(PriceUtils.createPriceSelection(userContext)); return sphereClient.execute(request) .whenCompleteAsync((result, t) -> logProductRequest(LOGGER, request, result), HttpExecution.defaultContext()) .thenApply(SunriseProductRecommendation::resultToProductSet); }
@Test public void forId() { final Function<ProductProjectionFilterSearchModel, ExistsAndMissingFilterSearchModelSupport<ProductProjection>> dsl = m -> m.state(); withProduct(client(), product -> { final ProductProjectionSearch baseRequest = ProductProjectionSearch.ofStaged() .withQueryFilters(m1 -> { final List<String> productIds = singletonList(product.getId()); return m1.id().isIn(productIds); }); final ProductProjectionSearch exists = baseRequest.plusQueryFilters(m -> m.id().exists()); final ProductProjectionSearch missing = baseRequest.plusQueryFilters(m -> m.id().missing()); assertEventually(() -> { assertThat(client().executeBlocking(exists).getResults()).hasSize(1) .extracting(s -> s.getId()).as("exists").containsExactly(product.getId()); assertThat(client().executeBlocking(missing).getResults()).isEmpty(); }); }); }
private static List<ProductProjection> searchForCategoryAndSort(final String categoryId) { final ProductProjectionSearch searchRequest = ProductProjectionSearch.ofStaged() .withQueryFilters(productModel -> productModel.categories().id().is(categoryId)) .withSort(productModel -> productModel.categoryOrderHints().category(categoryId).asc()); return client().executeBlocking(searchRequest).getResults(); }
@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); } }
@Test public void containsAnyIncludingSubtrees() { final List<String> categoryIds1 = getCategoryIds("A", "B-1", "C-2-2"); final List<String> categoryIds2 = getCategoryIds("A", "B-1"); final List<String> categoryIds3 = getCategoryIds("A", "B", "C"); withProductInCategories(client(), categoryIds1, (Product product1) -> { withProductInCategories(client(), categoryIds2, (Product product2) -> { withProductInCategories(client(), categoryIds3, (Product product3) -> { assertEventually(() -> { final ProductProjectionSearch request = ProductProjectionSearch.ofStaged() .withQueryFilters(m -> m.categories().id().containsAnyIncludingSubtrees(getCategoryIds("C-2"))) .plusQueryFilters(m -> m.id().isIn(asList(product1.getId(), product2.getId(), product3.getId()))); assertThat(client().executeBlocking(request)).has(onlyProducts(product1)); }); }); }); }); }