@@ -45,7 +45,7 @@ public Page<Post> searchPosts(PostSearchCondition condition, Pageable pageable)
4545 }
4646
4747 /**
48- * Full-Text Search가 필요한지 판단
48+ * 제목, 제목+내용 + 검색어 검색 시 Full-Text Search 적용
4949 */
5050 private boolean isFullTextSearchRequired (PostSearchCondition condition ) {
5151 return StringUtils .hasText (condition .keyword ())
@@ -64,7 +64,6 @@ private Page<Post> searchPostsWithFullText(PostSearchCondition condition, Pageab
6464 Query dataQuery = em .createNativeQuery (dataSql , Post .class );
6565 setQueryParameters (dataQuery , condition , tsQuery , pageable );
6666
67- @ SuppressWarnings ("unchecked" )
6867 List <Post > posts = dataQuery .getResultList ();
6968
7069 // 전체 카운트 조회
@@ -158,22 +157,29 @@ private void setQueryParameters(Query query, PostSearchCondition condition, Stri
158157 * 전체 개수 조회
159158 */
160159 private long countWithFullText (PostSearchCondition condition , String tsQuery ) {
160+ // 10,000건 이상이면 "10,000+" 표시
161+ long maxCount = 10000 ;
162+
161163 StringBuilder sql = new StringBuilder ();
162- sql .append ("SELECT COUNT(*) FROM post p " );
163- sql .append ("WHERE 1=1 " );
164+ sql .append ("SELECT COUNT(*) FROM (" );
165+ sql .append (" SELECT 1 FROM post p " );
166+ sql .append (" WHERE 1=1 " );
164167
165168 if (condition .category () != null ) {
166- sql .append ("AND p.category = :category " );
169+ sql .append (" AND p.category = :category " );
167170 }
168171
169- sql .append ("AND " ).append (getFullTextCondition (condition .searchType ()));
172+ sql .append (" AND " ).append (getFullTextCondition (condition .searchType ()));
173+ sql .append (" LIMIT :maxCount" );
174+ sql .append (") subquery" );
170175
171176 Query countQuery = em .createNativeQuery (sql .toString ());
172177
173178 if (condition .category () != null ) {
174179 countQuery .setParameter ("category" , condition .category ().name ());
175180 }
176181 countQuery .setParameter ("tsQuery" , tsQuery );
182+ countQuery .setParameter ("maxCount" , maxCount );
177183
178184 return ((Number ) countQuery .getSingleResult ()).longValue ();
179185 }
0 commit comments