All Products
Search
Document Center

OpenSearch:query clause

Last Updated:Apr 06, 2023

Overview

A query clause is an essential part of a search query. A query clause defines the specific 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, you can combine the title and subject fields of the TEXT type to create the composite index field default. Then, you can perform searches based on the index field default to retrieve the documents in which the title or subject field contains the search query.

You can also use only the title field to create the index field title_search. Then, when you perform searches based on the index field title_search, only the documents in which the title field contains the search query can be retrieved.

Description

You can use query conditions in the following formats:

Regular query: index: 'Search query'^boost

Range search: index:'circle (Longitude Latitude,Radius)'

Range search: index:[Numeric value 1,Numeric value 2]

  • index: the index information that is configured when you define the index schema. The index field is used to search for the documents in which the source fields of the specified index field contain the search query.

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

  • boost: the weight that you want to specify for a 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. This parameter is optional and takes effect only when you configure the exact_match_boost() function in a rough sort expression.

  • You can specify multiple query conditions and associate the query conditions by using the following logical operators: (), AND, OR, ANDNOT, and RANK. All of these operators must be in uppercase. Those operators are ranked in the following priority order: RANK<OR<AND<ANDNOT<().

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

  • The range search is used for geographic location analysis and numerical analysis. For more information, see Range search.

Usage notes

  • The query clause is required and cannot be empty.

  • The ANDNOT operator cannot be used separately and the left condition next to the ANDNOT operator cannot be null, such as index_name:''.

  • If the RANK condition is not at the end of the query, such as index1:'xxx' RANK index2:'xxx' AND index3:'xxx', you must enclose the RANK operator and related conditions in parentheses (), such as (index1:'xxx' RANK index2:'xxx') AND index3:'xxx'. Otherwise, all the conditions after the RANK operator are included in the RANK condition. Therefore, index1:'xxx' RANK index2:'xxx' AND index3:'xxx' is equal to index1:'xxx' RANK (index2:'xxx' AND index3:'xxx').

  • A search query must be enclosed in single quotation marks (") or double quotation marks (""). Otherwise, a query error is returned or unexpected query results are returned.

  • Only fields of the TEXT and SHORT_TEXT types can be combined to create a composite index.

  • 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 various searches, see Built-in analyzer.

Examples

  1. Search for documents whose title contains Peking University.

    query=title: 'Peking University'
  2. Search for documents whose title contains Peking University and Zhejiang University.

    query=title: 'Peking University' AND title: 'Zhejiang University'
  3. Search for documents whose title contains Peking University or Zhejiang University and in which the type parameter is set to 1.

    query=(title: 'Peking University' OR title: 'Zhejiang University') AND type:'1'
  4. Search for documents whose title contains Peking University or Zhejiang University. In this example, the OR syntax is presented in another form.

    query=title: 'Peking University'|'Zhejiang University'
  5. Search for documents whose title contains Peking University but not Tsinghua. The documents whose title contains Principal are ranked in the front.

    query=(title:'Peking University' ANDNOT title:'Tsinghua') RANK title: 'Principal'
    // Fine sort expression: text_relevance(title)
  6. Search for documents whose title contains Peking University. Peking University cannot be broken into multiple terms and the documents that contain similar terms are not returned, such as "University in Beijing".

    query=title: 'Peking University'
  7. Perform searches based on the index field of the ARRAY type.

    // A field of the INT ARR type is used as the index arr_index_1
    // Retrieve the documents in which the value of the index arr_index_1 is 1.
    query=arr_index_1:'1'