@Test public void simpleQueryTest() { List<Product> baseList = SolrTestUtils.createProductList(10); repo.save(baseList); Assert.assertEquals(baseList.size(), TEST_RECORD_COUNT); Assert.assertEquals(INITIAL_RECORD_COUNT + TEST_RECORD_COUNT, repo.count()); List<Product> productsByCategory = repo.findProductsBySimpleQuery("cat:test"); Assert.assertEquals(TEST_RECORD_COUNT, productsByCategory.size()); }
@Test public void testProductCRUD() { // create local product object Product product = SolrTestUtils.createProduct(PRODUCT_ID); // save product to Solr Index and confirm index count increased by 1 repo.save(product); Assert.assertEquals(INITIAL_RECORD_COUNT + 1, repo.count()); // find single product from Solr Product loaded = repo.findOne(Integer.toString(PRODUCT_ID)); Assert.assertEquals(product.getName(), loaded.getName()); // update product name in Solr and confirm index count not changed loaded.setName("changed named"); repo.save(loaded); Assert.assertEquals(INITIAL_RECORD_COUNT + 1, repo.count()); // retrieve product from Solr and confirm name change loaded = repo.findOne(Integer.toString(PRODUCT_ID)); Assert.assertEquals("changed named", loaded.getName()); // delete the test product in Solr and confirm index count equal to initial count repo.delete(loaded); Assert.assertEquals(INITIAL_RECORD_COUNT, repo.count()); }
@Test public void testFacetQuery() { FacetPage<Product> facetPage = repo.findProductCategoryFacets(new PageRequest(0, 100)); Assert.assertEquals(repo.findAllProducts().size(), facetPage.getNumberOfElements()); Page<FacetFieldEntry> page = facetPage.getFacetResultPage(SolrProductField.CATEGORY); Assert.assertEquals(INITIAL_CATEGORY_COUNT, page.getNumberOfElements()); for (FacetFieldEntry entry : page) { Assert.assertEquals(SolrProductField.CATEGORY.getName(), entry.getField().getName()); Assert.assertEquals(repo.findByCategory(entry.getValue()).size(), entry.getValueCount()); } }
@Test public void testCustomQueries() { // Named Query from named-queries.properties List<Product> products = repo.findByNameOrCategory(SOLR_STRING, sortByIdDesc()); Assert.assertEquals(1, products.size()); // Method Name Query test for findByPopularityGreaterThanEqual() Product product = SolrTestUtils.createProduct(PRODUCT_ID); repo.save(product); Page<Product> popularProducts = repo.findByPopularityGreaterThanEqual(10000, new PageRequest(0, 10)); Assert.assertEquals(1, popularProducts.getTotalElements()); Assert.assertEquals(Integer.toString(PRODUCT_ID), popularProducts.getContent().get(0).getId()); }
@Override public List<Product> getProducts() { logger.debug("Retrieving all records and filtering out by 'doctype:product'"); List<Product> products = Lists.newArrayList(productRepo.findAll()); return products .stream() .filter(p -> p.getDoctype().equals(SolrDocType.PRODUCT)) .collect(Collectors.toList()); }
@Test public void highlightedResultsShouldContainTermsInBold() { List<Product> baseList = SolrTestUtils.createProductList(10); repo.save(baseList); HighlightPage<Product> highlightProductPage = productService.findByHighlightedName("product", new PageRequest(0, 20)); assertTrue(containsSnipplet(highlightProductPage, "<b>product</b>")); }
@Override public List<Product> getAvailableProducts() { logger.debug("Retrieving all available products where inStock:true"); List<Product> products = productRepo.findByAvailableTrueAndDoctype(SolrDocType.PRODUCT); return products; // List<Product> products = productRepo.findByAvailableTrue(); // return products.stream().filter(p -> // p.getDoctype().equals(SolrDocType.PRODUCT)).collect(Collectors.toList()); }
@Override public Page<Product> getProductsByPopularity(int popularity) { logger.debug("Using JPA Method Name Query - findByPopularityGreaterThanEqual()"); return productRepo.findByPopularityGreaterThanEqual(popularity, new PageRequest(0, 10)); }
@Override public List<Product> getProductsByNameOrCategoryAnnotatedQuery(String searchTerm) { logger.debug("Using annotated @query - ('(name:*?0* OR cat:*?0*) AND doctype:product'"); return productRepo.findByAnnotatedQuery(searchTerm, sortByIdDesc()); }
@Override public Iterable<Product> getProductsByNameOrCategory(String searchTerm) { logger.debug("Using 'Product.findByNameOrCategory' named query - ('name:*?0* OR cat:*?0*')"); return productRepo.findByNameOrCategory(searchTerm, sortByIdDesc()); }
@Override public void updateProductName(Product product) { productRepo.updateProductName(product); }
@Override public List<Product> getProductsByQuery() { logger.debug("Retrieving all products by solr @Query"); return productRepo.findAllProducts(); }
@Override public Page<Product> getTestRecords() { return productRepo.findTestCategoryRecords(); }
@Override public FacetPage<Product> getFacetedProductsCategory() { logger.debug("Retrieving faceted products by category"); return productRepo.findProductCategoryFacets(new PageRequest(0, 20)); // return simpleProductRepo.findByFacetOnCategory(); }
@Override public List<Product> searchWithCriteria(String searchTerm) { return productRepo.searchWithCriteria(searchTerm); }
@Override public List<Product> getProductsByStartOfName(String nameStart) { logger.debug("Named Method Query - findByNameStartingWith()"); return productRepo.findByNameStartingWith(nameStart); }
@Override public Product getProduct(String Id) { return productRepo.findOne(Id); }