Escape single quotes in Azure Search query
Asked Answered
L

1

6

I am having a single quote in the data in filter condition.

Example: David O'Neil.

So If we have give this in Search Explorer as,

*&$count=true&$filter=Name eq 'David O'Neil'

How can we escape that single quote in the filter.

Update 1: If we use single quote to escape that one, 0 records are coming.

Update 2: Not able to use backslashes() also.

Loiretcher answered 21/2, 2018 at 22:7 Comment(0)
A
13

Single quotes in OData filters are escaped by doubling:

$filter=Name eq 'David O''Neil'

Since that resulted in zero records for you, I would guess there are other issues with your data (perhaps the apostrophe in O'Neil is not the ASCII character 39 but something more ornate?)

Andrien answered 22/2, 2018 at 21:32 Comment(2)
Doubling the single quotes does not seem to work for me. I get [AzureSearchServiceRESTCallException: {"error":{"code":"","message":"Invalid expression: Syntax error: character '\\' is not valid... It tries to escape with two back slashes in the expression. don't becomes /.*don\\'\\'t.*/Oversubtlety
@VivekAyer That expression looks like a Lucene regex query, not a filter. Only the filter parameter uses the OData syntax, and the above answer is not applicable in other contexts. If you're still having trouble, please post a separate question.Andrien

© 2022 - 2024 — McMap. All rights reserved.