The filter clause is used to search for documents in which users are interested based on query conditions. You can use a filter clause to filter documents that are queried by using a query clause to retrieve required documents.
Format of a filter condition: field=value.
Filter conditions support common conditional operators, such as >, <, =, <=, >=, !=,in, and notin, and arithmetic operators, such as +, -, *, /, &, ^, and |.
You can specify multiple filter conditions and connect them by using the following logical operators: AND, OR, and (). The logical operators must be uppercase.
The filter clause is optional.
The fields that you reference in the parameters of the filter clause must be configured as attribute fields when you define the application schema.
An exact equivalence cannot be checked for fields of the FLOAT and DOUBLE types due to precision issues. Use > and < in such scenarios.
You must use double quotation marks (") to enclose field values for fields of the LITERAL type in the filter clause. Otherwise, an error code 6135 and the following error message are returned: The value type of the constant expression is invalid. Fields of the LITERAL type support relational operations, but do not support arithmetic operations.
You can use functionality functions such as the distance function in the filter clause for sorting.
- To filter fields of the LITERAL type, you can use only operators = (equal to) and != (not equal to), but cannot use relational operators such as > and <. Fields of the LITERAL type are not segmented, and exact match is required.
- You can use the in or notin function to check whether field values are in a specific list. Only fields of the INT or FLOAT type are supported. Fields of the ARRAY, LITERAL, or TEXT type and the analyzer for fuzzy searches are not supported. For more information, see in and notin.
You cannot create attribute fields by using fields of text types, such as TEXT and SHORT_TEXT. You can create attribute fields only by using fields of numeric types or non-text types, such as INT, INT_ARRAY, FLOAT, FLOAT_ARRAY, DOUBLE, DOUBLE_ARRAY, LITERAL, and LITERAL_ARRAY.
Supported functionality functions
distance: returns the spherical distance between two points. Generally, this function is used in distance calculation for a location-based service (LBS).
Search for KFCs within 10 kilometers from the coordinates of a user. In this example, the longitude and latitude of the coordinates of the user are 120.34256 and 30.56982. The lon and lat fields that indicate the longitude and latitude of a KFC must be configured as attribute fields.
in_polygon: judges whether a point is within a specific polygon geographic area. Generally, this function is used to determine whether a user is within the delivery radius of merchants.
Judge whether a user is within the delivery radius of merchants. For example, the coordinates field indicates the delivery radius of merchants, and the coordinates of a user are (120.307234, 39.294245). Then, you can use the following query clause to search for merchants that support the delivery radius:
query=default:'Foods'&&filter=in_polygon(coordinates, 120.307234, 39.294245)>0
in_query_polygon: judges whether a point specified in a document is within a specified polygon geographic area.
Search for KFCs in the Yintai business district (xA,yA,xB,Yb,xC,Yc;xD,yD,xE,yE,xF,yF,xG,yG). The doc_point field is used to store the geographic location of KFCs.
bit_struct: splits each value into multiple parts for a field of the INT_ARRAY type and performs a specific operation on the parts.
Search for the stores that are open in a specified period of time. Your document has the open_time field of the INT_ARRAY type. Each 64-bit integer in the array indicates the business hours of a store. The first 32 bits of an integer indicate the time when the store is open. The last 32 bits of an integer indicate the time when the store is closed. If you want to search for stores that are open from 14:00 to 15:30, you must first convert each of the start time and end time to the number of minutes that have elapsed from 00:00 on the same day. In this example, 14:00 is converted to 840, and 15:30 is converted to 930.
fieldlen: calculates the value length of a field of the LITERAL type.
Search for documents in which the value of the usr_name field is not empty.
in/notin: checks whether field values are in a specific list.
Retrieve documents that contain "iphone" and the type field whose value is 1, 2, or 3. The type field is of the INT type.
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.
multi_attr: returns the value of the specified array field at the specified position.
Commodities have multiple prices, including market prices, discount prices, and sales prices. The prices are recorded in the prices field. You can use the following query clause to search for mobile phones whose sales price is less than 1,000:
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