투표 쿼리수정 필터링 추가

This commit is contained in:
khj0414 2025-03-27 13:09:50 +09:00
parent 4fdc97fde5
commit 373a8c6a5e
2 changed files with 85 additions and 34 deletions

View File

@ -19,7 +19,9 @@ import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.company.localhost.common.dto.MapDto;
import io.company.localhost.mapper.commoncodMapper;
@ -37,12 +39,8 @@ public class worddictyService {
public List<MapDto> selectWordList(MapDto map) {
List<MapDto> wordList = worddictymapper.selectWordList(map);
for (MapDto word : wordList) {
Object content = word.get("WRDDICCON");
// BlobUtil.procBlobToString으로 content 변환
String contentString = BlobUtil.procBlobToString(content);
word.put("WRDDICCON", contentString);
}
String keyword = map.getString("searchKeyword");
List<MapDto> processedList = new ArrayList<>();
// 데이터 가공
@ -66,7 +64,60 @@ public class worddictyService {
processedDto.putAll(dto);
processedList.add(processedDto);
}
return processedList;
return filterPostsByKeyword(processedList,keyword);
}
/**
* 검색어로 순수 텍스트 기준 게시글 필터링
*/
private List<MapDto> filterPostsByKeyword(List<MapDto> posts, String keyword) {
keyword = keyword.trim();
List<MapDto> filtered = new ArrayList<>();
for (MapDto post : posts) {
String title = post.getString("WRDDICTTL");
Object content = post.get("WRDDICCON");
// BlobUtil.procBlobToString으로 content 변환
String contentString = BlobUtil.procBlobToString(content);
if(keyword != null || !keyword.trim().isEmpty()) {
String plainText = extractPlainText(contentString);
if ((title != null && title.contains(keyword)) ||plainText.contains(keyword)) {
post.put("WRDDICCON", contentString); // content는 원래대로 복원
filtered.add(post);
}
}
}
return filtered;
}
public String extractPlainText(String deltaJson) {
StringBuilder sb = new StringBuilder();
ObjectMapper mapper = new ObjectMapper();
try {
JsonNode root = mapper.readTree(deltaJson);
if (root != null && root.isArray()) {
for (JsonNode op : root) {
JsonNode insertNode = op.get("insert");
// insert가 문자열일 경우만 추출
if (insertNode != null && insertNode.isTextual()) {
String text = insertNode.asText();
if (!"\n".equals(text.trim())) { // 줄바꿈 제외
sb.append(text).append(" ");
}
}
// insert가 객체면 image일 가능성 제외
}
}
} catch (Exception e) {
e.printStackTrace(); // 실제 운영에서는 로깅 처리
}
return sb.toString().trim();
}
public Long insertWord(MapDto map) {

View File

@ -5,8 +5,8 @@
<!-- 검색어 조건 -->
<if test="searchKeyword != null and searchKeyword != ''">
and (
REGEXP_REPLACE(w.WRDDICTTL, '\\[\\{.*?"insert":"', '') LIKE CONCAT('%', #{searchKeyword}, '%')
OR REGEXP_REPLACE(w.WRDDICCON, '\\[\\{.*?"insert":"', '') LIKE CONCAT('%', #{searchKeyword}, '%')
w.WRDDICTTL LIKE CONCAT('%', #{searchKeyword}, '%')
OR w.WRDDICCON LIKE CONCAT('%', #{searchKeyword}, '%')
)
</if>
<!-- 색인표 조건 -->