From ffebeb46b7ed1e1f1760de6904c27c98942a3317 Mon Sep 17 00:00:00 2001 From: Tim Date: Sun, 28 Sep 2025 15:08:20 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=96=B0=E5=A2=9E=E6=8B=BC=E9=9F=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/openisle/service/SearchService.java | 51 +++++++++++++++---- 1 file changed, 42 insertions(+), 9 deletions(-) diff --git a/backend/src/main/java/com/openisle/service/SearchService.java b/backend/src/main/java/com/openisle/service/SearchService.java index cc3372c64..61e845a32 100644 --- a/backend/src/main/java/com/openisle/service/SearchService.java +++ b/backend/src/main/java/com/openisle/service/SearchService.java @@ -343,7 +343,17 @@ public class SearchService { .preTags("") .postTags("") .fields("title", f -> f.fragmentSize(highlightFragmentSize()).numberOfFragments(1)) + .fields("title.py", f -> f.fragmentSize(highlightFragmentSize()).numberOfFragments(1)) .fields("content", f -> f.fragmentSize(highlightFragmentSize()).numberOfFragments(1)) + .fields("content.py", f -> + f.fragmentSize(highlightFragmentSize()).numberOfFragments(1) + ) + .fields("author", f -> f.numberOfFragments(0)) + .fields("author.py", f -> f.numberOfFragments(0)) + .fields("category", f -> f.numberOfFragments(0)) + .fields("category.py", f -> f.numberOfFragments(0)) + .fields("tags", f -> f.numberOfFragments(0)) + .fields("tags.py", f -> f.numberOfFragments(0)) ) .size(DEFAULT_OPEN_SEARCH_LIMIT > 0 ? DEFAULT_OPEN_SEARCH_LIMIT : 10), SearchDocument.class @@ -425,8 +435,10 @@ public class SearchService { return null; } Map> highlight = hit.highlight(); - String highlightedContent = firstHighlight(highlight, "content"); - String highlightedTitle = firstHighlight(highlight, "title"); + String highlightedContent = firstHighlight(highlight, "content", "content.py"); + String highlightedTitle = firstHighlight(highlight, "title", "title.py"); + String highlightedAuthor = firstHighlight(highlight, "author", "author.py"); + String highlightedCategory = firstHighlight(highlight, "category", "category.py"); boolean highlightTitle = highlightedTitle != null && !highlightedTitle.isBlank(); String documentType = document.type() != null ? document.type() : ""; String effectiveType = documentType; @@ -465,7 +477,18 @@ public class SearchService { String highlightedText = highlightTitle ? highlightedTitle : highlightHtml(document.title(), keyword); - String highlightedSubText = highlightHtml(subText, keyword); + String highlightedSubText; + if ("comment".equals(documentType)) { + highlightedSubText = highlightedAuthor != null && !highlightedAuthor.isBlank() + ? highlightedAuthor + : highlightHtml(subText, keyword); + } else if ("post".equals(documentType) || "post_title".equals(effectiveType)) { + highlightedSubText = highlightedCategory != null && !highlightedCategory.isBlank() + ? highlightedCategory + : highlightHtml(subText, keyword); + } else { + highlightedSubText = highlightHtml(subText, keyword); + } String highlightedExtra = snippetHtml != null ? snippetHtml : highlightHtml(snippet, keyword); return new SearchResult( effectiveType, @@ -480,15 +503,25 @@ public class SearchService { ); } - private String firstHighlight(Map> highlight, String field) { - if (highlight == null || field == null) { + private String firstHighlight(Map> highlight, String... fields) { + if (highlight == null || fields == null) { return null; } - List values = highlight.get(field); - if (values == null || values.isEmpty()) { - return null; + for (String field : fields) { + if (field == null) { + continue; + } + List values = highlight.get(field); + if (values == null || values.isEmpty()) { + continue; + } + for (String value : values) { + if (value != null && !value.isBlank()) { + return value; + } + } } - return values.get(0); + return null; } private String cleanHighlight(String value) {