From 16a83b9e0e658f1e354444b6f3922f372a6a195d Mon Sep 17 00:00:00 2001 From: John Niang Date: Tue, 7 Mar 2023 11:48:13 +0800 Subject: [PATCH] Improve the quality of full-text searching result (#3457) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? /kind improvement /area core #### What this PR does / why we need it: This PR uses QueryParser to build Query instead of FuzzyQuery to improve the searching result quality. image #### Which issue(s) this PR fixes: Fixes https://github.com/halo-dev/halo/issues/3234 #### Does this PR introduce a user-facing change? ```release-note 优化搜索查询结果 ``` --- .../app/search/post/LucenePostSearchService.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/run/halo/app/search/post/LucenePostSearchService.java b/src/main/java/run/halo/app/search/post/LucenePostSearchService.java index eeb9b512a..423d77a8d 100644 --- a/src/main/java/run/halo/app/search/post/LucenePostSearchService.java +++ b/src/main/java/run/halo/app/search/post/LucenePostSearchService.java @@ -23,7 +23,8 @@ import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.Term; -import org.apache.lucene.search.FuzzyQuery; +import org.apache.lucene.queryparser.classic.ParseException; +import org.apache.lucene.queryparser.classic.QueryParser; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.Sort; @@ -57,7 +58,7 @@ public class LucenePostSearchService implements PostSearchService, DisposableBea public LucenePostSearchService(HaloProperties haloProperties) throws IOException { - analyzer = new IKAnalyzer(); + analyzer = new IKAnalyzer(true); var postIdxPath = haloProperties.getWorkDir().resolve("indices/posts"); postIndexDir = FSDirectory.open(postIdxPath); } @@ -79,7 +80,7 @@ public class LucenePostSearchService implements PostSearchService, DisposableBea var hits = new ArrayList(topDocs.scoreDocs.length); for (var scoreDoc : topDocs.scoreDocs) { - hits.add(convert(searcher.doc(scoreDoc.doc), highlighter)); + hits.add(convert(searcher.storedFields().document(scoreDoc.doc), highlighter)); } var result = new SearchResult(); @@ -132,12 +133,11 @@ public class LucenePostSearchService implements PostSearchService, DisposableBea } - private Query buildQuery(String keyword) { - keyword = stripToEmpty(keyword).toLowerCase(); + private Query buildQuery(String keyword) throws ParseException { if (log.isDebugEnabled()) { log.debug("Trying to search for keyword: {}", keyword); } - return new FuzzyQuery(new Term("searchable", keyword)); + return new QueryParser("searchable", analyzer).parse(keyword); } private Document convert(PostDoc post) {