The filter clause narrows down query results by applying conditions based on your business requirements. It further filters documents returned by a query clause to retrieve only the results you need.
Syntax
Format of a filter condition: field=value.
-
Filter conditions support conditional operators such as >, <, =, <=, >=, !=, in, and notin, and arithmetic operators such as +, -, *, /, &, ^, and |.
-
Multiple filter conditions can be combined with the logical operators AND, OR, and (). These operators must be uppercase.
Usage notes
-
The filter clause is optional.
-
Fields used in a filter clause must be configured as attribute fields in the application schema.
-
Due to precision issues, equality checks on FLOAT or DOUBLE fields are unreliable. Use a range expression with > and < operators instead.
-
LITERAL values must be enclosed in double quotation marks ('') in the filter clause. LITERAL fields support relational operations but not arithmetic operations. Omitting the quotation marks returns error code 6135: The value type of the constant expression is invalid.
-
Functionality functions such as distance can be used in the filter clause for sorting.
-
LITERAL fields support only the equality (=) and inequality (!=) operators. The > and < operators are not supported because LITERAL fields are not segmented and require exact matches.
-
The in and notin functions check whether field values exist in a specific list. Only INT, LITERAL, FLOAT, and DOUBLE fields are supported. ARRAY and TEXT fields, and the fuzzy search analyzer are not supported. For more information, see in and notin.
-
Attribute fields cannot be created from text types such as TEXT and SHORT_TEXT. Only numeric or non-text types are supported, including INT, INT_ARRAY, FLOAT, FLOAT_ARRAY, DOUBLE, DOUBLE_ARRAY, LITERAL, and LITERAL_ARRAY.
Supported functionality functions
-
distance: returns the spherical distance between two points, typically used for location-based service (LBS) calculations.
Example:
Search for KFCs within 10 kilometers of a user at coordinates (120.34256, 30.56982). The lon and lat fields representing KFC locations must be configured as attribute fields.
query=default:'KFC'&&filter=distance(lon,lat,"120.34256","30.56982")<10
-
in_polygon: checks whether a point falls within a polygon geographic area, typically used for delivery radius checks.
Example:
Check whether a user at coordinates (120.307234, 39.294245) is within the delivery radius defined by the coordinates field. The following query returns merchants whose delivery area covers the user:
query=default:'Foods'&&filter=in_polygon(coordinates, 120.307234, 39.294245)>0
-
in_query_polygon: checks whether a point specified in a document is within a specific polygon geographic area.
Example:
Search for KFCs in the Yintai business district (xA,yA,xB,Yb,xC,Yc;xD,yD,xE,yE,xF,yF,xG,yG). The point field is used to store the geographic location of KFCs.
query=default:'KFC'&&filter=in_query_polygon("polygons",point)>0&&kvpairs=polygons:xA\,yA\,xB\,Yb\,xC\,Yc;xD\,yD\,xE\,yE\,xF\,yF\,xG\,yG
-
bit_struct: splits each value into multiple parts for a field of the INT_ARRAY type and performs a specific operation on the parts.
Example:
Search for stores open during a specific time range. The open_time field (INT_ARRAY type) stores business hours as 64-bit integers: the first 32 bits represent the opening time, and the last 32 bits represent the closing time. To find stores open from 14:00 to 15:30, convert each time to minutes elapsed since 00:00 — 14:00 becomes 840 and 15:30 becomes 930.
filter=bit_struct(open_time, "0-31,32-63","overlap,$1,$2,840,930")!=-1
-
fieldlen: calculates the value length of a field of the LITERAL type.
Example:
Search for documents in which the value of the usr_name field is not empty.
query=default:'Keyword'&&filter=fieldlen(usr_name)>0
-
in or notin: checks whether field values are in a specific list.
Example:
Retrieve documents that contain "iPhone" and the type field whose value is 1, 2, or 3. The type field is of the INT type.
query=default:'iphone'&&filter=in(type, "1|2|3")
Retrieve documents that contain "iPhone" and the type field whose value is not 1, 2, or 3. The type field is of the INT32 type.
query=default:'iphone'&&filter=notin(type, "1|2|3")
-
multi_attr: returns the value of a specific array field at a specific position.
Example:
A commodity may have multiple prices (market, discount, and sales) stored in the prices field. The following query searches for mobile phones with a sales price below 1,000:
query=default:'Mobile phone'&&filter=multi_attr(price,2)<1000
Examples
-
The category field of the INT32 type in the application has values such as 1 (news) and 2 (bbs). You can use one of the following query clauses to search for documents that contain "Zhejiang University" and in which the category field is set to 2:
query=default:'Zhejiang University' AND category_search:'2' // The index field category_search is created for the category field. // Or query=default:'Zhejiang University'&&filter=category=2 -
In an application that is used to search for novels, the tags field of the STRING_ARRAY type specifies the following tags based on the styles of novels: palace, suspense and horror, and romance. You can use the following query clause to search for documents that contain "Empresses in the palace" and in which the tags field has a value of "palace":
query=default:'Empresses in the palace'&&filter=tags="palace" -
The hit and sale fields of the INT32 type and the create_time field of the INT64 type exist in an e-commerce application. You can use the following query clause to search for documents that meet the following conditions: 1. The documents contain "dress". 2. The product of the value of the rate field and the sum of the values of the hit and sale fields exceeds 10000. 3. The value of the create_time field is less than 1402345600.
query=default:'dress'&&filter=(hit+sale)*rate>10000 AND create_time<1402345600