转载自:https://blog.csdn.net/chengyuqiang/column/info/18392,ES版本号6.3.0
词项查询介绍
全文查询将在执行之前分析查询字符串,但词项级别查询将按照存储在倒排索引中的词项进行精确操作。
这些查询通常用于数字,日期和枚举等结构化数据,而不是全文本字段。或者,他们允许您制作低级查询,并在分析过程之前进行。
term查询
term查询用于词项搜索
terms查询
terms查询可以用来查询文档中包含任一个给定多词项的文档;
terms_set查询
terms_set查询是一个新的查询,它的语法将来可能会变
查找与一个或多个指定词项匹配的文档,其中必须匹配的术语数量取决于指定的最小值,应匹配字段或脚本
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"
}
}
}
}
查询结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.5753642,
"hits": [
{
"_index": "my-index",
"_type": "doc",
"_id": "2",
"_score": 0.5753642,
"_source": {
"codes": [
"def",
"ghi"
],
"required_matches": 2
}
}
]
}
}
一个总是限制匹配条件数量永远不会超过指定词项数量的例子如下,其中params.num_terms参数在脚本中可用,以指示已指定的词项数量
GET my-index/_search
{
"query": {
"terms_set":{
"codes":{
"terms" : ["abc", "def", "ghi"],
"minimum_should_match_script":{
"source": "Math.min(params.num_terms, doc['required_matches'].value)"
}
}
}
}
}
返回结果
{
"took": 163,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.5753642,
"hits": [
{
"_index": "my-index",
"_type": "doc",
"_id": "2",
"_score": 0.5753642,
"_source": {
"codes": [
"def",
"ghi"
],
"required_matches": 2
}
}
]
}
}
range查询
range查询用于匹配数值型、日期型或字符串字段在某一范围内的文档
(1)搜索age字段在10到20的所有文档
DELETE my-index
PUT my-index
PUT my-index/doc/1
{"age":12}
PUT my-index/doc/2
{"age":18}
PUT my-index/doc/3
{"age":21}
GET _search
{
"query": {
"range": {
"age": {
"gte": 10,
"lte": 20,
"boost": 2
}
}
}
}
返回
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 47,
"successful": 47,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 2,
"hits": [
{
"_index": "my-index",
"_type": "doc",
"_id": "2",
"_score": 2,
"_source": {
"age": 18
}
},
{
"_index": "my-index",
"_type": "doc",
"_id": "1",
"_score": 2,
"_source": {
"age": 12
}
}
]
}
}
(2)日期范围查询
GET website/_search
{
"query": {
"range": {
"postdate": {
"gte": "2017-01-01",
"lte": "2017-12-31",
"format": "yyyy-MM-dd"
}
}
}
}
返回结果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "website",
"_type": "blog",
"_id": "8",
"_score": 1,
"_source": {
"title": "es高亮",
"author": "程裕强",
"postdate": "2017-01-03",
"abstract": "Elasticsearch查询关键字高亮",
"url": "http://url/53991802"
}
}
]
}
}
exists查询
返回原始字段中至少包含一个非空值的文档
PUT my-index/doc/1
{ "user": "jane" }
PUT my-index/doc/2
{ "user": "" }
PUT my-index/doc/3
{ "user": [] }
PUT my-index/doc/4
{ "user": ["jane", null ] }
PUT my-index/doc/5
{ "age": 28 }
GET _search
{
"query": {
"exists":{"field":"user"}
}
}
返回结果
{
"took": 34,
"timed_out": false,
"_shards": {
"total": 47,
"successful": 47,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "my-index",
"_type": "doc",
"_id": "2",
"_score": 1,
"_source": {
"user": ""
}
},
{
"_index": "my-index",
"_type": "doc",
"_id": "4",
"_score": 1,
"_source": {
"user": [
"jane",
null
]
}
},
{
"_index": "my-index",
"_type": "doc",
"_id": "1",
"_score": 1,
"_source": {
"user": "jane"
}
}
]
}
}
prefix查询
(1)查询以ki开头的用户
GET /_search
{
"query": {
"prefix": {"user": "ki"}
}
}
返回
{
"took": 58,
"timed_out": false,
"_shards": {
"total": 47,
"successful": 47,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
(2)查询以ja开头的用户
GET /_search
{
"query": {
"prefix": {"user": "ja"}
}
}
返回
{
"took": 11,
"timed_out": false,
"_shards": {
"total": 47,
"successful": 47,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "my-index",
"_type": "doc",
"_id": "4",
"_score": 1,
"_source": {
"user": [
"jane",
null
]
}
},
{
"_index": "my-index",
"_type": "doc",
"_id": "1",
"_score": 1,
"_source": {
"user": "jane"
}
}
]
}
}
wildcard查询(通配符匹配)
GET website/_search
{
"query": {
"wildcard": {
"title":"*yum*"
}
}
}
返回
{
"took": 10,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "website",
"_type": "blog",
"_id": "6",
"_score": 1,
"_source": {
"title": "CentOS更换国内yum源",
"author": "程裕强",
"postdate": "2016-12-30",
"abstract": "CentOS更换国内yum源",
"url": "http://url.cn/53946911"
}
}
]
}
}
regexp查询(正则表达式查询)
正则表达式查询的性能很大程度上取决于所选的正则表达式。类似.*的匹配内容的正则表达式非常缓慢,并且使用了lookaround正则表达式。如果可以的话,请尝试在正则表达式开始之前使用长前缀。像.*?+这样的通配匹配符器大多会降低性能。
大多数正则表达式引擎,允许您匹配字符串的任何部分。如果你想让正则表达式从字符串开头开始,或者在字符串的末尾完成,那么你必须明确的定位它,使用^表示开始或$结束
GET website/_search
{
"query": {
"regexp":{
"title":"gc.*"
}
}
}
返回
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "website",
"_type": "blog",
"_id": "3",
"_score": 1,
"_source": {
"title": "CentOS升级gcc",
"author": "程裕强",
"postdate": "2016-12-25",
"abstract": "CentOS升级gcc",
"url": "http://url.cn/53868915"
}
}
]
}
}
fuzzy查询(模糊查询)
GET website/_search
{
"query": {
"fuzzy":{
"title":"vmwere"
}
}
}
返回
{
"took": 45,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.76896155,
"hits": [
{
"_index": "website",
"_type": "blog",
"_id": "4",
"_score": 0.76896155,
"_source": {
"title": "vmware复制虚拟机",
"author": "程裕强",
"postdate": "2016-12-29",
"abstract": "vmware复制虚拟机",
"url": "http://url.cn/53946664"
}
}
]
}
}
type查询
GET /_search
{
"query": {
"type" : {
"value" : "my_type"
}
}
}
返回
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 47,
"successful": 47,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "my_index",
"_type": "my_type",
"_id": "2",
"_score": 1,
"_source": {
"city": "York"
}
},
{
"_index": "my_index",
"_type": "my_type",
"_id": "1",
"_score": 1,
"_source": {
"city": "New York"
}
}
]
}
}
ids查询
GET _search
{
"query": {
"ids":{
"type": "blog",
"values":["2","3"]
}
}
}
返回
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 47,
"successful": 47,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "website",
"_type": "blog",
"_id": "2",
"_score": 1,
"_source": {
"title": "watchman源码编译",
"author": "程裕强",
"postdate": "2016-12-23",
"abstract": "CentOS7.x的watchman源码编译",
"url": "http://url.cn/53844169"
}
},
{
"_index": "website",
"_type": "blog",
"_id": "3",
"_score": 1,
"_source": {
"title": "CentOS升级gcc",
"author": "程裕强",
"postdate": "2016-12-25",
"abstract": "CentOS升级gcc",
"url": "http://url.cn/53868915"
}
}
]
}
}