Search code examples
c#query-stringurlencodeazure-cognitive-search

Literal Ampersands in Azure Search Query String


I'm am trying to apply a filter on a faceted search in Azure Search. Unfortunately, some of my client's search data contain an ampersand character like this:

&facet=client&$filter=client eq 'Dunn & Bradstreet'

Since the above example is part of a query string, the ampersand in Dunn & Bradstreet is an issue.

Escaping the ampersand as follows results in an error:

&facet=client&$filter=client eq 'Dunn %26 Bradstreet'

The error when using the Azure Search Explorer to test search queries is:

{
    "error": {
        "code": "",
        "message": "Invalid expression: There is an unterminated string literal at position 34 in 'client eq 'Dunn '.\r\nParameter name: $filter"
    }
}

Is there some magical workaround for escaping the & symbol in strings that won't break Azure Search?


Solution

  • This appears to be a bug in Search Explorer. It encodes characters in the URL other than ampersand. If you encode the ampersand itself, Search Explorer double-encodes it. I've confirmed that if you encode the ampersand when calling the REST API directly, it works correctly.

    I'll update this answer when the bug has been fixed. Sorry for the inconvenience.

    Update: we fixed the issue in Search Explorer