布尔(boolean)

概览

谓词表达式可以输出布尔值。Milvus 通过使用谓词进行标量过滤。当评估谓词表达式时,返回的结果为 true 或 false。请查看 Python SDK API 参考文档 以获取使用谓词表达式的说明。

EBNF (opens in a new tab) 语法规则描述了布尔表达式的方法:

Expr = LogicalExpr | NIL
LogicalExpr = LogicalExpr BinaryLogicalOp LogicalExpr 
              | UnaryLogicalOp LogicalExpr
              | "(" LogicalExpr ")"
              | SingleExpr;
BinaryLogicalOp = "&&" | "and" | "||" | "or";
UnaryLogicalOp = "not";
SingleExpr = TermExpr | CompareExpr;
TermExpr = IDENTIFIER "in" ConstantArray;
Constant = INTEGER | FLOAT
ConstantExpr = Constant
               | ConstantExpr BinaryArithOp ConstantExpr
               | UnaryArithOp ConstantExpr;
 
ConstantArray = "[" ConstantExpr { "," ConstantExpr } "]";
UnaryArithOp = "+" | "-"
BinaryArithOp = "+" | "-" | "*" | "/" | "" | "**";
CompareExpr = IDENTIFIER CmpOp IDENTIFIER
              | IDENTIFIER CmpOp ConstantExpr
              | ConstantExpr CmpOp IDENTIFIER
              | ConstantExpr CmpOpRestricted IDENTIFIER CmpOpRestricted ConstantExpr;
CmpOpRestricted = "<" | "<=";
CmpOp = ">" | ">=" | "<" | "<=" | "=="| "!=";
MatchOp = "like" | "LIKE";

以下表格列出了上述布尔表达式规则中提到的每个符号的描述。

标记描述
=定义。
,连接。
;结束符。
{...}重复。
(...)分组。
NIL空。表达式可以是空字符串。
INTEGER整数,例如 1、2、3。
FLOAT浮点数,例如 1.0、2.0。
CONST整数或浮点数。
IDENTIFIER标识符。在 Milvus 中,标识符代表字段名。
LogicalOpLogicalOp 是一种逻辑操作符,支持在一个比较中结合多个关系运算符。LogicalOp 的返回值只能是 TRUE (1) 或 FALSE (0)。有两种类型的 LogicalOp,包括 BinaryLogicalOps 和 UnaryLogicalOps。
UnaryLogicalOpUnaryLogicalOp 指一元逻辑运算符“not”。
BinaryLogicalOp二元逻辑运算符在两个操作数上执行操作。在具有两个或多个操作数的复杂表达式中,计算顺序取决于优先级规则。
ArithmeticOpArithmeticOp,即算术运算符,在操作数上执行数学运算,例如加法和减法。
UnaryArithOpUnaryArithOp 是一种算术运算符,以单个操作数为操作数执行操作。负的 UnaryArithOp 将一个正的表达式变成负的表达式,反之亦然。
BinaryArithOpBinaryArithOp,即二元运算符,对两个操作数进行操作。在具有两个或多个操作数的复杂表达式中,计算顺序取决于优先级规则。
CmpOpCmpOp 是一种关系运算符,对两个操作数执行操作。
CmpOpRestrictedCmpOpRestricted 限制为“小于”和“等于”。
ConstantExprConstantExpr 可以是常量或两个 ConstExprs 上的 BinaryArithOp 或单个 ConstantExpr 上的 UnaryArithOp。它被递归地定义。
ConstantArrayConstantArray 由方括号包装,可以在方括号中重复 ConstantExpr。

ConstArray 必须包含至少一个 ConstantExpr。

| TermExpr | TermExpr 用于检查 IDENTIFIER 的值是否出现在 ConstantArray 中。TermExpr 由 "in" 表示。| | CompareExpr | CompareExpr,即比较表达式,可以是两个 IDENTIFIER 的关系运算,或一个 IDENTIFIER 和一个 ConstantExpr 的关系运算,或两个 ConstantExprs 和一个 IDENTIFIER 的三元运算。 | | SingleExpr | SingleExpr,即单个表达式,可以是 TermExpr 或 CompareExpr。| | LogicalExpr | LogicalExpr 可以是逻辑操作 LogicalExprs 上的 BinaryLogicalOp,或一元逻辑操作 UnaryLogicalOp 上的单个 LogicalExpr,或圆括号内的 LogicalExpr,或 SingleExpr。LogicalExpr 递归定义。| | Expr | 表示表达式的缩写,可以是 LogicalExpr 或 NIL。 | | MatchOp | MatchOp,即匹配操作符,比较字符串和字符串常量或字符串前缀常量。|

操作符

逻辑运算符:

逻辑运算符用于在两个表达式之间进行比较。

SymbolOperationExampleDescription
'and' &&andexpr1 && expr2True if both expr1 and expr2 are true.
'or'or

二元算术运算符:

二元算术运算符包含两个操作数,可以执行基本算术运算并返回相应的结果。

SymbolOperationExampleDescription
+Additiona + bAdd the two operands.
-Subtractiona - bSubtract the second operand from the first operand.
*Multiplicationa * bMultiply the two operands.
/Divisiona / bDivide the first operand by the second operand.
**Powera ** bRaise the first operand to the power of the second operand.
%Moduloa % bDivide the first operand by the second operand and yield the remainder portion.

Relational operators:

Relational operators use symbols to check for equality, inequality, or relative order between two expressions.

SymbolOperationExampleDescription
<Less thana < bTrue if a is less than b.
>Greater thana > bTrue if a is greater than b.
==Equala == bTrue if a is equal to b.
!=Not equala != bTrue if a is not equal to b.
<=Less than or equala <= bTrue if a is less than or equal to b.
>=Greater than or equala >= bTrue if a is greater than or equal to b.

运算符优先级和结合性

下表列出了运算符的优先级和结合性。运算符从上到下按优先级降序排列。

PrecedenceOperatorDescriptionAssociativity
1+ -UnaryArithOpLeft-to-right
2notUnaryLogicOpRight-to-left
3**BinaryArithOpLeft-to-right
4* / %BinaryArithOpLeft-to-right
5+ -BinaryArithOpLeft-to-right
6< <= > >=CmpOpLeft-to-right
7== !=CmpOpLeft-to-right
8like LIKEMatchOpLeft-to-right
9&& andBinaryLogicOpLeft-to-right
10or

通常,表达式从左到右进行求值。复杂表达式一次计算一个。表达式求值的顺序由所使用的运算符的优先级确定。

如果一个表达式包含两个或多个具有相同优先级的运算符,则先计算左边的运算符。

For example, 10 / 2 * 5 will be evaluated as (10 / 2) and the result multiplied by 5.

当需要先处理低优先级操作时,应将其括在括号内。

For example, 30 / 2 + 8. This is normally evaluated as 30 divided by 2 then 8 added to the result. If you want to divide by 2 + 8, it should be written as 30 / (2 + 8).

括号可以嵌套在表达式中。最内层的括号表达式先求值。

使用

Milvus中所有可用布尔表达式用法的示例如下(int64表示包含INT64类型数据的标量字段,float表示包含浮点类型数据的标量字段,VARCHAR表示包含VARCHAR类型数据的标量字段):

  • CmpOp
"int64 > 0"
 
"0 < int64 < 400"
 
"500 <= int64 < 1000"
 
VARCHAR > "str1"
 
  • BinaryLogicalOp和括号
"(int64 > 0 && int64 < 400) or (int64 > 500 && int64 < 1000)"
 
  • TermExpr和UnaryLogicOp

Milvus only supports deleting entities with clearly specified primary keys, which can be achieved merely with the term expression in.

"int64 not in [1, 2, 3]"
 
VARCHAR not in ["str1", "str2"]
 
  • TermExpr、BinaryLogicalOp 和 CmpOp(在不同的字段上)
"int64 in [1, 2, 3] and float != 2"
 
  • BinaryLogicalOp 和 CmpOp
"int64 == 0 || int64 == 1 || int64 == 2"
 
  • CmpOp 和 UnaryArithOp 或 BinaryArithOp
"200+300 < int64 <= 500+500"
 
  • MatchOp(前缀匹配)
VARCHAR like "prefix"
 

下一步是什么

现在,您已经知道在Milvus中如何使用位集,您可能还想: