From 9238162a4c3d6b4c19cd99f0bde73ed0873228ad Mon Sep 17 00:00:00 2001 From: Hannes Date: Fri, 29 May 2020 11:06:00 +0200 Subject: [PATCH] implement better search --- .../action/shop/SearchByTermAction.java | 40 +++++++++++++++++++ .../controller/shop/ShopSearchController.java | 3 +- .../repos/shop/ArticleRepository.java | 5 ++- 3 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 prototype/src/main/java/org/hso/ecommerce/action/shop/SearchByTermAction.java diff --git a/prototype/src/main/java/org/hso/ecommerce/action/shop/SearchByTermAction.java b/prototype/src/main/java/org/hso/ecommerce/action/shop/SearchByTermAction.java new file mode 100644 index 0000000..cfdfffe --- /dev/null +++ b/prototype/src/main/java/org/hso/ecommerce/action/shop/SearchByTermAction.java @@ -0,0 +1,40 @@ +package org.hso.ecommerce.action.shop; + +import org.hso.ecommerce.entities.shop.Article; +import org.hso.ecommerce.repos.shop.ArticleRepository; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class SearchByTermAction { + + public static List
searchByTerm(String sourceTerm, ArticleRepository repository) { + + List terms = Arrays.asList(sourceTerm.split(" ")); + List
resultArticles = new ArrayList<>(); + + terms.forEach(term -> { + List
titleArticles = repository.getArticlesByTermInTitle(term); //search in Title + titleArticles.forEach(article -> { + if(!resultArticles.contains(article)){ + resultArticles.add(article); + } + }); + + }); + + terms.forEach(term -> { + List
descArticles = repository.getArticlesByTermInDescription(term); //search by Term + descArticles.forEach(article -> { + if(!resultArticles.contains(article)){ + resultArticles.add(article); + } + }); + + }); + + return resultArticles; + } +} diff --git a/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopSearchController.java b/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopSearchController.java index 1d14c61..ad1d107 100644 --- a/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopSearchController.java +++ b/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopSearchController.java @@ -1,5 +1,6 @@ package org.hso.ecommerce.controller.shop; +import org.hso.ecommerce.action.shop.SearchByTermAction; import org.hso.ecommerce.entities.shop.Article; import org.hso.ecommerce.repos.shop.ArticleRepository; import org.hso.ecommerce.repos.shop.CategoryRepository; @@ -32,7 +33,7 @@ public class ShopSearchController { model.addAttribute("categories", categoryRepository.getCategories()); //for sidebar if (term != null) { //if search by Term - List
articles = articleRepository.getArticlesByTerm(term); //search by Term + List
articles = SearchByTermAction.searchByTerm(term, articleRepository); model.addAttribute("articles", articles); } else if (category != null) { //if search by Category List
articles = articleRepository.getArticlesByCategory(category); //search by Category diff --git a/prototype/src/main/java/org/hso/ecommerce/repos/shop/ArticleRepository.java b/prototype/src/main/java/org/hso/ecommerce/repos/shop/ArticleRepository.java index 0340f59..a7f7597 100644 --- a/prototype/src/main/java/org/hso/ecommerce/repos/shop/ArticleRepository.java +++ b/prototype/src/main/java/org/hso/ecommerce/repos/shop/ArticleRepository.java @@ -31,7 +31,10 @@ public interface ArticleRepository extends JpaRepository { Optional findArticleIDByRelatedID(@Param("relatedId") long relatedId); @Query(value = "Select a.* from articles as a, warehouse_booking_position_entries as wbpe where wbpe.article_id = a.id and a.title LIKE %:term% group by wbpe.slot_id having max(wbpe.id) and wbpe.new_sum_slot != 0", nativeQuery = true) - List
getArticlesByTerm(String term); + List
getArticlesByTermInTitle(String term); + + @Query(value = "Select a.* from articles as a, warehouse_booking_position_entries as wbpe where wbpe.article_id = a.id and a.description LIKE %:term% group by wbpe.slot_id having max(wbpe.id) and wbpe.new_sum_slot != 0", nativeQuery = true) + List
getArticlesByTermInDescription(String term); @Query(value = "Select a.* from articles as a, categories as c, article_categories_bindings as acb, warehouse_booking_position_entries as wbpe where wbpe.article_id = a.id and acb.articles_id = a.id and acb.categories_id = c.id and c.name = :category group by wbpe.slot_id having max(wbpe.id) and wbpe.new_sum_slot != 0", nativeQuery = true) List
getArticlesByCategory(String category);