implement better search

This commit is contained in:
Hannes Huber 2020-05-29 11:06:00 +02:00
parent 1a1502b38c
commit 9238162a4c
3 changed files with 46 additions and 2 deletions

View File

@ -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<Article> searchByTerm(String sourceTerm, ArticleRepository repository) {
List<String> terms = Arrays.asList(sourceTerm.split(" "));
List<Article> resultArticles = new ArrayList<>();
terms.forEach(term -> {
List<Article> titleArticles = repository.getArticlesByTermInTitle(term); //search in Title
titleArticles.forEach(article -> {
if(!resultArticles.contains(article)){
resultArticles.add(article);
}
});
});
terms.forEach(term -> {
List<Article> descArticles = repository.getArticlesByTermInDescription(term); //search by Term
descArticles.forEach(article -> {
if(!resultArticles.contains(article)){
resultArticles.add(article);
}
});
});
return resultArticles;
}
}

View File

@ -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<Article> articles = articleRepository.getArticlesByTerm(term); //search by Term
List<Article> articles = SearchByTermAction.searchByTerm(term, articleRepository);
model.addAttribute("articles", articles);
} else if (category != null) { //if search by Category
List<Article> articles = articleRepository.getArticlesByCategory(category); //search by Category

View File

@ -31,7 +31,10 @@ public interface ArticleRepository extends JpaRepository<Article, Long> {
Optional<Integer> 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<Article> getArticlesByTerm(String term);
List<Article> 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<Article> 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<Article> getArticlesByCategory(String category);