From 3a81d93e689f69833847e4fe656a463c86c6767d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Kottal?= Date: Fri, 15 Mar 2024 12:25:09 +0100 Subject: [PATCH] Allows searching using an empty string as the search term --- CHANGELOG.md | 3 + .../Services/SearchService.cs | 75 ++++++++++--------- 2 files changed, 42 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e70f91b..fa6c5b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## 4.0.2 +- Allows searching using an empty string as the search term + ## 4.0.1 - Prevents HTTPPageRenderer trying to fetch page if its URL is invalid (eg. "#") diff --git a/src/Our.Umbraco.FullTextSearch/Services/SearchService.cs b/src/Our.Umbraco.FullTextSearch/Services/SearchService.cs index d542557..5202fd6 100644 --- a/src/Our.Umbraco.FullTextSearch/Services/SearchService.cs +++ b/src/Our.Umbraco.FullTextSearch/Services/SearchService.cs @@ -85,45 +85,48 @@ private ISearchResults GetResults() { var query = new StringBuilder(); - query.Append("("); - - switch (_search.SearchType) + if (_search.SearchTerm.IsNullOrWhiteSpace() == false) { - case SearchType.MultiRelevance: - case SearchType.MultiAnd: + query.Append("("); - // We formulate the query differently depending on the input. - if (_search.SearchTerm.Contains('"')) - { - // If the user has entered double quotes we don't bother - // searching for the full string - query.Append(QueryAllPropertiesOr(_search.SearchTermSplit, 1)); - } - else if (!_search.SearchTerm.Contains('"') && !_search.SearchTerm.Contains(' ')) - { - // if there's no spaces or quotes we don't need to get the quoted term and boost it - query.Append(QueryAllPropertiesOr(_search.SearchTermSplit, 1)); - } - else - { - // otherwise we search first for the entire query in quotes, - // then for each term in the query OR'd together. - query.Append($"({QueryAllPropertiesOr(_search.SearchTermQuoted, 2)} OR {QueryAllPropertiesOr(_search.SearchTermSplit, 1)})"); - } - - break; - - case SearchType.SimpleOr: - - query.Append(QueryAllProperties(_search.SearchTermSplit, 1.0, "OR", true)); - break; - - case SearchType.AsEntered: - - query.Append(QueryAllPropertiesAnd(_search.SearchTermSplit, 1.0)); - break; + switch (_search.SearchType) + { + case SearchType.MultiRelevance: + case SearchType.MultiAnd: + + // We formulate the query differently depending on the input. + if (_search.SearchTerm.Contains('"')) + { + // If the user has entered double quotes we don't bother + // searching for the full string + query.Append(QueryAllPropertiesOr(_search.SearchTermSplit, 1)); + } + else if (!_search.SearchTerm.Contains('"') && !_search.SearchTerm.Contains(' ')) + { + // if there's no spaces or quotes we don't need to get the quoted term and boost it + query.Append(QueryAllPropertiesOr(_search.SearchTermSplit, 1)); + } + else + { + // otherwise we search first for the entire query in quotes, + // then for each term in the query OR'd together. + query.Append($"({QueryAllPropertiesOr(_search.SearchTermQuoted, 2)} OR {QueryAllPropertiesOr(_search.SearchTermSplit, 1)})"); + } + + break; + + case SearchType.SimpleOr: + + query.Append(QueryAllProperties(_search.SearchTermSplit, 1.0, "OR", true)); + break; + + case SearchType.AsEntered: + + query.Append(QueryAllPropertiesAnd(_search.SearchTermSplit, 1.0)); + break; + } + query.Append(")"); } - query.Append(")"); if (_search.RootNodeIds.Any()) {