query clause

更新时间:
复制 MD 格式

Query clauses define what to search for, which index fields to search, and how to combine multiple conditions by using operators such as AND, OR, ANDNOT, and RANK.

Clause description

A query clause specifies what to search for in an index field. You can define multiple conditions and combine them with operators such as AND, OR, ANDNOT, and RANK.

For example, you can combine two TEXT fields, such as `title` and `subject`, to create a common index named `default`. A query on the `default` index then retrieves documents where the search query matches 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 that index retrieves a document only if the search query matches the text in the `title` field.

Syntax

Simple queries

Syntax:

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

  • index_name: The index configured in the index schema. The search is performed on the source fields of this index to find documents that contain the search query.

  • search_query: The content to search for.

  • boost: The weight of the search query. The value must be an integer from 0 to 99. If you do not specify this parameter, the default value is 99.

  • search_condition: One or more search conditions. The supported operators, listed in ascending order of priority, are `RANK`, `OR`, `AND`, `ANDNOT`, and `()`. The operators must be in uppercase.

  • AND: Finds documents that match both search queries. For example, `query=default:'mobile phone' AND default:'bluetooth'` retrieves documents that contain both "mobile phone" and "bluetooth".

  • OR: Finds documents that match at least one of the search queries. For example, `query=default:'mobile phone' OR default:'bluetooth'` retrieves documents that contain "mobile phone" or "bluetooth".

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

  • RANK: Finds documents that match the first search query, regardless of whether they match the second. For example, `query=default:'mobile phone' RANK default:'bluetooth'` retrieves documents that contain "mobile phone" but does not require them to contain "bluetooth". Use this operator when a query should affect scoring but not retrieval. Combined with text relevance calculations, it can rank documents that contain "bluetooth" higher.

Advanced queries

Use one index name for multiple search queries

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_term"^boost OPERATOR index_name:"search_term"^boost

Enclosing a search query in double quotation marks ("") creates a phrase query, which requires the tokenized terms to be adjacent and in the same order.

  • To perform a phrase query, enclose the search query in double quotation marks (`""`). A phrase query matches terms that are contiguous and in the same order after tokenization.

  • Range queries include queries on geographic and numeric data.

weakand queries

Syntax:

query=index_name:('search_query'||'search_query')^number

query=index_name:('search_query')^^number

A weakand query retrieves documents that match only a part of a long search query. You can specify the minimum number of terms that must be matched in two ways. If you tokenize the query before sending it, use the `^` operator to specify the exact minimum number of matched terms. If the DPI engine tokenizes the query and you do not know the number of terms in advance, use the `^^` operator to set a minimum match ratio. The `number` parameter for `^^` is a value in thousandths. The minimum number of matched terms is calculated as `(Number of terms after tokenization × number / 1000.0)`, and the result is rounded up.

A weakand query performs between an AND query and an OR query. Performance improves as the minimum number of matched terms increases.

Note: The weakand query feature is disabled by default. To use this feature, contact technical support.

Geo-queries

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. Use a space to separate them.

  • 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 the order, the values are automatically adjusted. For longitude, the range from `minX` to `maxX` is interpreted from west to east. If you reverse the order, the range becomes incorrect.

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

Note:

  • The index for the query must be of the spatial type.

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

  • The coordinates for lines and polygons are mapped onto a flat world map to define their range. The system does not consider shapes that cross the 180-degree meridian. Query results for `location` indexes are precise. Results for `line` and `polygon` indexes require row filtering.

Numeric range queries

Syntax:

query=index_name:(value1,value2]

`value1` and `value2` represent the start and end of the numeric range. Range queries support open and closed intervals. A parenthesis `(` indicates an open interval, and a square bracket `]` indicates a closed interval.

Examples:

Open interval: `query = price:(3,100)`. This query retrieves data where `3 < x < 100`.

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

Half-open interval: `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 index for the query must be of the numeric range type.

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

Date queries

Syntax:

query=index_name:(start_time,end_time]

Start and end times are integer UNIX timestamps in milliseconds. If you do not specify a start time, the search begins from 0. If you do not specify an end time, the default value is 4102416000000, which corresponds to `2100-01-01 00:00:00`. Date queries also support open and closed intervals.

Note:

  • The index for the query must be of the date type.

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