Azure 认知搜索中的 OData 集合运算符 - any
和 all
OData collection operators in Azure Cognitive Search - any
and all
在编写 OData 筛选器表达式以用于 Azure 认知搜索时,对集合字段进行筛选通常很有用。When writing an OData filter expression to use with Azure Cognitive Search, it is often useful to filter on collection fields. 可以使用 any
和 all
运算符实现这一点。You can achieve this using the any
and all
operators.
语法Syntax
以下 EBNF(扩展巴科斯-瑙尔范式)定义了使用 any
或 all
的 OData 表达式的语法。The following EBNF (Extended Backus-Naur Form) defines the grammar of an OData expression that uses any
or all
.
collection_filter_expression ::=
field_path'/all(' lambda_expression ')'
| field_path'/any(' lambda_expression ')'
| field_path'/any()'
lambda_expression ::= identifier ':' boolean_expression
下面还提供了交互式语法图:An interactive syntax diagram is also available:
备注
请参阅适用于 Azure 认知搜索的 OData 表达式语法参考以获取完整的 EBNF。See OData expression syntax reference for Azure Cognitive Search for the complete EBNF.
有三种形式的表达式可以筛选集合。There are three forms of expression that filter collections.
- 前两个表达式遍历集合字段,将以 lambda 表达式形式给定的谓词应用于集合的每个元素。The first two iterate over a collection field, applying a predicate given in the form of a lambda expression to each element of the collection.
- 如果集合的每个元素的谓词都为 true,则使用
all
的表达式将返回true
。An expression usingall
returnstrue
if the predicate is true for every element of the collection. - 如果集合的至少一个元素的谓词为 true,则使用
any
的表达式将返回true
。An expression usingany
returnstrue
if the predicate is true for at least one element of the collection.
- 如果集合的每个元素的谓词都为 true,则使用
- 第三种形式的集合筛选器使用不带 lambda 表达式的
any
来测试集合字段是否为空。The third form of collection filter usesany
without a lambda expression to test whether a collection field is empty. 如果集合有任何元素,则返回true
。If the collection has any elements, it returnstrue
. 如果集合为空,则返回false
。If the collection is empty, it returnsfalse
.
集合筛选器中的 lambda 表达式类似于编程语言中的循环体。A lambda expression in a collection filter is like the body of a loop in a programming language. 它定义了一个变量,称为范围变量,它在迭代期间保存集合的当前元素。It defines a variable, called the range variable, that holds the current element of the collection during iteration. 它还定义了另一个布尔表达式,该表达式是应用于集合的每个元素的范围变量的筛选条件。It also defines another boolean expression that is the filter criteria to apply to the range variable for each element of the collection.
示例Examples
匹配 tags
字段恰好包含字符串“wifi”的文档:Match documents whose tags
field contains exactly the string "wifi":
tags/any(t: t eq 'wifi')
匹配其中 ratings
字段的每个元素介于 3 和 5 之间(包括 3 和 5)的文档:Match documents where every element of the ratings
field falls between 3 and 5, inclusive:
ratings/all(r: r ge 3 and r le 5)
匹配其中 locations
字段中任何地理坐标在给定多边形内的文档:Match documents where any of the geo coordinates in the locations
field is within the given polygon:
locations/any(loc: geo.intersects(loc, geography'POLYGON((-122.031577 47.578581, -122.031577 47.678581, -122.131577 47.678581, -122.031577 47.578581))'))
匹配其中 rooms
字段为空的文档:Match documents where the rooms
field is empty:
not rooms/any()
匹配对于所有房间 rooms/amenities
字段包含“tv”且 rooms/baseRate
小于 100 的文档:Match documents where for all rooms, the rooms/amenities
field contains "tv" and rooms/baseRate
is less than 100:
rooms/all(room: room/amenities/any(a: a eq 'tv') and room/baseRate lt 100.0)
限制Limitations
并非每个筛选器表达式功能都可在 Lambda 表达式主体中使用。Not every feature of filter expressions is available inside the body of a lambda expression. 根据要筛选的集合字段的数据类型,限制会有所不同。The limitations differ depending on the data type of the collection field that you want to filter. 下表总结了这些限制。The following table summarizes the limitations.
数据类型Data type | 带 any 的 lambda 表达式中允许的功能Features allowed in lambda expressions with any |
带 all 的 lambda 表达式中允许的功能Features allowed in lambda expressions with all |
---|---|---|
Collection(Edm.ComplexType) |
除 search.ismatch 和 search.ismatchscoring 外的所有内容Everything except search.ismatch and search.ismatchscoring |
相同Same |
Collection(Edm.String) |
使用 eq 或 search.in 进行比较Comparisons with eq or search.in 使用 or 组合子表达式Combining sub-expressions with or |
使用 ne 或 not search.in() 进行比较Comparisons with ne or not search.in() 使用 and 组合子表达式Combining sub-expressions with and |
Collection(Edm.Boolean) |
使用 eq 或 ne 进行比较Comparisons with eq or ne |
相同Same |
Collection(Edm.GeographyPoint) |
将 geo.distance 与 lt 或 le 配合使用Using geo.distance with lt or le geo.intersects 使用 or 组合子表达式Combining sub-expressions with or |
将 geo.distance 与 gt 或 ge 配合使用Using geo.distance with gt or ge not geo.intersects(...) 使用 and 组合子表达式Combining sub-expressions with and |
Collection(Edm.DateTimeOffset) 、Collection(Edm.Double) 、Collection(Edm.Int32) 、Collection(Edm.Int64) Collection(Edm.DateTimeOffset) , Collection(Edm.Double) , Collection(Edm.Int32) , Collection(Edm.Int64) |
使用 eq 、ne 、lt 、gt 、le 或 ge 进行比较Comparisons using eq , ne , lt , gt , le , or ge 使用 or 将比较与其他子表达式组合Combining comparisons with other sub-expressions using or 使用 and 将除 ne 以外的比较与其他子表达式组合Combining comparisons except ne with other sub-expressions using and 在析取范式 (DNF) 中使用 and 和 or 组合的表达式Expressions using combinations of and and or in Disjunctive Normal Form (DNF) |
使用 eq 、ne 、lt 、gt 、le 或 ge 进行比较Comparisons using eq , ne , lt , gt , le , or ge 使用 and 将比较与其他子表达式组合Combining comparisons with other sub-expressions using and 使用 or 将除 eq 以外的比较与其他子表达式组合Combining comparisons except eq with other sub-expressions using or 在合取范式 (CNF) 中使用 and 和 or 组合的表达式Expressions using combinations of and and or in Conjunctive Normal Form (CNF) |
若要更详细地了解这些限制和相关示例,请参阅排查 Azure 认知搜索中的集合筛选器问题。For more details on these limitations as well as examples, see Troubleshooting collection filters in Azure Cognitive Search. 若要更深入地了解为何存在这些限制,请参阅了解 Azure 认知搜索中的集合筛选器。For more in-depth information on why these limitations exist, see Understanding collection filters in Azure Cognitive Search.