@Test public void canAccessCategories() throws Exception { assertThat(FACET_MODEL.categories().id().allTerms().expression()).isEqualTo("categories.id"); assertThat(FILTER_MODEL.categories().id().is("some-id")).extracting(expression()).containsExactly("categories.id:\"some-id\""); }
@SuppressWarnings("unchecked") @Test public void isInSubtree() { withCategoryAndParentCategory(client(), (Category cat, Category parent) -> { withProduct(client(), builder -> builder.categoriesAsObjectList(singletonList(cat)), product -> { final String parentId = parent.getId(); assertThat(product.getMasterData().getStaged().getCategories()) .contains(cat.toReference()) .doesNotContain(parent.toReference()); assertEventually(() -> { //search for products directly in parent category final ProductProjectionSearch requestForDirectCategories = ProductProjectionSearch.ofStaged() .withQueryFilters(m -> m.categories().id().is(parentId)); assertThat(client().executeBlocking(requestForDirectCategories).getResults()).isEmpty(); //search for products in parent category or its descendants final ProductProjectionSearch requestForTree = ProductProjectionSearch.ofStaged() .withQueryFilters(m -> m.categories().id().isInSubtree(parentId)); final List<ProductProjection> results = client().executeBlocking(requestForTree).getResults(); assertThat(results).hasSize(1); }); }); }); } }
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(); }