The term query finds documents that contain the exact term specified in the inverted index. For instance:
{
"term" : { "user" : "Kimchy" }
}
Finds documents which contain the exact term Kimchy in the inverted index of the user field.
boost参数
boost参数可以对查找的document数据加权, 提高score
GET /_search
{
"query": {
"bool": {
"should": [
{
"term": {
"status": {
"value": "urgent",
"boost": 2.0
}
}
},
{
"term": {
"status": "normal"
}
}
]
}
}
}
默认的权值是1.0,
但是urgent关键字的权值会被设置为2.0,
The urgent query clause has a boost of 2.0, meaning it is twice as important as the query clause for normal.
The normal clause has the default neutral boost of 1.0.
//定义一个索引
PUT my_index
{
"mappings": {
"my_type": {
"properties": {
"full_text": {
"type": "string"
},
"exact_value": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
The full_text
field is analyzed by default.
The exact_value
field is set to be not_analyzed.
//添加一条记录
PUT my_index/my_type/1
{
"full_text": "Quick Foxes!",
"exact_value": "Quick Foxes!"
}
The full_text
inverted index will contain the terms: [quick, foxes]
.
The exact_value
inverted index will contain the exact term: [Quick Foxes!].
可以看到full_text
字段默认会被分词分析, 所以”Quick Foxes!”, 会被拆分为两个单词[quick, foxes]
exact_value
字段在mapping时被设置为”not_analyzed”, 则不会被分词所以分析, 所以会作为一个整体[Quick Foxes!].
被记录
示例:
//匹配: because the exact_value field contains the exact term Quick Foxes!.
GET my_index/my_type/_search
{
"query": {
"term": {
"exact_value": "Quick Foxes!"
}
}
}
//不匹配, 因为full_text只是匹配quick,foxes, 而不是完整的term "Quick Foxes!"
GET my_index/my_type/_search
{
"query": {
"term": {
"full_text": "Quick Foxes!"
}
}
}
//匹配
GET my_index/my_type/_search
{
"query": {
"term": {
"full_text": "foxes"
}
}
}
//匹配, 注意此处使用的是match, match会首先分析查询条件, 然后去搜索两个分词quick, foxes
//This match query on the full_text field first analyzes the query string, then looks for documents containing quick or foxes or both.
GET my_index/my_type/_search
{
"query": {
"match": {
"full_text": "Quick Foxes!"
}
}
}