text
类型的查询, 可以用于处理各种文本. 例如:
{ "text" : { "message" : "this is a test" } }
注意, 虽然他的名字叫text, 但可以用它来精确匹配 (类似于 term
) 数字和日期.
其中, message
是字段的名称, 你可以用你实际使用的字段名来替换 (包括 _all
).
Text Queries的类型
boolean
默认的 text
查询是 boolean
型的. 意思就是说提供的文本会被分析构建为一个布尔型查询. operator
标志可以使用 or
或者 and
来组合布尔子句 (默认为 or
).
analyzer
用于设定在分析过程中哪一个分析器会用于处理这段文本. 它会使用mapping中定义的分析器, 如果没有定义则会使用索引的默认分析器.
fuzziness
can be set to a value (depending on the relevant type, for string types it should be a value between 0.0
and 1.0
) to constructs fuzzy queries for each term analyzed. The prefix_length
and max_expansions
can be set in this case to control the fuzzy process.
下面这个例子使用了额外的参数 (注意例子中的结构变化, message
是字段的名称):
{ "text" : { "message" : { "query" : "this is a test", "operator" : "and" } } }
phrase
text_phrase
查询会分析文本并且创建一个 phrase
查询. 例如:
{ "text_phrase" : { "message" : "this is a test" } }
既然 text_phrase
只是 text
查询的一个 种类
, 你也可以使用下面的方式:
{ "text" : { "message" : { "query" : "this is a test", "type" : "phrase" } } }
A phrase query maintains order of the terms up to a configurable slop
(which defaults to 0).
The analyzer
can be set to control which analyzer will perform the analysis process on the text. It default to the field explicit mapping definition, or the default search analyzer, for example:
{ "text_phrase" : { "message" : { "query" : "this is a test", "analyzer" : "my_analyzer" } } }
text_phrase_prefix
The text_phrase_prefix
is the same as text_phrase
, expect it allows for prefix matches on the last term in the text. For example:
{ "text_phrase_prefix" : { "message" : "this is a test" } }
Or:
{ "text" : { "message" : { "query" : "this is a test", "type" : "phrase_prefix" } } }
It accepts the same parameters as the phrase type. In addition, it also accepts a max_expansions
parameter that can control to how many prefixes the last term will be expanded. It is highly recommended to set it to an acceptable value to control the execution time of the query. For example:
{ "text_phrase_prefix" : { "message" : { "query" : "this is a test", "max_expansions" : 10 } } }
Comparison to query_string / field
The text family of queries does not go through a “query parsing” process. It does not support field name prefixes, wildcard characters, or other “advance” features. For this reason, chances of it failing are very small / non existent, and it provides an excellent behavior when it comes to just analyze and run that text as a query behavior (which is usually what a text search box does). Also, the phrase_prefix
can provide a great “as you type” behavior to automatically load search results.