Skip to content
Merged
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 @@ -26,18 +26,17 @@ public class KakaoNewsService {
private final ElasticsearchClient client;

/**
* μ‚¬μš©μžμ˜ ν‚€μ›Œλ“œλ₯Ό 리슀트둜 λ¬Άμ–΄ λ‰΄μŠ€ 검색 λ©”μ„œλ“œμ— μ „λ‹¬ν•©λ‹ˆλ‹€.
* Hot Fixed
*
*
* @param includeKeywords 포함 ν‚€μ›Œλ“œ
* @param blockKeywords μ œμ™Έ ν‚€μ›Œλ“œ
* @return
* @return λ‰΄μŠ€ 리슀트
*/
public List<NewsEsDocument> searchNews(List<String> includeKeywords, List<String> blockKeywords) {
try {

//μ „λ‚  κΈ°μ€€μœΌλ‘œ μ‹œκ°„μ„ μΈ‘μ •
LocalDate yesterday = LocalDate.now().minusDays(1);

// 포함 ν‚€μ›Œλ“œ 쿼리
Query includeKeywordQuery = Query.of(q -> q
.bool(b -> b
.should(includeKeywords.stream()
Expand All @@ -54,23 +53,6 @@ public List<NewsEsDocument> searchNews(List<String> includeKeywords, List<String
)
);

// μ œμ™Έ ν‚€μ›Œλ“œ 쿼리
Query excludeKeywordQuery = Query.of(q -> q
.bool(b -> b
.should(blockKeywords.stream()
.map(kw -> Query.of(q2 -> q2
.multiMatch(m -> m
.query(kw)
.fields("title", "summary", "content_url", "publisher")
.type(TextQueryType.BoolPrefix)
)
))
.collect(Collectors.toList())
)
)
);

// λ‚ μ§œ ν•„ν„° 쿼리
Query dateFilter = Query.of(q -> q
.range(r -> r
.field("published_at")
Expand All @@ -80,16 +62,43 @@ public List<NewsEsDocument> searchNews(List<String> includeKeywords, List<String
)
);

// 전체 쿼리 μ‘°ν•©
Query finalQuery = Query.of(q -> q
.bool(b -> b
.must(includeKeywordQuery)
.must(dateFilter)
.mustNot(excludeKeywordQuery)
)
);
// 쿼리 리슀트 μ‘°ν•©
List<Query> mustQueries = new ArrayList<>();
mustQueries.add(includeKeywordQuery);
mustQueries.add(dateFilter);

// μ œμ™Έ ν‚€μ›Œλ“œκ°€ μžˆλŠ” κ²½μš°μ—λ§Œ must_not μΆ”κ°€
Query finalQuery;
if (blockKeywords != null && !blockKeywords.isEmpty()) {
Query excludeKeywordQuery = Query.of(q -> q
.bool(b -> b
.should(blockKeywords.stream()
.map(kw -> Query.of(q2 -> q2
.multiMatch(m -> m
.query(kw)
.fields("title", "summary", "content_url", "publisher")
.type(TextQueryType.BoolPrefix)
)
))
.collect(Collectors.toList())
)
)
);

finalQuery = Query.of(q -> q
.bool(b -> b
.must(mustQueries)
.mustNot(excludeKeywordQuery)
)
);
} else {
finalQuery = Query.of(q -> q
.bool(b -> b
.must(mustQueries)
)
);
}

// 검색 μš”μ²­
SearchRequest request = SearchRequest.of(s -> s
.index("news-index-nori")
.query(finalQuery)
Expand All @@ -99,15 +108,12 @@ public List<NewsEsDocument> searchNews(List<String> includeKeywords, List<String
)
);

// 검색 μˆ˜ν–‰
SearchResponse<NewsEsDocument> response = client.search(request, NewsEsDocument.class);

// 둜그: μŠ€μ½”μ–΄ 확인
response.hits().hits().forEach(hit ->
log.info("{} | score: {}", hit.source().getTitle(), hit.score())
);

// κ²°κ³Ό λ°˜ν™˜
return response.hits().hits().stream()
.map(hit -> hit.source())
.collect(Collectors.toList());
Expand All @@ -117,4 +123,100 @@ public List<NewsEsDocument> searchNews(List<String> includeKeywords, List<String
return List.of();
}
}

// ======================= Deprecated =========================

// /**
// * μ‚¬μš©μžμ˜ ν‚€μ›Œλ“œλ₯Ό 리슀트둜 λ¬Άμ–΄ λ‰΄μŠ€ 검색 λ©”μ„œλ“œμ— 전달
// *
// * @param includeKeywords 포함 ν‚€μ›Œλ“œ
// * @param blockKeywords μ œμ™Έ ν‚€μ›Œλ“œ
// * @return
// */
// public List<NewsEsDocument> searchNews(List<String> includeKeywords, List<String> blockKeywords) {
// try {
//
// /* μ „λ‚  κΈ°μ€€μœΌλ‘œ μ‹œκ°„μ„ μΈ‘μ • */
// LocalDate yesterday = LocalDate.now().minusDays(1);
//
// /* 포함 ν‚€μ›Œλ“œ 쿼리 */
// Query includeKeywordQuery = Query.of(q -> q
// .bool(b -> b
// .should(includeKeywords.stream()
// .map(kw -> Query.of(q2 -> q2
// .multiMatch(m -> m
// .query(kw)
// .fields("title", "summary", "content_url", "publisher")
// .type(TextQueryType.BoolPrefix)
// )
// ))
// .collect(Collectors.toList())
// )
// .minimumShouldMatch("1")
// )
// );
//
// /* μ œμ™Έ ν‚€μ›Œλ“œ 쿼리 */
// Query excludeKeywordQuery = Query.of(q -> q
// .bool(b -> b
// .should(blockKeywords.stream()
// .map(kw -> Query.of(q2 -> q2
// .multiMatch(m -> m
// .query(kw)
// .fields("title", "summary", "content_url", "publisher")
// .type(TextQueryType.BoolPrefix)
// )
// ))
// .collect(Collectors.toList())
// )
// )
// );
//
// /* λ‚ μ§œ ν•„ν„° 쿼리 */
// Query dateFilter = Query.of(q -> q
// .range(r -> r
// .field("published_at")
// .gte(JsonData.of(yesterday.toString()))
// .lte(JsonData.of(yesterday.toString()))
// .format("yyyy-MM-dd")
// )
// );
//
// /* 전체 쿼리 μ‘°ν•© */
// Query finalQuery = Query.of(q -> q
// .bool(b -> b
// .must(includeKeywordQuery)
// .must(dateFilter)
// .mustNot(excludeKeywordQuery)
// )
// );
//
// /* 검색 μš”μ²­ */
// SearchRequest request = SearchRequest.of(s -> s
// .index("news-index-nori")
// .query(finalQuery)
// .size(5)
// .sort(sort -> sort
// .score(sc -> sc.order(co.elastic.clients.elasticsearch._types.SortOrder.Desc))
// )
// );
//
// /* 검색 μˆ˜ν–‰ */
// SearchResponse<NewsEsDocument> response = client.search(request, NewsEsDocument.class);
//
// /* 둜그: μŠ€μ½”μ–΄ 확인 */
// response.hits().hits().forEach(hit ->
// log.info("{} | score: {}", hit.source().getTitle(), hit.score())
// );
//
// /* κ²°κ³Ό λ°˜ν™˜ */
// return response.hits().hits().stream()
// .map(hit -> hit.source())
// .collect(Collectors.toList());
//
// } catch (IOException e) {
// log.error("ν‚€μ›Œλ“œ 기반 λ‰΄μŠ€ 검색 μ‹€νŒ¨: {}", e.getMessage(), e);
// return List.of();
// }
// }
}