3
votes

I want to search string which contains special characters using Azure Search .NET SDK.

I've tried escaping every special character, tried using full query syntax and tried wildcards, nothing worked as expected.

Here is the method:

public virtual async Task<SearchResultDto<T>> Search(string query, SearchOptionsInput searchOptionsInput)
{
   if (!_azureOptions.IsEnabled)
   {
      return null;
   }

   var searchIndexClient = GetSearchIndexClientForGivenType();
   var searchParameters = _objectMapper.Map<SearchParameters>(searchOptionsInput);

   searchParameters.QueryType = QueryType.Full;

   var searchResult = await searchIndexClient.Documents.SearchAsync<T>(query, searchParameters);

   return new SearchResultDto<T>
        {
                Count = searchResult.Count,
                Results = searchResult.Results.Select(r => r.Document).ToList(),
                FacetResults = searchResult.Facets,
        };
}

I have document with Name field set to $@!Q$@!. When I write it in query and escape exclamation mark (like $@\!Q$@\!) I get proper result. But when I delete last ! and write * wildcard in query instead I get no results. Without any sign there's no result aswell.

Is there a way to properly write special characters so search will return results on match eg. when I write $@\!*?

1

1 Answers

2
votes

For now there is no built in method available to escape the special character. But special characters must be escaped to be used as part of the search text. You can escape them by prefixing them with backslash \. Special characters that need to be escaped include the following:

+ - && || ! ( ) { } [ ] ^ " ~ * ? : \ /

For example, to escape a wildcard character, use **\***.

For additional details please check the below link:

https://docs.microsoft.com/en-us/azure/search/query-lucene-syntax#escaping-special-characters

Please upvote the user voice request for implementing this functionality in below link, will help us prioritizing the request.

https://feedback.azure.com/forums/263029-azure-search/suggestions/32114773-provide-method-for-escaping-characters-in-the-sear

Hope it helps.