Clause description
The `query` clause is a fundamental and required component of a search statement. It specifies what to search for and in which index field. You can also define multiple search conditions and their relationships, such as AND, OR, ANDNOT, and RANK.
For example, you can combine two TEXT fields, `title` and `subject`, to create a common index named `default`. A query on the `default` index retrieves documents where the search query hits the text in either the `title` or `subject` field.
If you create a separate index named `title_search` for the `title` field, a query on this index retrieves a document only if the search query hits the text in the `title` field.
Syntax
{
"query": ""
}The `query` clause is required. Its value is a string that consists of the index to be queried and the search query. For example, `title:'OpenSearch' AND tag:'1'`. For more information about the specific query syntax, see the Simple queries and Advanced queries sections.
Simple queries
Syntax:
index_name:'search_query'^boost operator index_name:'search_query'^boostindex_name: The name of an index that is configured in the index schema. This parameter specifies that the search for the `search_query` is performed in the source fields of this index.
search_query: The content to search for.
boost: The weight of the search query. The value must be an integer from 0 to 99. The default value is 99.
You can specify multiple conditions. The supported operators are
(),AND,OR,ANDNOT, andRANK. The operators must be in uppercase. The operator precedence from lowest to highest is RANK, OR, AND, ANDNOT, and ().AND finds the intersection of two search queries. For example, `default:'phone' AND default:'bluetooth'` retrieves documents that contain both `phone` and `bluetooth`.
OR finds the union of two search queries. For example, `default:'phone' OR default:'bluetooth'` retrieves documents that contain either `phone` or `bluetooth`.
ANDNOT retrieves documents that match the first query but not the second. For example, `default:'phone' ANDNOT default:'bluetooth'` retrieves documents that contain `phone` but not `bluetooth`.
RANK retrieves documents that match the first query, but not necessarily the second. For example, `default:'phone' RANK default:'bluetooth'` retrieves documents that contain `phone`. The documents are not required to contain `bluetooth`. This syntax is mainly used in scenarios where a search query affects only scoring, not retrieval. For example, you can use this syntax with text relevance calculations to rank documents that contain `bluetooth` higher.
Advanced queries
Multiple search queries for one index name
Syntax:
index_name:'search_query'^boost | 'search_query'^boost
index_name:'search_query'^boost & 'search_query'^boost`|` indicates an OR relationship between multiple search queries. `&` indicates an AND relationship.
Phrase queries
Syntax:
index_name:"search_query"^boost operator index_name:"search_query"^boost Enclose the `search_query` in double quotation marks (`""`) to perform a phrase query. A phrase query requires that the terms of the tokenized search query are contiguous and in the same order.
If a search query is enclosed in
"", it is treated as a phrase query. In a phrase query, the terms that result from tokenization must appear in the same order and in adjacent positions.Range queries comprise geo queries and numeric range queries.
Escape double quotation marks in phrase queries when generating the final `query` clause.
Geo queries:
Syntax:
index_name:'SHAPE(ARGS...)' SHAPE(ARGS...) can be one of the following:
Point: `point(LON LAT)`. LON is the longitude and LAT is the latitude. They are separated by a space.
Circle: `circle(LON LAT,Radius)`. LON is the longitude, LAT is the latitude, and Radius is the radius in meters.
Rectangle: `rectangle(minLON minLAT,maxLON maxLAT)`. For latitude, maxY must be greater than or equal to minY. If you reverse these values, the system adjusts them automatically. For longitude, minX to maxX is interpreted from west to east. If you reverse these values, the specified range is incorrect.
Polygon: `polygon(LON1 LAT1,LON2 LAT2,LON3 LAT3,LON4 LAT4,...)`. A polygon can be convex or concave. The start and end points of the polygon must be the same. Contiguous edges cannot be collinear. The edges of the polygon cannot intersect.
Note:
The queried index must be a spatial index.
Enclose the shape for a spatial index query in single quotation marks (`''`). For example: `query=spatial_index:'circle(130.0 10.0,1000.0)'`.
The coordinates of points for lines and polygons are mapped to a flat world map to determine their range. The system does not handle cases that cross the 180-degree meridian. The query results for a `location` inverted index are precise. The query results for `line` and `polygon` inverted indexes require row filtering.
Numeric range queries:
Syntax:
index_name:(value1,value2]`value1` and `value2` are the start and end values of the numeric range. Range queries support open and closed intervals. `(` indicates an open interval, and `]` indicates a closed interval.
Examples:
Open interval query: `query = price:(3,100)`. This query retrieves data where 3 < x < 100.
Closed interval query: `query = price:[3,100]`. This query retrieves data where 3 <= x <= 100.
Half-open interval query: `query = price:(3,100]`. This query retrieves data where 3 < x <= 100.
Unspecified start value: `query=price:(, 100)`. This query retrieves data where x < 100.
Note:
The queried index must be a numeric range index.
The values must be integers. Floating-point numbers are not supported.
Date queries:
Syntax:
index_name:(start_time,end_time]The start and end times are integer UNIX timestamps in milliseconds. If you do not specify a start time, the search starts from 0. If you do not specify an end time, the default value is 4102416000000, which is the millisecond timestamp for 2100-01-01 00:00:00. Date queries also support open and closed intervals.
Note:
The queried index must be of the date type.
The timestamp must be an integer in milliseconds. If the value exceeds 4102416000000, it is processed as 4102416000000.