All Products
Search
Document Center

OpenSearch:filter clause

Last Updated:Feb 28, 2024

Description

You can specify conditions in a filter clause to query the documents that you want to obtain. After the system obtains the documents that you query by using a query clause, the system filters the documents based on the conditions that you specified in the filter clause and returns the documents that match the specified conditions. A filter clause is optional.

Syntax

Specify one condition

filter=Left operand Relational operator Right operand

Left operand: an attribute field or a constant. The constant can be a numeric value or a string.

Relational operator: a relational operator. The following operators are supported: > < = <= >= !=

Right operand: an attribute field or a constant. The constant can be a numeric value or a string.

Examples:

filter=price > 100: You can use this clause to query the documents in which values in the price field are greater than 100.
filter=ids=1: The ids field is a multi-value field. You can use this clause to query the documents in which values in the ids field contain 1.
filter=province!= "Zhejiang": You can use this clause to query the documents in which values in the province field are not Zhejiang.

Specify multiple conditions

filter=Condition Operator Condition

Condition: a relational expression, such as price > 100.

Operator: the AND or OR operator. If you use the AND operator, the system returns the documents that match both conditions. If you use the OR operator, the system returns the documents that match one condition. You can also enclose conditions that you want to assign the highest priority with parentheses ().

Examples:

filter=price > 100 AND categoryId=10: You can use this clause to query the documents in which values in the categoryId field are 10 and values in the price field are greater than 100.
filter=categoryId = 100 OR categoryId=10: You can use this clause to query the documents in which values in the categoryId field are 10 or values in the price field are greater than 100.
filter=(categoryId = 100 OR categoryId=10) AND price > 100: You can use this clause to query the documents in which values in the categoryId field are 100 or 10, and values in the price field are greater than 100.

Use arithmetic expressions to specify conditions

filter=Left operand Arithmetic operator Right operand Relational operator Condition value

Left operand: an attribute field or a constant. The constant can be a numeric value or a string.

Arithmetic operator: an arithmetic operator. The following arithmetic operators are supported: the addition operator (+), the subtraction operator (-), the multiplication operator (*), and the division operator (/).

Right operand: an attribute field or a constant. The constant can be a numeric value or a string.

Condition value: an attribute field or a constant.

Examples:

filter=price*0.5 > 100: You can use this clause to query each document in which the product of the value in the price field multiplied by 0.5 is greater than 100.
filter=price-cost > 100: You can use this clause to query each document in which the difference between the value in the price field and the value in the cost field is greater than 100.
filter=(price*0.5 > 100) AND categoryId=10: You can use this clause to query each document in which the product of the value in the price field multiplied by 0.5 is greater than 100 and the value in the categoryId field is 10.

Use functions to specify conditions

filter=Function Relational operator Right operand

You can use built-in functions to specify conditions, such as in or notin. For information about built-in functions, see Built-in functions. If the function that you use returns values of the BOOLEAN type, you do not need to specify a relational operator in the filter clause. You can also specify a function as the right operand of a relational operator.

Example:

filter=in(id,"1|2|3"): You can use this clause to query the documents in which values in the id field are 1, 2, or 3.

Usage notes

  • The fields that you specify in a filter clause must be attribute fields that you specified in the schema.json file.

  • The system cannot check whether a value of the FLOAT or DOUBLE type is equal to another value due to precision issues. If a field of the FLOAT or DOUBLE type is required in a filter clause, you can use an expression that includes a greater-than operator (>) and a smaller-than operator (<) to define the relationship.

  • You must enclose values of the STRING type with double quotation marks ('') in a filter clause. You can specify a relational operator in an expression that contains a string to define the relationship between the string and another value. You cannot use an arithmetic operator to define the relationship between the string and another value.

  • You can use the equality operator (=) or inequality operator (!=) but cannot use the greater-than operator (>) or smaller-than operator (<) in an expression that contains fields of the STRING type.

  • If you use the equality operator (=) or inequality operator (!=) to define the relationship between a multi-value field and a condition value, the system returns the documents in which values in the multi-value field include the condition value.