词条查询
Returns documents that contain an exact term in a provided field.
返回指定字段中包含精确词条的文档。
You can use the term
query to find documents based on a precise value such as a price, a product ID, or a username.
您可以使用term
查询根据精确的值(例如价格,产品ID或用户名)查找文档。
警告:
Avoid using the term
query for text
fields.
避免对text
字段使用term
查询。
By default, Elasticsearch changes the values of text
fields as part of analysis. This can make finding exact matches for text
field values difficult.
默认情况下,Elasticsearch在analysis的阶段会更改text
字段的值,一部分。这使得很难按照精确匹配来查找text
字段的值。
To search text
field values, use the match
query instead.
要搜索text
字段值,请改用match
查询。
GET /_search
{
"query": {
"term": {
"user": {
"value": "Kimchy",
"boost": 1.0
}
}
}
}
Copy as cURLView in Console
term
term
的一级参数
field
(Required, object) Field you wish to search.
(必填,对象)您要搜索的字段。
field
field
的参数
value
(Required, string) Term you wish to find in the provided field
. To return a document, the term must exactly match the field value, including whitespace and capitalization.
(必需,字符串)您希望在指定field
要查找的词条。要返回文档,该词条必须与字段值完全匹配,包括空格和大写字母。
boost
(Optional, float) Floating point number used to decrease or increase the relevance scores of a query. Defaults to 1.0
.
(可选,float)浮点数,用于降低或增加查询的 相关性分数。默认为1.0
。
You can use the boost
parameter to adjust relevance scores for searches containing two or more queries.
您可以使用该boost
参数来调整包含两个或多个查询的搜索的相关性得分。
Boost values are relative to the default value of 1.0
. A boost value between 0
and 1.0
decreases the relevance score. A value greater than 1.0
increases the relevance score.
boost值是相对于默认值的1.0
的。介于0
和 1.0
之间的boost值会降低相关性得分。大于1.0
的boost值会增加相关性得分。
term
query for text
fields避免对text
字段用`term查询
By default, Elasticsearch changes the values of text
fields during analysis. For example, the default standard analyzer changes text
field values as follows:
默认情况下,Elasticsearch在分析期间更改 text
字段的值。例如,默认的标准分析器按如下方式更改text
字段值:
Removes most punctuation
Divides the remaining content into individual words, called tokens
Lowercases the tokens
删除大多数标点符号
将剩余内容分为单个词,称为 token
小写token
To better search text
fields, the match
query also analyzes your provided search term before performing a search. This means the match
query can search text
fields for analyzed tokens rather than an exact term.
为了更好地搜索text
字段,match
查询在执行搜索之前会为您提供的搜索词进行分词处理。这意味着match
查询可以用分析后的tokens在text
字段中进行搜索,而不是精确的词条。
The term
query does not analyze the search term. The term
query only searches for the exact term you provide. This means the term
query may return poor or no results when searching text
fields.
term
查询并没有分析搜索词。term
查询仅搜索您提供的精确词条。这意味着term
在搜索text
字段时查询可能返回差的结果或没有结果。
To see the difference in search results, try the following example.
要查看搜索结果的差异,请尝试以下示例。
Create an index with a text
field called full_text
.
创建一个带text
类型的full_text
字段的索引。
PUT my_index
{
"mappings" : {
"properties" : {
"full_text" : { "type" : "text" }
}
}
}
Copy as cURLView in Console
Index a document with a value of Quick Brown Foxes!
in the full_text
field.
添加一个full_text
字段值为Quick Brown Foxes!
的文档。
PUT my_index/_doc/1
{
"full_text": "Quick Brown Foxes!"
}
Copy as cURLView in Console
Because full_text
is a text
field, Elasticsearch changes Quick Brown Foxes!
to [quick, brown, fox]
during analysis.
由于full_text
是一个text
字段,因此Elasticsearch 在分析期间会将Quick Brown Foxes!
更改为 [quick, brown, fox]
。
Use the term
query to search for Quick Brown Foxes!
in the full_text
field. Include the pretty
parameter so the response is more readable.
使用term
查询在full_text
字段中搜索Quick Brown Foxes!
。包括pretty
参数,以便响应更易读。
GET my_index/_search?pretty
{
"query": {
"term": {
"full_text": "Quick Brown Foxes!"
}
}
}
Copy as cURLView in Console
Because the full_text
field no longer contains the exact term Quick Brown Foxes!
, the term
query search returns no results.
因为full_text
字段不再包含精确的词条Quick Brown Foxes!
,所以term
查询搜索不返回任何结果。
Use the match
query to search for Quick Brown Foxes!
in the full_text
field.
使用match
查询在full_text
字段中搜索Quick Brown Foxes!
。
GET my_index/_search?pretty
{
"query": {
"match": {
"full_text": "Quick Brown Foxes!"
}
}
}
Copy as cURLView in Console
Unlike the term
query, the match
query analyzes your provided search term, Quick Brown Foxes!
, before performing a search. The match
query then returns any documents containing the quick
, brown
, or fox
tokens in the full_text
field.
与term
查询不同,match
查询Quick Brown Foxes!
在执行搜索之前会为您提供的搜索词进行分词 。match
查询会返回full_text
字段中中包含quick
,brown
或fox
词条的文档 。
Here’s the response for the match
query search containing the indexed document in the results.
这是match
查询返回的搜索结果,其中包含索引到的文档。
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.8630463,
"hits" : [
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.8630463,
"_source" : {
"full_text" : "Quick Brown Foxes!"
}
}
]
}
}