Skip to content

Commit

Permalink
Changes how query is generated
Browse files Browse the repository at this point in the history
if no search term, it failed previously because the query would start with " AND "
  • Loading branch information
skttl committed Apr 18, 2024
1 parent 987e0c8 commit 3380a4a
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/Our.Umbraco.FullTextSearch/Services/SearchService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,10 @@ private List<SearchProperty> SetSearchProperties()
private ISearchResults GetResults()
{
var query = new StringBuilder();
var queryParts = new List<string>();

if (_search.SearchTerm.IsNullOrWhiteSpace() == false)
{
query.Append("(");

switch (_search.SearchType)
{
case SearchType.MultiRelevance:
Expand Down Expand Up @@ -125,55 +124,55 @@ private ISearchResults GetResults()
query.Append(QueryAllPropertiesAnd(_search.SearchTermSplit, 1.0));
break;
}
query.Append(")");
queryParts.Add(query.ToString());
}

if (_search.RootNodeIds.Any())
{
var rootNodeGroup = string.Join(" OR ", _search.RootNodeIds.Select(x =>
$"{_options.FullTextPathField}:{x}"));
query.Append($" AND ({rootNodeGroup})");
queryParts.Add(rootNodeGroup);
}

var allowedContentTypes = _search.AllowedContentTypes.Where(t => !string.IsNullOrWhiteSpace(t)).ToList();
if (allowedContentTypes.Any())
{
var contentTypeGroup = string.Join(" OR ", allowedContentTypes.Select(x =>
$"__NodeTypeAlias:{x}"));
query.Append($" AND ({contentTypeGroup})");
queryParts.Add(contentTypeGroup);
}


if (_search.PublishedOnly)
{
var publishedPropertySuffix = string.IsNullOrEmpty(_search.Culture) ? "" : $"_{_search.Culture.ToLower()}";
var publishedQuery = $"((__VariesByCulture:y AND __Published{publishedPropertySuffix}:y) OR (__VariesByCulture:n AND __Published:y))";
query.Append($" AND {publishedQuery}");
queryParts.Add(publishedQuery);
}

if (_search.ContentOnly)
{
query.Append($" AND __IndexType:content");
queryParts.Add("__IndexType:content");
}

var disallowedContentTypes = _options.DisallowedContentTypeAliases;
if (disallowedContentTypes.Any()) query.Append($" AND -({string.Join(" ", disallowedContentTypes.Select(x => $"__NodeTypeAlias:{x}"))})");
if (disallowedContentTypes.Any()) queryParts.Add($"-({string.Join(" ", disallowedContentTypes.Select(x => $"__NodeTypeAlias:{x}"))})");

var disallowedPropertyAliases = _options.DisallowedPropertyAliases;
if (disallowedPropertyAliases.Any())
{
var disallowedPropertyAliasGroup = string.Join(" OR ", disallowedPropertyAliases.Select(x => $"{x}_{_search.Culture}:1 OR {x}:1"));
query.Append($" AND -({disallowedPropertyAliasGroup})");
queryParts.Add($"-({disallowedPropertyAliasGroup})");
}

if (_search.RequireTemplate)
{
query.Append($" AND -(templateID:0)");
queryParts.Add($"-(templateID:0)");
}

if (!string.IsNullOrWhiteSpace(_search.CustomQuery))
{
query.Append($" AND ({_search.CustomQuery})");
queryParts.Add($"({_search.CustomQuery})");
}

ISearcher searcher = null;
Expand All @@ -186,6 +185,9 @@ private ISearchResults GetResults()
}
}

query.Clear();
query.Append(string.Join(" AND ", queryParts.Select(x => $"({x})")));

if (searcher != null)
{
_logger.LogDebug("Trying to search for {query}", query.ToString());
Expand Down

0 comments on commit 3380a4a

Please sign in to comment.