From ac63c62ccb34cf517e344479d9f4ec598a994c0c Mon Sep 17 00:00:00 2001 From: Le Phuc Date: Wed, 17 Jul 2024 11:00:52 +0700 Subject: [PATCH] search --- .../example/controllers/SearchController.java | 29 ++++++++++++++ .../org/example/objects/SearchResultDTO.java | 36 +++++++++++++++++ .../repositories/CategoryRepository.java | 3 ++ .../example/repositories/PostRepository.java | 3 ++ .../repositories/SearchRepository.java | 23 +++++++++++ .../services/RecommendationService.java | 2 +- .../org/example/services/SearchService.java | 40 +++++++++++++++++++ 7 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 BE/src/main/java/org/example/controllers/SearchController.java create mode 100644 BE/src/main/java/org/example/objects/SearchResultDTO.java create mode 100644 BE/src/main/java/org/example/repositories/SearchRepository.java create mode 100644 BE/src/main/java/org/example/services/SearchService.java diff --git a/BE/src/main/java/org/example/controllers/SearchController.java b/BE/src/main/java/org/example/controllers/SearchController.java new file mode 100644 index 0000000..5bbbfb7 --- /dev/null +++ b/BE/src/main/java/org/example/controllers/SearchController.java @@ -0,0 +1,29 @@ +package org.example.controllers; + + +import ch.qos.logback.core.joran.sanity.Pair; +import org.example.models.Post; +import org.example.models.User; +import org.example.objects.SearchResultDTO; +import org.example.services.SearchService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/search") +public class SearchController { + @Autowired + private final SearchService searchService; + + public SearchController(SearchService searchService) { + this.searchService = searchService; + } + + @GetMapping("/get") + public List search(@RequestParam("condition") String condition) { + System.out.println(condition); + return searchService.searchResult(condition); + } +} diff --git a/BE/src/main/java/org/example/objects/SearchResultDTO.java b/BE/src/main/java/org/example/objects/SearchResultDTO.java new file mode 100644 index 0000000..6c1246b --- /dev/null +++ b/BE/src/main/java/org/example/objects/SearchResultDTO.java @@ -0,0 +1,36 @@ +package org.example.objects; + +import org.example.models.Post; +import org.example.models.User; +import org.springframework.data.neo4j.core.schema.GeneratedValue; +import org.springframework.data.neo4j.core.schema.Id; +import org.springframework.web.bind.annotation.GetMapping; + +public class SearchResultDTO { + @Id + @GeneratedValue + private Long id; + private User user; + private Post post; + + public SearchResultDTO(Post post, User user) { + this.user = user; + this.post = post; + } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + + public Post getPost() { + return post; + } + + public void setPost(Post post) { + this.post = post; + } +} diff --git a/BE/src/main/java/org/example/repositories/CategoryRepository.java b/BE/src/main/java/org/example/repositories/CategoryRepository.java index f4447c8..613a9b4 100644 --- a/BE/src/main/java/org/example/repositories/CategoryRepository.java +++ b/BE/src/main/java/org/example/repositories/CategoryRepository.java @@ -20,4 +20,7 @@ public interface CategoryRepository extends Neo4jRepository { "OPTIONAL MATCH (p)-[:HAS_VIDEO]->(video:Video)\n" + "RETURN ID(p)") List findPostsByCategoryNames(@Param("categoryName") String categoryName); + + @Query("MATCH (post:Post)-[HAS_CATEGORY]->(cate:Category) WHERE ID(post)=$postId return cate") + Category getCategory(@Param("postId") Long postId); } diff --git a/BE/src/main/java/org/example/repositories/PostRepository.java b/BE/src/main/java/org/example/repositories/PostRepository.java index b684ca5..970b45a 100644 --- a/BE/src/main/java/org/example/repositories/PostRepository.java +++ b/BE/src/main/java/org/example/repositories/PostRepository.java @@ -1,6 +1,7 @@ package org.example.repositories; import org.example.controllers.UserController; +import org.example.models.Category; import org.example.models.Image; import org.example.models.Post; import org.example.queryresults.PostQueryResult; @@ -28,6 +29,8 @@ public interface PostRepository extends Neo4jRepository { "return image.filename") String getImage(@Param("postId") Long postId); + + @Query("MATCH (p:Post ) where ID(p)=$postId \n" + "SET p.title = $title,\n" + " p.content = $content\n" + diff --git a/BE/src/main/java/org/example/repositories/SearchRepository.java b/BE/src/main/java/org/example/repositories/SearchRepository.java new file mode 100644 index 0000000..7ab88b7 --- /dev/null +++ b/BE/src/main/java/org/example/repositories/SearchRepository.java @@ -0,0 +1,23 @@ +package org.example.repositories; + +import ch.qos.logback.core.joran.sanity.Pair; +import org.example.models.Post; +import org.example.models.User; +import org.example.objects.SearchResultDTO; +import org.springframework.data.neo4j.repository.Neo4jRepository; +import org.springframework.data.neo4j.repository.query.Query; +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.Map; +import java.util.Objects; + +@Repository +public interface SearchRepository extends Neo4jRepository { + @Query("MATCH (p:Post)<-[:createPost]-(u:User)\n" + + "WHERE p.title CONTAINS $condition OR p.content CONTAINS $condition OR u.username CONTAINS $condition \n" + + "RETURN p \n") + List search(@Param("condition") String condition); +} diff --git a/BE/src/main/java/org/example/services/RecommendationService.java b/BE/src/main/java/org/example/services/RecommendationService.java index e35b5d0..ce3b17b 100644 --- a/BE/src/main/java/org/example/services/RecommendationService.java +++ b/BE/src/main/java/org/example/services/RecommendationService.java @@ -37,7 +37,7 @@ public class RecommendationService { for (Post post : postsInteracts) { post.setImgUrl(postRepository.getImage(post.getId())); recommendationPosts.add(post); - } + } //tìm kiếm các bài post có chung chủ đề List categoriesNames = userRepository.findInteractedCategories(username); for (String categoryName : categoriesNames) { diff --git a/BE/src/main/java/org/example/services/SearchService.java b/BE/src/main/java/org/example/services/SearchService.java new file mode 100644 index 0000000..7376a67 --- /dev/null +++ b/BE/src/main/java/org/example/services/SearchService.java @@ -0,0 +1,40 @@ +package org.example.services; + + +import ch.qos.logback.core.joran.sanity.Pair; +import org.example.models.Post; +import org.example.models.User; +import org.example.objects.SearchResultDTO; +import org.example.repositories.CategoryRepository; +import org.example.repositories.PostRepository; +import org.example.repositories.SearchRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.stream.Collectors; + +@Service +public class SearchService { + @Autowired + private final SearchRepository searchRepository; + @Autowired + private final PostRepository postRepository; + @Autowired + private final CategoryRepository categoryRepository; + + public SearchService(SearchRepository searchRepository, PostRepository postRepository,CategoryRepository categoryRepository) { + this.searchRepository = searchRepository; + this.postRepository = postRepository; + this.categoryRepository = categoryRepository; + } + public List searchResult(String condition) { + List posts= searchRepository.search(condition); + for(Post post: posts) { + post.setImgUrl(postRepository.getImage(post.getId())); + post.setCategory(categoryRepository.getCategory(post.getId())); + } + return posts; + } + +}