Query clause

Updated at:

Clause description

The query clause is a required part of a search statement. It specifies what content to search for in which index field. You can also specify multiple query conditions and the relationships between them (AND \ OR \ ANDNOT \ RANK).

For example, you can combine the title and subject fields, which are of the text type, to create a shared index field named default. A query on the default index then retrieves documents in which the search terms match the original text of either title or subject.

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 terms match the original text of the title field.

Syntax

Query conditions use the following formats:

Regular query: index:'search term'^boost

Range queries: index:'circle(longitude latitude,radius)'

Range queries: index:[value1,value2]

  • index (index field): the index information configured when you define the index schema. It specifies that the system searches the source fields included in the specified index field for documents that contain the search term.

  • search term: the content to search for.

  • boost: the weight of the search term. The value is of the int type and ranges from 0 to 99. Default value: 99. This parameter takes effect only if you configure exact_match_boost() in the rough sort expression. This feature is optional.

  • You can specify multiple query conditions. The supported relationships are (), AND, OR, ANDNOT, and RANK (must be in uppercase). The priorities in ascending order are: RANK<OR<AND<ANDNOT<()

  • If the search term is enclosed in "", the query is a phrase query. In this case, the terms obtained after the search term is tokenized must be adjacent and appear in the same order.

  • Range queries, including geographic location analysis and numeric analysis.

Usage notes

  • The query clause is required and cannot be empty.

  • The ANDNOT operator cannot be used alone, and the query on the left side of the ANDNOT operator cannot be null, that is, index_name:''.

  • If RANK is not placed at the end of the statement, for example, index1:'xxx' RANK index2:'xxx' AND index3:'xxx', you must use () to bind RANK with the other conditions, that is, (index1:'xxx' RANK index2:'xxx') AND index3:'xxx'. Otherwise, all conditions connected after RANK are included in RANK, that is, index1:'xxx' RANK (index2:'xxx' AND index3:'xxx').

  • Search terms must be enclosed in single or double quotation marks. Otherwise, the query fails with an error and returns no results, or behaves unpredictably.

  • Only fields of the TEXT and SHORT_TEXT types can be used to create combined indexes.

  • If the boost value is less than 0, 0 is used. If the boost value is greater than 99, 99 is used.

  • For more information about the meanings of the different types of searches, see Text analyzers.

Examples

  1. Query documents whose title index field contains "Peking University":

    query=title:'Peking University'
  2. Query documents whose title index field contains both "Peking University" and "Zhejiang University":

    query=title:'Peking University' AND title:'Zhejiang University'
  3. Query documents whose title index field contains "Peking University" or "Zhejiang University" and whose type is "1":

    query=(title:'Peking University' OR title:'Zhejiang University') AND type:'1'
  4. Query documents whose title index field contains "Peking University" or "Zhejiang University" by using an alternative OR syntax:

    query=title:'Peking University'|'Zhejiang University'
  5. Query documents whose title index field contains Peking University but does not contain Tsinghua. Documents whose title contains president are ranked higher:

    query=(title:'Peking University' ANDNOT title:'Tsinghua') RANK title:'president'
    // The fine sort expression is: text_relevance(title)
  6. Query documents whose title index field contains Peking University, where Peking University must not be split, so that documents such as "a university in Peking" are not returned:

    query=title:"Peking University"
  7. Search on an array-type field:

    //Assume that a field of the int arr type is configured as the index arr_index_1
    //A record is retrieved if any element of the array has the value 1
    query=arr_index_1:'1'