0

We are using Azure Search to find items by path. We apply the following search code:

var filterPredicate = PredicateBuilder.True<SearchResultItem>();
filterPredicate.And(x => x.Path.StartsWith(rootItem.Paths.Path));
var query = context.GetQueryable<SearchResultItem>().Filter(filterPredicate);
​SearchResults<SearchResultItem> results = query.GetResults();

However, when we do this with a path that includes spaces, the query gets generated as the below query (taken from the logs):

10216 15:53:13 INFO  AzureSearch Query [sitecore_master_index]: &search=(fullpath_1:(/\/sitecore\/content\/Sites\/Site.*/ /1\/Services\/ServiceA.*/))&queryType=full&$skip=0&$top=3&$count=true

Notice the extra .* included after the Site of Site 1. This means that when the query gets executed, it returns all results whose path starts with /sitecore/content/Sites/Site instead of only the results below the complete path.

Does anyone know how to resolve this issue?

Environment:

  • Sitecore 9.0.2
  • Azure PaaS

1 Answer 1

2

Rather than use the string based full path, use the IEnumerable<ID> Paths property:

var filterPredicate = PredicateBuilder.True<SearchResultItem>();
filterPredicate.And(x => x.Paths.Contains(rootItem.ID));
var query = context.GetQueryable<SearchResultItem>().Filter(filterPredicate);
​SearchResults<SearchResultItem> results = query.GetResults();

That should give you more accurate results than searching a string that is tokenized.

1
  • While this does not fix the incorrect query generation, it does fix my issue. Thanks, Richard! Commented Sep 11, 2019 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.