@Test public void filterByProductKey() { final List<String> keys = Stream.of(product1, product2) .map(product -> product.getKey()) .collect(toList()); final ProductProjectionSearch search = ProductProjectionSearch.ofStaged() .plusQueryFilters(m -> m.key().isIn(keys)); assertEventually(Duration.ofSeconds(45), Duration.ofMillis(200), () -> { final List<ProductProjection> results = client().executeBlocking(search).getResults(); assertThat(results) .extracting(m -> m.getKey()) .containsOnly(keys.get(0), keys.get(1)); }); }
@Test public void filterByEvilCharacterWord() throws Exception { final ProductProjectionSearch search = ProductProjectionSearch.ofStaged() .plusQueryFilters(productModel -> productModel.allVariants().attribute().ofString(ATTR_NAME_EVIL).is(EVIL_CHARACTER_WORD)); assertEventually(Duration.ofSeconds(60), Duration.ofMillis(200), () -> { final PagedSearchResult<ProductProjection> result = executeEvilSearch(search); assertThat(result.getTotal()).as("total").isEqualTo(1); }); }
@Test public void filtersByAnyTerm() throws Exception { final ProductProjectionSearch search = ProductProjectionSearch.ofStaged() .plusQueryFilters(productModel -> productModel.allVariants().attribute().ofNumber(ATTR_NAME_SIZE).isIn(asList(valueOf(36), valueOf(38)))); testResultIds(search, resultIds -> assertThat(resultIds).containsOnly(product1.getId(), product2.getId())); }
@Test public void simpleFilterByRange() throws Exception { final FilterExpression<ProductProjection> filterExpr = FilterExpression.of("variants.attributes." + ATTR_NAME_SIZE + ":range(44 to *)"); final ProductProjectionSearch search = ProductProjectionSearch.ofStaged().plusQueryFilters(singletonList(filterExpr)); testResultIds(search, resultIds -> assertThat(resultIds).containsOnly(product1.getId(), product2.getId())); }
@Test public void filterBySku() { final List<String> skus = Stream.of(product1, product2) .map(product -> product.getMasterData().getStaged().getMasterVariant().getSku()) .collect(toList()); final ProductProjectionSearch search = ProductProjectionSearch.ofStaged() .plusQueryFilters(m -> m.allVariants().sku().isIn(skus)); assertEventually(Duration.ofSeconds(45), Duration.ofMillis(200), () -> { assertThat(client().executeBlocking(search).getResults()) .extracting(m -> m.getMasterVariant().getSku()) .containsOnly(skus.get(0), skus.get(1)); }); }
@Test public void filterByProductVariantKey() { final List<String> keys = Stream.of(product1, product2) .map(product -> product.getMasterData().getStaged().getMasterVariant().getKey()) .collect(toList()); final ProductProjectionSearch search = ProductProjectionSearch.ofStaged() .plusQueryFilters(m -> m.allVariants().key().isIn(keys)); assertEventually(Duration.ofSeconds(45), Duration.ofMillis(200), () -> { final List<ProductProjection> results = client().executeBlocking(search).getResults(); assertThat(results) .extracting(m -> m.getMasterVariant().getKey()) .containsOnly(keys.get(0), keys.get(1)); }); }
protected static PagedSearchResult<ProductProjection> executeEvilSearch(final ProductProjectionSearch search) { final List<String> ids = asList(evilProduct1.getId(), evilProduct2.getId()); final ProductProjectionSearch sphereRequest = search.plusQueryFilters(product -> product.id().containsAny(ids)); return client().executeBlocking(sphereRequest); }
@Test public void filtersByAllRanges() throws Exception { final ProductProjectionSearch search = ProductProjectionSearch.ofStaged() .plusQueryFilters(productModel -> productModel.allVariants().attribute().ofNumber(ATTR_NAME_SIZE).isBetweenAll(asList(atLeast(valueOf(39)), atMost(valueOf(43))))); testResultIds(search, resultIds -> assertThat(resultIds).containsOnly(product3.getId())); }
@Test public void filtersByAnyRange() throws Exception { final ProductProjectionSearch search = ProductProjectionSearch.ofStaged() .plusQueryFilters(productModel -> productModel.allVariants().attribute().ofNumber(ATTR_NAME_SIZE).isBetweenAny(asList(atLeast(valueOf(46)), atMost(valueOf(36))))); testResultIds(search, resultIds -> assertThat(resultIds).containsOnly(product1.getId(), product2.getId())); }
@Test public void channelsFilterDsl() { final StringHttpRequestBody body = (StringHttpRequestBody) ProductProjectionSearch.ofStaged().plusQueryFilters(m -> m.allVariants().availability().channels().channelId("channel-id-12") .availableQuantity().isGreaterThanOrEqualTo(new BigDecimal(3))).httpRequestIntent().getBody(); assertThat(body.getString()).contains("filter.query=variants.availability.channels.channel-id-12.availableQuantity%3Arange%283+to+*%29"); }
protected static PagedSearchResult<ProductProjection> executeSearch(final ProductProjectionSearch search) { final List<String> ids = asList(product1.getId(), product2.getId(), product3.getId()); final ProductProjectionSearch sphereRequest = search.plusQueryFilters(product -> product.id().isIn(ids)); return client().executeBlocking(sphereRequest); }
@Test public void searchForIsOnStock() { withProductOfStock(client(), 2, product -> { final ProductProjectionSearch request = ProductProjectionSearch.ofStaged() .plusQueryFilters(m -> m.id().is(product.getId())) .plusQueryFilters(m -> m.allVariants().availability().isOnStock().is(true)); assertEventually(() -> { final PagedSearchResult<ProductProjection> res = client().executeBlocking(request); assertThat(res.getResults()).hasSize(1); }); }); }
@Test public void filterByValueAsString() throws Exception { final ProductProjectionSearch search = ProductProjectionSearch.ofStaged() .plusQueryFilters(productModel -> productModel.allVariants().attribute().ofNumber(ATTR_NAME_SIZE).containsAnyAsString(asList("36", "38"))); testResultIds(search, resultIds -> assertThat(resultIds).containsOnly(product1.getId(), product2.getId())); }
@Test public void searchForIsNotOnStock() { withProductOfStock(client(), 0, product -> { final ProductProjectionSearch request = ProductProjectionSearch.ofStaged() .plusQueryFilters(m -> m.id().is(product.getId())) .plusQueryFilters(m -> m.allVariants().availability().isOnStock().is(false)); assertEventually(() -> { final PagedSearchResult<ProductProjection> res = client().executeBlocking(request); assertThat(res.getResults()).hasSize(0); }); }); }
@Test public void filtersByRange() throws Exception { final ProductProjectionSearch search = ProductProjectionSearch.ofStaged() .plusQueryFilters(productModel -> productModel.allVariants().attribute().ofNumber(ATTR_NAME_SIZE).isGreaterThanOrEqualTo(valueOf(44))); testResultIds(search, resultIds -> assertThat(resultIds).containsOnly(product1.getId(), product2.getId())); }
@Test public void filterQueryFiltersBeforeFacetsAreCalculated() throws Exception { final ProductProjectionSearch search = ProductProjectionSearch.ofStaged() .plusFacets(SIZE_FACETED_SEARCH.facetExpression()) .plusFacets(COLOR_FACETED_SEARCH.facetExpression()) .plusQueryFilters(COLOR_FACETED_SEARCH.filterExpressions()); testResult(search, ids -> assertThat(ids).containsOnly(product2.getId()), colors -> assertThat(colors).containsOnly(TermStats.of("red", 1L)), sizes -> assertThat(sizes).containsOnly(TermStats.of("36.0", 1L))); }
@Test public void filtersByTerm() throws Exception { final ProductProjectionSearch search = ProductProjectionSearch.ofStaged() .plusQueryFilters(productModel -> productModel.allVariants().attribute().ofString(ATTR_NAME_COLOR).is("red")); testResultIds(search, resultIds -> assertThat(resultIds).containsOnly(product2.getId())); }
protected static PagedSearchResult<ProductProjection> executeSearch(final ProductProjectionSearch search) { final List<String> ids = asList(product1.getId(), product2.getId()); final ProductProjectionSearch sphereRequest = search.plusQueryFilters(productModel -> productModel.id().isIn(ids)); return client().executeBlocking(sphereRequest); }
@Test public void resultsArePaginated() throws Exception { final ProductProjectionSearch search = ProductProjectionSearch.ofStaged() .plusQueryFilters(productModel -> productModel.allVariants().attribute().ofString(ATTR_NAME_COLOR).isIn(asList("blue", "red"))) .withSort(sort -> sort.name().locale(ENGLISH).desc()) .withOffset(1L) .withLimit(1L); testResultIds(search, resultIds -> assertThat(resultIds).containsOnly(product2.getId())); }
private void sortCheck(final List<PriceDraft> prices1, final List<PriceDraft> prices2, final SortExpression<ProductProjection> sortExpression) { withProductOfPrices(prices1, product1 -> { withProductOfPrices(prices2, product2 -> { final ProductProjectionSearch search = ProductProjectionSearch.ofStaged() .withPriceSelection(PriceSelection.of(EUR).withPriceCountry(DE)) .plusQueryFilters(m -> m.id().isIn(asList(product1.getId(), product2.getId()))) .plusSort(sortExpression); assertEventually(() -> { final PagedSearchResult<ProductProjection> result = client().executeBlocking(search); assertThat(result.getResults()).extracting(ResourceView::getId) .as("product1 with small price included").containsExactly(product1.getId(), product2.getId()); }); }); }); }