Movatterモバイル変換


[0]ホーム

URL:


Loading

Simple query string query

Returns documents based on a provided query string, using a parser with a limited but fault-tolerant syntax.

This query uses asimple syntax to parse and split the provided query string into terms based on special operators. The query thenanalyzes each term independently before returning matching documents.

While its syntax is more limited than thequery_string query, thesimple_query_string query does not return errors for invalid syntax. Instead, it ignores any invalid parts of the query string.

GET /_search{  "query": {    "simple_query_string" : {        "query": "\"fried eggs\" +(eggplant | potato) -frittata",        "fields": ["title^5", "body"],        "default_operator": "and"    }  }}
query
(Required, string) Query string you wish to parse and use for search. SeeSimple query string syntax.
fields
(Optional, array of strings) Array of fields you wish to search.

This field accepts wildcard expressions. You also can boost relevance scores for matches to particular fields using a caret (^) notation. SeeWildcards and per-field boosts in thefields parameter for examples.

Defaults to theindex.query.default_field index setting, which has a default value of*. The* value extracts all fields that are eligible to term queries and filters the metadata fields. All extracted fields are then combined to build a query if noprefix is specified.

Warning

There is a limit on the number of fields that can be queried at once. It is defined by theindices.query.bool.max_clause_countsearch setting, which defaults to1024.

default_operator

(Optional, string) Default boolean logic used to interpret text in the query string if no operators are specified. Valid values are:

  • OR (Default)For example, a query string ofcapital of Hungary is interpreted ascapital OR of OR Hungary.
  • ANDFor example, a query string ofcapital of Hungary is interpreted ascapital AND of AND Hungary.
analyze_wildcard
(Optional, Boolean) Iftrue, the query attempts to analyze wildcard terms in the query string. Defaults tofalse. Note that, in case oftrue, only queries that end with a*are fully analyzed. Queries that start with* or have it in the middleare onlynormalized.
analyzer
(Optional, string)Analyzer used to convert text in the query string into tokens. Defaults to theindex-time analyzer mapped for thedefault_field. If no analyzer is mapped, the index’s default analyzer is used.
auto_generate_synonyms_phrase_query
(Optional, Boolean) Iftrue, the parser creates amatch_phrase query for eachmulti-position token. Defaults totrue. For examples, seeMulti-position tokens.
flags
(Optional, string) List of enabled operators for thesimple query string syntax. Defaults toALL (all operators). SeeLimit operators for valid values.
fuzzy_max_expansions
(Optional, integer) Maximum number of terms to which the query expands for fuzzy matching. Defaults to50.
fuzzy_prefix_length
(Optional, integer) Number of beginning characters left unchanged for fuzzy matching. Defaults to0.
fuzzy_transpositions
(Optional, Boolean) Iftrue, edits for fuzzy matching include transpositions of two adjacent characters (ab → ba). Defaults totrue.
lenient
(Optional, Boolean) Iftrue, format-based errors, such as providing a text value for anumeric field, are ignored. Defaults tofalse.
minimum_should_match
(Optional, string) Minimum number of clauses that must match for a document to be returned. See theminimum_should_match parameter for valid values and more information.
quote_field_suffix
(Optional, string) Suffix appended to quoted text in the query string.

You can use this suffix to use a different analysis method for exact matches. SeeMixing exact search with stemming.

Thesimple_query_string query supports the following operators:

  • + signifies AND operation
  • | signifies OR operation
  • - negates a single token
  • " wraps a number of tokens to signify a phrase for searching
  • * at the end of a term signifies a prefix query
  • ( and) signify precedence
  • ~N after a word signifies edit distance (fuzziness)
  • ~N after a phrase signifies slop amount

To use one of these characters literally, escape it with a preceding backslash (\).

The behavior of these operators may differ depending on thedefault_operator value. For example:

GET /_search{  "query": {    "simple_query_string": {      "fields": [ "content" ],      "query": "foo bar -baz"    }  }}

This search is intended to only return documents containingfoo orbar that also donot containbaz. However because of adefault_operator ofOR, this search actually returns documents that containfoo orbar and any documents that don’t containbaz. To return documents as intended, change the query string tofoo bar +-baz.

You can use theflags parameter to limit the supported operators for the simple query string syntax.

To explicitly enable only specific operators, use a| separator. For example, aflags value ofOR|AND|PREFIX disables all operators exceptOR,AND, andPREFIX.

GET /_search{  "query": {    "simple_query_string": {      "query": "foo | bar + baz*",      "flags": "OR|AND|PREFIX"    }  }}

The available flags are:

ALL (Default)
Enables all optional operators.
AND
Enables the+ AND operator.
ESCAPE
Enables\ as an escape character.
FUZZY
Enables the~N operator after a word, whereN is an integer denoting the allowed edit distance for matching. SeeFuzziness.
NEAR
Enables the~N operator, after a phrase whereN is the maximum number of positions allowed between matching tokens. Synonymous toSLOP.
NONE
Disables all operators.
NOT
Enables the- NOT operator.
OR
Enables the\| OR operator.
PHRASE
Enables the" quotes operator used to search for phrases.
PRECEDENCE
Enables the( and) operators to control operator precedence.
PREFIX
Enables the* prefix operator.
SLOP
Enables the~N operator, after a phrase whereN is maximum number of positions allowed between matching tokens. Synonymous toNEAR.
WHITESPACE
Enables whitespace as split characters.

Fields can be specified with wildcards, eg:

GET /_search{  "query": {    "simple_query_string" : {      "query":    "Will Smith",      "fields": [ "title", "*_name" ]    }  }}
  1. Query thetitle,first_name andlast_name fields.

Individual fields can be boosted with the caret (^) notation:

GET /_search{  "query": {    "simple_query_string" : {      "query" : "this is a test",      "fields" : [ "subject^3", "message" ]    }  }}
  1. Thesubject field is three times as important as themessage field.

By default, thesimple_query_string query parser creates amatch_phrase query for eachmulti-position token in the query string. For example, the parser creates amatch_phrase query for the multi-word synonymny, new york:

(ny OR ("new york"))

To match multi-position tokens with anAND conjunction instead, setauto_generate_synonyms_phrase_query tofalse:

GET /_search{  "query": {    "simple_query_string": {      "query": "ny city",      "auto_generate_synonyms_phrase_query": false    }  }}

For the above example, the parser creates the followingbool query:

(ny OR (new AND york)) city)

Thisbool query matches documents with the termny or the conjunctionnew AND york.

Welcome to the docs for thelatest Elastic product versions, including Elastic Stack 9.0 and Elastic Cloud Serverless.To view previous versions, go toelastic.co/guide.


[8]ページ先頭

©2009-2025 Movatter.jp