Azure认知搜索-如何按包含特殊字符的字段进行筛选 [英] Azure Cognitive Search - How to filter by fields containing special characters

查看:143
本文介绍了Azure认知搜索-如何按包含特殊字符的字段进行筛选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用Azure Cognitive Search .NET SDK,并尝试通过字符串字段$filter,其中该字符串字段可以包含诸如&之类的特殊搜索字符以及单引号.

We are using the Azure Cognitive Search .NET SDK and are trying to $filter by a string field that can contain Search-special characters such as &, as well as single quotes.

当使用带有特殊字符的厨房水槽过滤测试用例时,我们得到的结果为零(我们排除了|,因为它是search.in的分隔符):

We get zero results when filtering against a test case with the kitchen sink of special characters (we excluded | since it's our separator for search.in):

{
  "FirmName": "Crazy Charz Inc. ' + - && ! ( ) { } [ ] ^ \" ~ * ? : \\ /"
  ...
}

当我们使用\转义特殊字符时,如询问此处在此答案中所示,而不是在SDK文档中显示),我们得到的结果为零.

When we escape the special characters with \ as asked about here and recommended here, and the single quote by double-quoting it '' (as revealed in this answer, not in the SDK docs), we get zero results.

(这是在VS中检查变量时的外观;应该正确地对它进行转义.)

(That's how it looks when inspecting the variable in VS; it should be properly escaped.)

我们得到的结果为零.

我们已经确认它特定于特殊字符,因为我们有大量的测试,这些测试具有与其他文档匹配的字段,而这些文档的值中不包含此类字符.

We've confirmed it's specific to the special characters, because we have plenty of tests with the same field matching other docs that contain no such chars in their value.

出于好奇,我们尝试像这样在Search Explorer中运行它:

Out of curiosity, we tried running it in Search Explorer like this:

$filter=search.in(FirmName, 'Crazy Charz Inc. '' \+ \- \&\& \! \( \) \{ \} \[ \] \^ \" \~ \* \? \: \\ \/', '|')

这样做时,我们会收到错误消息:

When we do so, we get the error:

"Invalid expression: Found an unbalanced bracket expression.\r\nParameter name: $filter"

我们已经确认SDK会返回实际的零结果响应,而不是错误(我们在过滤器表达式中添加了实际的不平衡表达式以确认这一点.)

We've confirmed that the SDK returns an actual zero-results response, not an error (we put an actual unbalanced expression in the filter expression to confirm this).

如何使用.NET SDK使用特殊字符的值$filter?这是一个错误,还是我们做错了什么?

How can we $filter on values with special chars using the .NET SDK? Is this a bug, or are we doing something wrong?

注意:我们正在提供选项的选择列表并进行精确匹配;因此,请针对此用例进行过滤,而不是进行搜索.稍后我们将在其他字段上添加搜索.

Note: We are providing a pick list of options and doing an exact match; hence filter, and not search, for this use case. We'll be adding search on other fields later.

我们是否需要简单地对所有字段进行URLEncode? gh ...

Do we need to simply URLEncode all our fields? Ugh...

推荐答案

问题是您使用的编码方案语法与所需语法不同.

The issue is that you're using an encoding scheme for a different syntax than the one you want.

Azure认知搜索中有三种查询语法,每种语法都有自己的编码规则:

There are three query syntaxes in Azure Cognitive Search, each with its own encoding rules:

  1. 简单的查询语法(在search参数中使用;您链接到的文档中描述的编码规则)
  2. 完整的Lucene查询语法(也在search中使用,或多或少是Simple查询语法的超集)
  3. OData语法(用在$filter$select$orderby中;已记录在
  1. Simple query syntax (used in the search parameter; encoding rules described in the docs to which you linked)
  2. Full Lucene query syntax (also used in search, more or less a superset of the Simple query syntax)
  3. OData syntax (used in $filter, $select, and $orderby; documented here).

单引号加倍的规则来自OData.您要应用的其他规则是针对简单查询语法的,而不是针对OData的.

The rule about doubling single quotes comes from OData. The other rules you're applying are for Simple query syntax, not OData.

我编写了一个小型控制台应用程序进行测试,并且能够使用此确切的字符串文字匹配预期的文档:

I wrote a small console app to test this, and I was able to match the expected document using this exact string literal:

@"search.in(hotelName, 'Crazy Charz Inc. '' + - && ! ( ) { } [ ] ^ "" ~ * ? : \ /', '|')"

请注意,因为我使用的是逐字字符串,所以只需要对引号进行转义(OData的单引号,编译器的双引号).

Note that because I'm using a verbatim string, only the quotes need to be escaped (single quote for OData, double quote for the compiler).

这篇关于Azure认知搜索-如何按包含特殊字符的字段进行筛选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆