All Products
Search
Document Center

OpenSearch:query clause

Last Updated:Jun 17, 2024

Overview

A query clause is the most basic and indispensable part of a statement. The query clause defines the 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 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 the values of the title field match the search queries that you specify.

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 create. The system scans the values of the field based on which you create the specified index and returns the documents in which the values of the field match the search query that you specify.

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

  • boost: the weight that you want to specify for the 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 query 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

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.

  • If you write a phrase query statement, enclose each search query in double quotation marks ("). 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.

Weakand queries

Syntax:

query=Index name:('Search query'||'Search query')^number

query=Index name:('Search query')^^number

If the search query that you want to use is long, you can use a weakand query to retrieve the documents that contain only some terms after the search query is analyzed. You can use one of the following methods to specify the minimum number of terms contained in a document: use ^number to directly specify the minimum number of terms contained in a document if you manually analyze the search query, or use ^^number to specify the ratio of terms contained in a document if the search engine analyzes the search query. If you write the query statement in the ^^number format, set number to a ratio that contains three decimal places. In this case, the minimum number of terms contained in a document is calculated by multiplying the total number of terms after analysis by the specified ratio and then rounding the result to the nearest integer. This method is applicable to scenarios in which the total number of terms after analysis is excessively large.

Weakand queries provide better performance than queries in which all query conditions are connected by using only the AND or OR operator. If you set the minimum number of terms contained in a document to a larger value, the weakand query performs better.

Note: By default, the weakand query feature is disabled. To enable this feature, contact technical support.

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: meter.

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

Take note of the following items:

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

  • The expression that you use to define the circle must be enclosed 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.

Take note of the following items:

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

Take note of the following items:

  • The index field must be of the DATE type.

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