Skip to content

Commit

Permalink
Support blank keyword (#38)
Browse files Browse the repository at this point in the history
Fixes #37 

```release-note
None
```
  • Loading branch information
JohnNiang authored Jul 4, 2024
1 parent efe203d commit 2d658d9
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions src/main/java/run/halo/search/widget/SearchView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,19 +29,29 @@ RouterFunction<ServerResponse> searchView() {
}

private Mono<ServerResponse> 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<SearchResult> 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
));
Expand Down

0 comments on commit 2d658d9

Please sign in to comment.