query clause

Updated at:

Description

The `query` clause is a fundamental and essential part of a search statement. It specifies what to search for in an index field. You can also specify multiple query conditions and their relationships, such as AND, OR, ANDNOT, or 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 then retrieves documents where the search query hits the source text of either the `title` or `subject` field.

If you create a separate index named `title_search` for the `title` field, a query on the `title_search` index retrieves a document only if the search query hits the source text of the `title` field.

Syntax

Simple query

Syntax:

query=index_name:'search_query'^boost query_condition index_name:'search_query'^boost

​

  • index_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 on the source fields that are associated with the specified index.

  • search_query: The content to search for.

  • boost: The weight of the query clause. The value must be an integer in the range of 0 to 99. The default value is 99.

  • query_condition: The logical operator that connects multiple query clauses. The supported operators are (), AND, OR, ANDNOT, and RANK. These operators must be in uppercase. The order of precedence from lowest to highest is RANK, OR, AND, ANDNOT, and ().

  • AND: Retrieves documents that match both query clauses. For example, `query=default:'phone' AND default:'bluetooth'` retrieves documents that contain both "phone" and "bluetooth".

  • OR: Retrieves documents that match either query clause. For example, `query=default:'phone' OR default:'bluetooth'` retrieves documents that contain either "phone" or "bluetooth".

  • ANDNOT: Retrieves documents that match the first query clause but not the second. For example, `query=default:'phone' ANDNOT default:'bluetooth'` retrieves documents that contain "phone" but not "bluetooth".

  • RANK: Retrieves documents that match the first query clause. The second query clause is optional and is used only to adjust the relevance score. For example, `query=default:'phone' RANK default:'bluetooth'` retrieves all documents that contain "phone". Among these results, documents that also contain "bluetooth" are ranked higher.

Advanced query

Multiple query clauses sharing an index name

Syntax:

query=index_name:'search_query'^boost | 'search_query'^boost

query=index_name:'search_query'^boost & 'search_query'^boost

​

The `|` operator specifies an OR relationship between query clauses. The `&` operator specifies an AND relationship.

​

Phrase query

Syntax:

query=index_name:"search_query"^boost query_condition index_name:"search_query"^boost

​

Enclosing a search query in "" performs a phrase query. This requires the resulting terms to be adjacent and in the same order after the query is tokenized.

​

  • If a `search_query` is enclosed in double quotation marks (`""`), it is treated as a phrase query. A phrase query requires that the terms in the `search_query` appear adjacent to each other and in the same order within the document.

  • Range queries include geospatial queries and numeric range queries.

Geo query

Syntax:

query=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 must be 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, `maxLAT` must be greater than or equal to `minLAT`. If you reverse these values, the system adjusts them automatically. For longitude, the range from `minLON` to `maxLON` is interpreted from west to east. If you reverse the longitude values, the range will be incorrect.

  • Polygon: `polygon(LON1 LAT1, LON2 LAT2, LON3 LAT3, LON4 LAT4, ...)`. The polygon can be convex or concave. The start and end points of the polygon must be the same. Adjacent edges cannot be collinear. The edges of the polygon cannot intersect.

​

Note:

  • The index used in the query must be of the spatial type.

  • Enclose the shape argument for a spatial index query in single quotation marks (`''`). For example: `query=spatial_index:'circle(130.0 10.0,1000.0)'`.

  • The point coordinates for lines and polygons are mapped to a flat world map to determine their range. This mapping does not account for crossing the 180-degree meridian. Query results for a point index are precise. Query results for line and polygon indexes require row filtering.

Numeric range query

Syntax:

query=index_name:(value1,value2]

​

`value1` and `value2` represent 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 retrieves data where 3 < x < 100.

Closed interval query: `query = price:[3,100]`. This retrieves data where 3 <= x <= 100.

Half-open interval query: `query = price:(3,100]`. This retrieves data where 3 < x <= 100.

Unspecified start value: `query=price:(, 100)`. This retrieves data where x < 100.

​

Note:

  • The index used in the query must be of a numeric type.

  • The values must be integers. Floating-point numbers are not supported.

Date query

Syntax:

query=index_name:(start_time,end_time]

The start and end times are integer timestamps in milliseconds. If you do not specify a start time, the search starts from the timestamp 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 index used in the query must be of the date type.

  • The timestamp must be an integer in milliseconds. If the value exceeds 4102416000000, it is treated as 4102416000000.