All Products
Search
Document Center

OpenSearch:filter clause

Last Updated:Aug 27, 2024

Overview

The filter clause allows you to specify filter conditions based on your business requirements to search for documents. You can use a filter clause to filter documents that are queried by using a query clause to retrieve required documents. The filter clause is optional.

Syntax

Specify one condition

filter=Left operand Relational operator Right operand

Left operand: an attribute field or a constant of a numeric value or a string.

Relational operator: a relational operator. Supported operators: > < = <= >= !=

Right operand: an attribute field or a constant of a numeric value or a string.

Example:

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 ().

Example:

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 100 or 10.
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 of a numeric value or a string.

Arithmetic operator: an arithmetic operator. Supported operators: + - * /

Right operand: an attribute field or a constant of a numeric value or a string.

Condition value: an attribute field or a constant.

Example:

filter=price*0.5 > 100: You can use this clause to query the documents 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 the documents 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 the documents 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 more 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 (!=) in an expression that contains fields of the STRING type, but cannot use the greater than operator (>) or smaller than operator (<) in such an expression.

  • 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.