Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ ORDER BY COUNT(cpl.id) DESC, cp.viewCount DESC
@EntityGraph(attributePaths = {"communityPostImageList"})
Page<CommunityPost> findAll(Pageable pageable);

@EntityGraph(attributePaths = {"communityPostImageList"})
Page<CommunityPost> findByTitleContainingOrContentContaining(
String titleKeyword, String contentKeyword, Pageable pageable);

Page<CommunityPost> findAllByUserIdOrderByCreatedDateDesc(Long userId, Pageable pageable);

@Modifying(clearAutomatically = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
Expand Down Expand Up @@ -258,30 +256,35 @@ public Page<CommunityPostListResponse> searchCommunityPost(
}

// Elasticsearch에서 검색어로 커뮤니티 게시글 페이징해서 가져옴
Pageable pageable = PageRequest.of(page, size);
List<Long> communitySearchResponses =
openSearchService.searchCommunityPost(keyword, page, size);
// Pageable pageable = PageRequest.of(page, size);
// List<Long> communitySearchResponses =
// openSearchService.searchCommunityPost(keyword, page, size);
//
// if (communitySearchResponses.isEmpty()) {
// return new PageImpl<>(List.of(), pageable, 0);
// }
//
// Map<Long, CommunityPost> postMap =
// communityPostRepository.findByIdIn(communitySearchResponses).stream()
// .collect(Collectors.toMap(CommunityPost::getId, post -> post));
//
// // 순서 유지: 검색 결과에 있는 ID 순서대로 매핑
// List<CommunityPostListResponse> results =
// communitySearchResponses.stream()
// .map(
// id ->
// Optional.ofNullable(postMap.get(id))
// .map(CommunityPostListResponse::from)
// .orElse(null))
// .filter(Objects::nonNull)
// .toList();
//
// return new PageImpl<>(results, pageable, communitySearchResponses.size());

if (communitySearchResponses.isEmpty()) {
return new PageImpl<>(List.of(), pageable, 0);
}

Map<Long, CommunityPost> postMap =
communityPostRepository.findByIdIn(communitySearchResponses).stream()
.collect(Collectors.toMap(CommunityPost::getId, post -> post));

// 순서 유지: 검색 결과에 있는 ID 순서대로 매핑
List<CommunityPostListResponse> results =
communitySearchResponses.stream()
.map(
id ->
Optional.ofNullable(postMap.get(id))
.map(CommunityPostListResponse::from)
.orElse(null))
.filter(Objects::nonNull)
.toList();

return new PageImpl<>(results, pageable, communitySearchResponses.size());
Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createdDate"));
return communityPostRepository
.findByTitleContainingOrContentContaining(keyword, keyword, pageable)
.map(CommunityPostListResponse::from);
}

public List<StoreRecentSearchKeyword> getRecentSearchKeywords(Long userId) {
Expand Down
Loading