All Products
Search
Document Center

OpenSearch:SQL statement

Last Updated:Feb 28, 2024

query clause

The query clause is the most basic and indispensable part of a statement. The query clause defines the specific content to be queried based on a specific index field. You can specify multiple query conditions and define their relationships by using logical operators such as AND, OR, ANDNOT, and RANK.

For example, a table contains the title field and the subject field of the TEXT data type. You can create a composite index named default based on these fields. In the query clause of a statement, you can specify the default index and search queries that consist of terms in the title field or subject field to query the documents that match the search queries.

You can also create an index named title_search based on the title field. When you perform a query based on the title_search index, the system returns only the documents in which values in the title field match the search queries that you specified.

Syntax

Simple queries

Syntax:

query=Index name:'Search query'^boost Logical operator Index name:'Search query'^boost

  • Index name: the index based on which you want to perform the query. The specified index must be included in the index schema that you created. The index field is used to search for the documents in which the source fields of the specified index field contain the search query.

  • Search query: the content for which you want to search.

  • boost: the weight that you want to specify for a search query. The value of this parameter must be of the INT type. Valid values: [0,99]. If you do not specify the boost parameter, the default boost value is 99.

  • Logical operator: the operator that you want to use to define logical relationships between query conditions. The following operators are supported: (), AND, OR, ANDNOT, and RANK. You must specify operators in uppercase letters. The priorities of these operators are in the following sequence: RANK < OR < AND < ANDNOT < ().

  • AND: If you specify this operator between two query conditions, the system returns only the documents that match both query conditions. For example, you can use the query=default:'Mobile Phone' AND default:'Bluetooth' clause to query documents that contain Mobile Phone and Bluetooth.

  • OR: If you specify this operator between two query conditions, the system returns the documents that match one query condition. For example, you can use the query=default:'Mobile Phone' OR default:'Bluetooth' clause to query documents that contain Mobile Phone or Bluetooth.

  • ANDNOT: If you specify this operator between two conditions, the system returns the documents that match only the left condition. For example, you can use the query=default:'Mobile Phone' ANDNOT default:'Bluetooth' clause to query documents that contain Mobile Phone and do not contain Bluetooth.

  • RANK: If you specify this operator between two conditions, the system returns the documents that match the left condition and may not match the right condition. For example, you can use the query=default:'Mobile Phone' RANK default:'Bluetooth' clause to query documents that contain Mobile Phone and may not contain Bluetooth. You can use this operator if specific search queries are required to calculate relevance scores, and these search queries do not affect the query results. In the preceding example, the default:'Bluetooth' condition is used to calculate relevance scores based on text in documents. In this case, the documents that contain Bluetooth are assigned high ranks.

Advanced queries

Specify multiple search queries based on the same index

Syntax:

query=Index name:'Search query'^boost | 'Search query'^boost

query=Index name:'Search query'^boost & 'Search query'^boost

The vertical bar (|) specifies the OR operator. The ampersand (&) specifies the AND operator.

Phrase queries

Syntax:

query=Index name:"Search query"^boost Logical operator Index name:"Search query"^boost

When you write a phrase query statement, enclose each search query with double quotation marks ("). In a phrase query, all the terms are connected and arranged in the same order before and after analysis.

  • A search query that is enclosed in double quotation marks (") is a phrase query. In a phrase query, all the terms are connected and arranged in the same order before and after analysis.

  • Range queries include geography queries and value range queries.

Geography queries

Syntax:

query=Index name:'SHAPE(ARGS...)'

You can specify the following SHAPE(ARGS...) in the query clause:

  • Point: You can specify a point in the point(LON LAT) format. LON specifies the longitude value of the point, and LAT specifies the latitude value of the point. Separate the longitude value and the latitude value with a space.

  • Circle: You can specify a circle in the circle(LON LAT,Radius) format. LON specifies the longitude value of the center for the circle, LAT specifies the latitude value of the center for the circle, and Radius specifies the radius of the circle. Unit: meters.

  • Rectangle: You can specify a rectangle in the rectangle(minLON minLAT,maxLON maxLAT) format. Take note that for latitude values, the value of maxLAT must be greater than or equal to minLAT. If the maxLAT value is smaller than the minLAT value, the system automatically exchanges the values. For longitude values, the value of minLON must be smaller than the value of maxLON. If the specified value of minLON is greater than the specified value of maxLON, the result is incorrect.

  • Polygon: You can specify a polygon in the polygon(LON1 LAT1,LON2 LAT2,LON3 LAT3,LON4 LAT4,...) format. You can specify a convex polygon or a concave polygon. The start point and the end point of the polygon must be the same point. You cannot specify adjacent sides that are collinear or sides that intersect.

Note:

  • The index that you specify must be of the SPATIAL type.

  • The expression that you use to define the circle must be included in single quotation marks ('). Example: query=spatial_index:'circle(130.0 10.0,1000.0)'.

  • The point coordinates of the lines and polygons are mapped to a flat world map to determine the scope of the line queries and polygon queries, regardless of the case of crossing 180 degrees longitude. The query result of the inverted index on the location field is accurate. The query results of the inverted index on the line and polygon fields need to be filtered.

Value range queries

Syntax:

query=Index name:(Numeric 1,Numeric 2]

Numeric 1 specifies the start of the value range, and Numeric 2 specifies the end of the value range. You can use open endpoints and closed endpoints to specify ranges. In the sample code, the opening parenthesis ( before Numeric 1 specifies that Numeric 1 is an open endpoint, and the closing bracket ] next to Numeric 2 specifies that Numeric 2 is a closed endpoint.

Examples:

query=price:(3,100): queries values that are greater than 3 and smaller than 100.

query=price:[3,100]: queries values that are greater than or equal to 3 and smaller than or equal to 100.

query=price:(3,100]: queries values that are greater than 3 and smaller than or equal to 100.

query=price:(, 100): queries values that are smaller than 100. In this clause, the start of the value range is not specified.

Note:

  • The index field must be of a numeric type.

  • Values in the specified field must be integers. Floating-point numbers are not supported.

Date queries

Syntax:

query=Index name:(Start time,End time).

The timestamps of the start time and end time must be of the INTEGER type and accurate to milliseconds. If you do not specify a start time, the system starts the scan from the value 0. If you do not specify an end time, the default value 4102416000000 is used. The default value indicates the following timestamp: 2100-01-01 00:00. You can use open endpoints and closed endpoints to specify ranges.

Note:

  • The index field must be of the DATE type.

  • The timestamps must be of the INTEGER type and accurate to milliseconds. If a timestamp that you specify exceeds 4102416000000, the system uses 4102416000000 as the timestamp.