Skip to content

Commit ffd2769

Browse files
committed
feature(search): 검색 keyword가 null인 경우 전체 조회
1 parent 8cb0b02 commit ffd2769

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

src/main/java/com/somemore/community/repository/board/CommunityBoardDocumentRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.List;
88

99
public interface CommunityBoardDocumentRepository extends ElasticsearchRepository<CommunityBoardDocument, Long> {
10+
List<CommunityBoardDocument> findAll();
1011
@Query("{\"multi_match\": {\"query\": \"?0\", \"fields\": [\"title\", \"content\"]}}")
1112
List<CommunityBoardDocument> findIdsByTitleOrContentContaining(String keyword);
1213
}

src/main/java/com/somemore/community/repository/board/CommunityBoardRepositoryImpl.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public boolean existsById(Long id) {
8888

8989
@Override
9090
public Page<CommunityBoardView> findByCommunityBoardsContaining(String keyword, Pageable pageable) {
91-
List<CommunityBoardDocument> boardDocuments = documentRepository.findIdsByTitleOrContentContaining(keyword);
91+
List<CommunityBoardDocument> boardDocuments = getBoardDocuments(keyword);
9292

9393
List<Long> boardIds = boardDocuments.stream()
9494
.map(CommunityBoardDocument::getId)
@@ -105,7 +105,8 @@ public Page<CommunityBoardView> findByCommunityBoardsContaining(String keyword,
105105
.select(communityBoard.count())
106106
.from(communityBoard)
107107
.join(volunteer).on(communityBoard.writerId.eq(volunteer.id))
108-
.where(communityBoard.id.in(boardIds));
108+
.where(communityBoard.id.in(boardIds)
109+
.and(isNotDeleted()));
109110

110111
return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchOne);
111112
}
@@ -160,4 +161,12 @@ private BooleanExpression isNotDeleted() {
160161
}
161162

162163
private BooleanExpression isWriter(UUID writerId) {return communityBoard.writerId.eq(writerId); }
164+
165+
private List<CommunityBoardDocument> getBoardDocuments(String keyword) {
166+
167+
if (keyword == null || keyword.isEmpty()) {
168+
return documentRepository.findAll();
169+
}
170+
return documentRepository.findIdsByTitleOrContentContaining(keyword);
171+
}
163172
}

src/main/java/com/somemore/recruitboard/repository/RecruitBoardDocumentRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.List;
88

99
public interface RecruitBoardDocumentRepository extends ElasticsearchRepository<RecruitBoardDocument, Long> {
10+
List<RecruitBoardDocument> findAll();
1011
@Query("{\"multi_match\": {\"query\": \"?0\", \"fields\": [\"title\", \"content\"]}}")
1112
List<RecruitBoardDocument> findIdsByTitleOrContentContaining(String keyword);
1213
}

src/main/java/com/somemore/recruitboard/repository/RecruitBoardRepositoryImpl.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,15 @@ public Page<RecruitBoardDetail> findAllNearby(RecruitBoardNearByCondition condit
138138
QLocation location = QLocation.location;
139139
QCenter center = QCenter.center;
140140

141+
List<RecruitBoardDocument> boardDocuments = getBoardDocuments(condition.keyword());
142+
143+
List<Long> boardIds = boardDocuments.stream()
144+
.map(RecruitBoardDocument::getId)
145+
.toList();
146+
141147
Pageable pageable = condition.pageable();
142148

143149
BooleanExpression predicate = locationBetween(condition)
144-
.and(keywordEq(condition.keyword()))
145150
.and(statusEq(condition.status()))
146151
.and(isNotDeleted());
147152

@@ -150,7 +155,8 @@ public Page<RecruitBoardDetail> findAllNearby(RecruitBoardNearByCondition condit
150155
.from(recruitBoard)
151156
.join(location).on(recruitBoard.locationId.eq(location.id))
152157
.join(center).on(recruitBoard.centerId.eq(center.id))
153-
.where(predicate)
158+
.where(recruitBoard.id.in(boardIds)
159+
.and(predicate))
154160
.offset(pageable.getOffset())
155161
.limit(pageable.getPageSize())
156162
.orderBy(toOrderSpecifiers(pageable.getSort()))
@@ -161,7 +167,8 @@ public Page<RecruitBoardDetail> findAllNearby(RecruitBoardNearByCondition condit
161167
.from(recruitBoard)
162168
.join(location).on(recruitBoard.locationId.eq(location.id))
163169
.join(center).on(recruitBoard.centerId.eq(center.id))
164-
.where(predicate);
170+
.where(recruitBoard.id.in(boardIds)
171+
.and(predicate));
165172

166173
return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchOne);
167174
}
@@ -197,11 +204,12 @@ public Page<RecruitBoard> findAllByCenterId(UUID centerId,
197204
}
198205

199206
@Override
200-
public Page<RecruitBoardWithCenter> findByRecruitBoardsContaining(String keyword, RecruitBoardSearchCondition condition) {
207+
public Page<RecruitBoardWithCenter> findByRecruitBoardsContaining(RecruitBoardSearchCondition condition) {
201208
QRecruitBoard recruitBoard = QRecruitBoard.recruitBoard;
202209
QCenter center = QCenter.center;
203210

204-
List<RecruitBoardDocument> boardDocuments = documentRepository.findIdsByTitleOrContentContaining(keyword);
211+
212+
List<RecruitBoardDocument> boardDocuments = getBoardDocuments(condition.keyword());
205213

206214
List<Long> boardIds = boardDocuments.stream()
207215
.map(RecruitBoardDocument::getId)
@@ -229,7 +237,8 @@ public Page<RecruitBoardWithCenter> findByRecruitBoardsContaining(String keyword
229237
.select(recruitBoard.count())
230238
.from(recruitBoard)
231239
.join(center).on(recruitBoard.centerId.eq(center.id))
232-
.where(predicate);
240+
.where(recruitBoard.id.in(boardIds)
241+
.and(predicate));
233242

234243
return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchOne);
235244
}
@@ -361,4 +370,12 @@ private List<RecruitBoardDocument> convertEntityToDocuments(List<RecruitBoard> r
361370
}
362371
return communityBoardDocuments;
363372
}
373+
374+
private List<RecruitBoardDocument> getBoardDocuments(String keyword) {
375+
376+
if (keyword == null || keyword.isEmpty()) {
377+
return documentRepository.findAll();
378+
}
379+
return documentRepository.findIdsByTitleOrContentContaining(keyword);
380+
}
364381
}

0 commit comments

Comments
 (0)