This topic describes the filter clause for result filtering and its syntax, notes the points that require attention, and provides examples of how to use functional functions.
Clause description
The filter feature allows you to select the documents that you are interested in based on query conditions. The documents found by the query clause are further filtered to return the final results that you need.
Syntax
Format of a filter condition: field=value
Filter conditions support common conditional operators such as >, <, =, <=, >=, !=, and in/notin, as well as arithmetic operators such as +, -, *, /, &, ^, and |.
You can configure multiple filter conditions and connect them by using the logical operators AND and OR and parentheses (). The logical operators must be in uppercase.
Usage notes
The filter clause is optional.
Fields that appear in the filter clause must be configured as attribute fields when you define the application schema.
Due to precision issues, values of the FLOAT and DOUBLE types cannot be compared for exact equality. In such scenarios, use > and < instead.
Values of LITERAL fields must be enclosed in double quotation marks in the filter clause. Otherwise, error 6135 (constant expression type error) is reported. LITERAL fields support all relational operations but do not support arithmetic operations.
Functions listed under the functional functions of the sort feature can be used in the filter clause, such as the distance function.
Filtering on LITERAL fields supports only the = and != operations, which mean equal to and not equal to. Relational operations such as > and < are not supported. (LITERAL fields are not tokenized and require an exact match.)
in/notin determines whether a field value is (or is not) in a specified list. It supports the INT, LITERAL, FLOAT, and DOUBLE types (the ARRAY, TEXT, and fuzzy tokenization types are not supported). For more information, see in and notin.
Tokenized field types, such as TEXT and SHORT_TEXT, cannot be configured as attributes. Only numeric field types and non-tokenized field types can be configured as attributes. For example, the INT, INT_ARRAY, FLOAT, FLOAT_ARRAY, DOUBLE, DOUBLE_ARRAY, LITERAL, and LITERAL_ARRAY field types are supported.
Use functional functions
distance: obtains the spherical distance between two points. This function is typically used for distance calculation in location-based services (LBS).
Example:
Find Grandma Home restaurants within 10 kilometers of the user (120.34256,30.56982). lon and lat are the longitude and latitude of the merchant recorded in the document and must be configured as attribute fields:
query=default:'Grandma Home'&&filter=distance(lon,lat,"120.34256","30.56982")in_polygon: determines whether a point is within a polygon. This function is typically used to determine delivery coverage.
Example:
Determine whether a user is within the delivery coverage of merchants. For example, if the field that stores the delivery coverage of a merchant is coordinates and the user location is (120.307234, 39.294245), filter the merchants whose delivery coverage includes the user:
query=default:'food'&&filter=in_polygon(coordinates, 120.307234, 39.294245)>0in_query_polygon: determines whether the point specified in a document is within the polygon specified by the user.
Example:
Search for Grandma Home restaurants in the Intime business district (xA,yA,xB,Yb,xC,Yc;xD,yD,xE,yE,xF,yF,xG,yG). The merchant locations are stored in the point field.
query=default:'Grandma Home'&&filter=in_query_polygon("polygons",point)>0&&kvpairs=polygons:xA\,yA\,xB\,Yb\,xC\,Yc;xD\,yD\,xE\,yE\,xF\,yF\,xG\,yGbit_struct: divides the values of an INT_ARRAY field into custom groups and allows a specified operation to be performed on the grouped values.
Example:
Which stores are open during a given time period? Assume that the user documents contain a field named open_time of the int64_array type. Each value represents an opening period, in which the upper 32 bits of the int64 value represent the start time and the lower 32 bits represent the end time. To query stores that are open from 14:00 to 15:30, convert the times into a time period in minutes counted from 00:00 of the day. In this case, 14:00 to 15:30 is represented as (840,930):
filter=bit_struct(open_time, "0-31,32-63","overlap,$1,$2,840,930")!=-1fieldlen: obtains the length of a LITERAL field.
Example:
Return documents in which the username usr_name is not empty.
query=default:'keyword'&&filter=fieldlen(usr_name)>0in/notin: determines whether a field value is (or is not) in a specified list.
Example:
Query documents that contain "iphone" and whose type (of the int type) is 1, 2, or 3:
query=default:'iphone'&&filter=in(type, "1|2|3")Query documents that contain "iphone" and whose type (of the int32 type) is not 1, 2, or 3:
query=default:'iphone'&&filter=notin(type, "1|2|3")multi_attr: returns the value at a specified position in an array field.
Example:
A product has multiple prices [market price, discount price, sales price], which are stored in the prices field. Query mobile phones whose sales price is less than 1000:
query=default:'mobile phone'&&filter=multi_attr(price,2)<1000Examples
An application has an int32 field named category, whose values include 1 (news) and 2 (bbs). To query documents whose category is 2 (bbs) and that contain "Zhejiang University":
query=default:'Zhejiang University' AND category_search:'2' //An index field named category_search is created for the category field. //Alternatively query=default:'Zhejiang University'&&filter=category=2A novel application has a LITERAL_ARRAY field named tags, which represents novel style tags such as "palace", "suspense and horror", and "romance". To query documents that contain "Empresses in the Palace" and whose tags include "palace":
query=default:'Empresses in the Palace'&&filter=tags="palace"An e-commerce application has the int32 fields hit (clicks) and sale (sales volume) and the int64 field create_time. To query documents that contain "dress", for which the sum of sales volume and clicks multiplied by rate (offset rate) exceeds 10000, and whose creation time is earlier than 1402345600:
query=default:'dress'&&filter=(hit+sale)*rate>10000 AND create_time<1402345600