Skip to content

Commit

Permalink
Allows searching using an empty string as the search term
Browse files Browse the repository at this point in the history
  • Loading branch information
skttl committed Mar 15, 2024
1 parent 8d7a248 commit 3a81d93
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 36 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. "#")

Expand Down
75 changes: 39 additions & 36 deletions src/Our.Umbraco.FullTextSearch/Services/SearchService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand Down

0 comments on commit 3a81d93

Please sign in to comment.