5

I am trying to search a Sitecore Azure Index using whole phrase searching, rather than single words.

For example: "applying for a job"

is matching all individual of the words in the phrase, rather than the whole phrase. What do I have to do to have a whole phrase match only?

private string CreateQueryText(string searchQuery)
{
    var replace = HttpUtility.UrlDecode(searchQuery);
    var query = $@"""{replace}""";
    return query;
}

private Expression<Func<ContentPageResultItem, bool>> CreateSearchPredicate(SearchQuery searchQuery)
{
    var query = CreateQueryText(searchQuery.Query);
    var basePredicate = PredicateBuilder.False<ContentPageResultItem>(); // Anchor False for OR Queries, True for And Queries
    basePredicate = basePredicate.Or(item => item.Title.Contains(query));
    basePredicate = basePredicate.Or(item => item.Summary.Contains(query));
    return basePredicate;
}

Thanks.

1

I'm not sure what version of Sitecore you are using but have just been fighting this same issue in Sitecore 8.2 update 7 and in the end it seems that the Like() function achieved what I wanted:

var phrasedText = $@"""{trimmedLookupText}""";
var predicate = PredicateBuilder.True<CustomSearchResultItem>();
predicate = predicate.And(p => p.Name.Like(phrasedText)); 
var results = context.GetQueryable<CustomSearchResultItem>().Where(predicate).ToList()

Note I do some trimming of the phrasedText text string and remove some special chars before passing it to the Like function.

The above results in a query like so being sent to Azure Search: 18464 16:47:09 INFO AzureSearch Query [test-azure-index]: &search=name:\"joe blogs\"~&queryType=full&$top=2147483647&$count=true

My understanding is that this is creating a 'Phase Search' in Azure so matches the whole text instead of just each word which would usually be the case with Azure Search: https://docs.microsoft.com/en-us/rest/api/searchservice/simple-query-syntax-in-azure-search

It is also worth reading this document regarding the limitations of Azure Search in Sitecore 8.2: https://doc.sitecore.com/developers/82/sitecore-experience-platform/en/support-reference-for-azure-search.html

I'm not sure if this is the same in 9.x but interestingly the Same article for 9.0 mentioned Fuzzy (Like()) searches being interpreted by Azure Search as Phrase queries (which seems to be what is happening here for 8.2 but isn't mentioned in the 8.2 article): https://doc.sitecore.com/developers/90/platform-administration-and-architecture/en/sitecore-azure-search-overview.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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