Skip to main content

关键字匹配 term

Term Query 关键词查询

查找包含倒排索引中指定关键词的文档。例如:

GET /_search
{
"query" : {
"term" : { "user" : "kimchy" }
}
}

可以指定 boost 参数来为该关键词提供比另一个查询更高的相关性分数,例如:

GET _search
{
"query": {
"bool": {
"should": [
{
"term": {
"status": {
"value": "urgent",
"boost": 2.0
}
}
},
{
"term": {
"status": "normal"
}
}
]
}
}
}
tip

urgent查询子句的提升为 2.0,这意味着它的重要性是普通查询子句的两倍。

Terms Query 包含多个词

GET /_search
{
"query": {
"terms" : { "user" : ["kimchy", "elasticsearch"]}
}
}

Terms Set Query 集合类查询

返回至少一个或多个提供的关键词匹配的任何文档。由于terms未被分词 必须完全匹配。因此由最小应匹配字段控制或按最小应匹配脚本计算每个文档。

PUT /my-index
{
"mappings": {
"_doc": {
"properties": {
"required_matches": {
"type": "long"
}
}
}
}
}

PUT /my-index/_doc/1?refresh
{
"codes": ["ghi", "jkl"],
"required_matches": 2
}

PUT /my-index/_doc/2?refresh
{
"codes": ["def", "ghi"],
"required_matches": 2
}
GET /my-index/_search
{
"query": {
"terms_set": {
"codes" : {
"terms" : ["abc", "def", "ghi"],
"minimum_should_match_field": "required_matches"
}
}
}
}