This topic describes the syntax and usage notes of the query clause for index retrieval.
Overview
The query clause is a required part of a search statement. It specifies which content to search for in an index field and supports multiple conditions and relationships between them (AND, OR, ANDNOT, and RANK).
For example, you can combine two text type fields, such as title and subject, into a single composite index named default. A search against the default index then retrieves documents where the search query matches the content of either the title or subject source fields.
If you index the title field separately as title_search, queries against the title_search index retrieve only documents whose title field matches the search query.
Syntax
Query conditions use the following formats:
Standard query: index:'search_term'^boost
range search: index:'circle(longitude latitude,radius)'
range search: index:[value1,value2]
-
index (index field): The name of the index field to search, as defined in your index schema. The search finds documents where the search query appears in any of the source fields that comprise this index field.
-
search query: The content that you want to search for.
-
boost: The relevance boost to apply to the search query. The value must be an integer from 0 to 99. If unspecified, the default is 99. This parameter is optional and only takes effect if the
exact_match_boost()function is configured in the rough sort expression. -
Multiple query conditions are supported. Supported operators include
(),AND,OR,ANDNOT,RANK(must be uppercase). Their precedence, from lowest to highest, is:RANK<OR<AND<ANDNOT<() -
To perform a phrase query, enclose the search query in double quotation marks (
""). A phrase query finds documents where the analyzed terms appear in the same order and position as in the original phrase. -
A range search supports queries on geographical locations and numerical ranges. For more information, see range search.
Usage notes
-
The query clause is required and cannot be empty..
-
ANDNOT cannot be used on its own. The query to the left of ANDNOT cannot be null, that is, index_name:''.;
-
If RANK is not at the end of the statement, for example,
index1:'xxx' RANK index2:'xxx' AND index3:'xxx', bind RANK to the other conditions with parentheses (), as in(index1:'xxx' RANK index2:'xxx') AND index3:'xxx'. Otherwise, all conditions after RANK participate in RANK, as inindex1:'xxx' RANK (index2:'xxx' AND index3:'xxx') -
Enclose query terms in single or double quotation marks. Otherwise, the query can fail and return no results, or produce unexpected behavior.
-
You can only use
TEXT,SHORT_TEXTfields to create a composite index. -
A boost below 0 is treated as 0. A boost above 99 is treated as 99.
-
For more information about how different search types are processed, see built-in analyzer.
Examples
-
Search for documents whose title index field contains "Peking University":
query=title:'Peking University' -
Search for documents whose title index field contains both "Peking University" and "Zhejiang University":
query=title:'Peking University' AND title:'Zhejiang University' -
Search for 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' -
Search for documents whose title index field contains "Peking University" or "Zhejiang University".An alternative OR syntax:
query=title:'Peking University'|'Zhejiang University' -
Search for documents whose title index field contains Peking University but not Tsinghua. If title contains president, rank those documents higher:
query=(title:'Peking University' ANDNOT title:'Tsinghua') RANK title:'president' // Fine sort expression: text_relevance(title) -
Search for documents whose title index field contains Peking University. The phrase Peking University must remain contiguous. Documents with separated terms, such as "Peking City University", must not be returned:
query=title:"Peking University" -
Search a numeric array (INT_ARRAY) field:
// Assume that an int array field is indexed as arr_index_1. // Retrieves records with an array element whose value is 1. query=arr_index_1:'1' -
Search a string array (LITERAL_ARRAY) field: LITERAL_ARRAY is an array of string constants. Each element supports only exact matching (without tokenization). Assume that a LITERAL_ARRAY field is indexed as tags_index:
// Retrieves the document if any array element exactly equals 'red'. // LITERAL_ARRAY uses exact matching: the query must equal an entire element. For an element 'Peking University', a query for 'Peking' does not match. query=tags_index:'red' // Search for documents whose array contains 'red' or 'blue'. query=tags_index:'red' OR tags_index:'blue'