From 3696220495e56b833ce043a327452aca5ab77e93 Mon Sep 17 00:00:00 2001 From: JohnNiang Date: Thu, 4 Jul 2024 15:03:23 +0800 Subject: [PATCH] Support blank keyword Signed-off-by: JohnNiang --- .../run/halo/search/widget/SearchView.java | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/main/java/run/halo/search/widget/SearchView.java b/src/main/java/run/halo/search/widget/SearchView.java index 40cc4cd..a028d62 100644 --- a/src/main/java/run/halo/search/widget/SearchView.java +++ b/src/main/java/run/halo/search/widget/SearchView.java @@ -6,8 +6,10 @@ import org.springframework.web.reactive.function.server.*; import reactor.core.publisher.Mono; import run.halo.app.search.SearchOption; +import run.halo.app.search.SearchResult; import run.halo.app.search.SearchService; +import java.util.List; import java.util.Map; @Component @@ -27,19 +29,29 @@ RouterFunction searchView() { } private Mono performSearch(ServerRequest serverRequest) { - var keyword = serverRequest.queryParam("keyword").orElse(""); - var limit = serverRequest.queryParam("limit") + int limit = serverRequest.queryParam("limit") .map(Integer::parseInt) .orElse(10); - var includeTypes = serverRequest.queryParams().get("includeTypes"); - var option = new SearchOption(); - option.setKeyword(keyword); - option.setLimit(limit); - option.setFilterExposed(true); - option.setFilterPublished(true); - option.setFilterRecycled(false); - option.setIncludeTypes(includeTypes); - var result = searchService.search(option); + var keyword = serverRequest.queryParam("keyword").orElse(""); + Mono result; + if (keyword.isBlank()) { + var searchResult = new SearchResult(); + searchResult.setTotal(0L); + searchResult.setLimit(limit); + searchResult.setProcessingTimeMillis(0); + searchResult.setHits(List.of()); + result = Mono.just(searchResult); + } else { + var includeTypes = serverRequest.queryParams().get("includeTypes"); + var option = new SearchOption(); + option.setKeyword(keyword); + option.setLimit(limit); + option.setFilterExposed(true); + option.setFilterPublished(true); + option.setFilterRecycled(false); + option.setIncludeTypes(includeTypes); + result = searchService.search(option); + } return ServerResponse.ok().render("search", Map.of( "searchResult", result ));