Skip to content

Commit

Permalink
[FIX] fix ootd search query
Browse files Browse the repository at this point in the history
  • Loading branch information
CokeLee777 committed Jan 18, 2024
1 parent b06e702 commit f260543
Showing 1 changed file with 33 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -335,40 +335,49 @@ public int softBulkDeleteByIds(List<Long> ids) {
@Override
public Page<Post> findAllBySearchQueryAndIdAscAndIsDeletedFalse(String query, Pageable pageable) {
JPAQuery<Long> indexQuery =
jpaQueryFactory
.selectDistinct(post.id)
.from(post)
.leftJoin(post.hashTags, hashTag)
.leftJoin(post.member, member)
.where(post.title.eq(query)
.or(hashTag.name.eq(query))
.or(member.nickname.eq(query)).and(post.isDeleted.eq(false)))
.orderBy(getOrderCondition(pageable.getSort()).toArray(OrderSpecifier[]::new))
.offset(pageable.getOffset())
.limit(pageable.getPageSize());
jpaQueryFactory
.select(post.id)
.from(post)
.innerJoin(post.hashTags, hashTag)
.innerJoin(post.member, member)
.where(
(post.title
.contains(query)
.or(hashTag.name.contains(query))
.or(member.nickname.contains(query))
.and(post.isDeleted.eq(false))))
.orderBy(getOrderCondition(pageable.getSort()).toArray(OrderSpecifier[]::new))
.offset(pageable.getOffset())
.limit(pageable.getPageSize());
List<Long> indexes = indexQuery.fetch();
if (indexes.isEmpty()) {
return new PageImpl<>(new ArrayList<>(), pageable, 0);
}

JPAQuery<Post> resultQuery =
jpaQueryFactory
.selectDistinct(post)
.from(post)
.leftJoin(post.postImage, postImage)
.fetchJoin()
.leftJoin(post.hashTags, hashTag)
.fetchJoin()
.leftJoin(post.member, member)
.fetchJoin()
.where(post.id.in(indexes))
.orderBy(getOrderCondition(pageable.getSort()).toArray(OrderSpecifier[]::new));
jpaQueryFactory
.selectDistinct(post)
.from(post)
.leftJoin(post.postImage, postImage)
.fetchJoin()
.leftJoin(post.hashTags, hashTag)
.fetchJoin()
.leftJoin(post.member, member)
.fetchJoin()
.where(post.id.in(indexes))
.orderBy(getOrderCondition(pageable.getSort()).toArray(OrderSpecifier[]::new));

List<Post> result = resultQuery.fetch();

JPAQuery<Long> countQuery =
jpaQueryFactory.select(post.id).from(post)
.where(post.id.in(indexes).and(post.isDeleted.eq(false)));
jpaQueryFactory
.select(post.id)
.from(post)
.innerJoin(post.hashTags, hashTag)
.innerJoin(post.member, member)
.where(
(post.title.contains(query).or(hashTag.name.contains(query)).or(member.nickname.contains(query)))
.and(post.isDeleted.eq(false)));

long total = countQuery.fetchCount();
return new PageImpl<>(result, pageable, total);
Expand Down

0 comments on commit f260543

Please sign in to comment.