ElasticSearch中结构化查询(term、terms、range、exists、match、bool)
ElasticSearch中结构化查询(term、terms、range、exists、match、bool)
·
term查询
term 主要用于精确匹配哪些值,比如数字,日期,布尔值或 not_analyzed 的字符串(未经分析的文本数据类型):
{ "term": { "age": 26 }}
{ "term": { "date": "2014-09-01" }}
{ "term": { "public": true }}
{ "term": { "tag": "full_text" }}
当前数据库中的数据:
POST /study/_search
# 请求数据
{
"query": {
"term": {
"age": 20
}
}
}
# 响应数据
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "study",
"_type": "_doc",
"_id": "SHjOVIIBpyNh4YQ4CVSN",
"_score": 1.0,
"_source": {
"name": "张三",
"age": 20,
"mail": "111@qq.com",
"hobby": "羽毛球、乒乓球、足球"
}
}
]
}
}
terms查询
terms 跟 term 相似,但 terms 允许指定多个匹配条件。 如果某个字段指定了多个值,那么文档需要一起去做匹配:
{
"terms": {
"tag": ["search", "full_text", "nosql"]
}
}
POST /study/_search
# 请求数据
{
"query": {
"terms": {
"age": [20, 21, 22]
}
}
}
# 响应数据
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "study",
"_type": "_doc",
"_id": "SXjOVIIBpyNh4YQ4CVSN",
"_score": 1.0,
"_source": {
"name": "李四",
"age": 21,
"mail": "222@qq.com",
"hobby": "羽毛球、乒乓球、足球、篮球"
}
},
{
"_index": "study",
"_type": "_doc",
"_id": "SHjOVIIBpyNh4YQ4CVSN",
"_score": 1.0,
"_source": {
"name": "张三",
"age": 20,
"mail": "111@qq.com",
"hobby": "羽毛球、乒乓球、足球"
}
},
{
"_index": "study",
"_type": "_doc",
"_id": "SnjOVIIBpyNh4YQ4CVSN",
"_score": 1.0,
"_source": {
"name": "王五",
"age": 22,
"mail": "333@qq.com",
"hobby": "羽毛球、篮球、游泳、听音乐"
}
}
]
}
}
range查询
range 过滤允许按照指定范围查找一批数据
范围操作符包含:
- gt :大于
- gte : 大于等于
- lt : 小于
- lte : 小于等于
POST /study/_search
# 请求数据
{
"query": {
"range": {
"age": {
"gte": 20,
"lte": 22
}
}
}
}
# 响应数据
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "study",
"_type": "_doc",
"_id": "SXjOVIIBpyNh4YQ4CVSN",
"_score": 1.0,
"_source": {
"name": "李四",
"age": 21,
"mail": "222@qq.com",
"hobby": "羽毛球、乒乓球、足球、篮球"
}
},
{
"_index": "study",
"_type": "_doc",
"_id": "SHjOVIIBpyNh4YQ4CVSN",
"_score": 1.0,
"_source": {
"name": "张三",
"age": 20,
"mail": "111@qq.com",
"hobby": "羽毛球、乒乓球、足球"
}
},
{
"_index": "study",
"_type": "_doc",
"_id": "SnjOVIIBpyNh4YQ4CVSN",
"_score": 1.0,
"_source": {
"name": "王五",
"age": 22,
"mail": "333@qq.com",
"hobby": "羽毛球、篮球、游泳、听音乐"
}
}
]
}
}
exists查询
exists 查询可以用于查找文档中是否包含指定字段或没有某个字段,类似于SQL语句中的 IS_NULL 条件
注意:这个查询只是针对已经查出一批数据来,但是想区分出某个字段是否存在的时候使用。
POST /study/_search
# 请求数据
{
"query": {
"exists": {
"field": "age"
}
}
}
# 响应数据
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "study",
"_type": "_doc",
"_id": "SXjOVIIBpyNh4YQ4CVSN",
"_score": 1.0,
"_source": {
"name": "李四",
"age": 21,
"mail": "222@qq.com",
"hobby": "羽毛球、乒乓球、足球、篮球"
}
},
{
"_index": "study",
"_type": "_doc",
"_id": "S3jOVIIBpyNh4YQ4CVSN",
"_score": 1.0,
"_source": {
"name": "赵六",
"age": 23,
"mail": "444@qq.com",
"hobby": "跑步、游泳"
}
},
{
"_index": "study",
"_type": "_doc",
"_id": "SHjOVIIBpyNh4YQ4CVSN",
"_score": 1.0,
"_source": {
"name": "张三",
"age": 20,
"mail": "111@qq.com",
"hobby": "羽毛球、乒乓球、足球"
}
},
{
"_index": "study",
"_type": "_doc",
"_id": "SnjOVIIBpyNh4YQ4CVSN",
"_score": 1.0,
"_source": {
"name": "王五",
"age": 22,
"mail": "333@qq.com",
"hobby": "羽毛球、篮球、游泳、听音乐"
}
},
{
"_index": "study",
"_type": "_doc",
"_id": "THjOVIIBpyNh4YQ4CVSN",
"_score": 1.0,
"_source": {
"name": "孙七",
"age": 24,
"mail": "555@qq.com",
"hobby": "听音乐、看电影"
}
}
]
}
}
match 查询
match 查询是一个标准查询,不管需要全文本查询还是精确查询基本上都要用到它。
如果使用 match 查询一个全文本字段,它会在真正查询之前用分析器先分析 match 一下查询字符。
如果用 match 指定了一个确切值,在遇到数字,日期,布尔值或者 not_analyzed 的字符串时,它将为搜索给定的值。
{ "match": { "age": 26 }}
{ "match": { "date": "2014-09-01" }}
{ "match": { "public": true }}
{ "match": { "tag": "full_text" }}
POST /study/_search
# 请求数据
{
"query": {
"match": {
"age": "20"
}
}
}
# 响应数据
{
"took": 18,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "study",
"_type": "_doc",
"_id": "SHjOVIIBpyNh4YQ4CVSN",
"_score": 1.0,
"_source": {
"name": "张三",
"age": 20,
"mail": "111@qq.com",
"hobby": "羽毛球、乒乓球、足球"
}
}
]
}
}
数据库中数据:
bool查询
bool 查询可以用来合并多个条件查询结果的布尔逻辑,它包含以下操作符:
- must : 多个查询条件的完全匹配,相当于 and 。
- must_not :多个查询条件的相反匹配,相当于 not 。
- should : 至少有一个查询条件匹配, 相当于 or 。
这些参数可以分别继承一个查询条件或者一个查询条件的数组:
{
"bool": {
"must": {
"term": {
"age": "20"
}
},
"must_not": {
"term": {
"sex": "男"
}
},
"should": [{
"term": {
"age": 25
}
}, {
"term": {
"age": 26
}
}]
}
}
POST /study/_search
# 请求数据
{
"query": {
"bool": {
"must": {
"match": {
"hobby": "足球"
}
},
"must_not": {
"match": {
"hobby": "音乐"
}
}
}
}
}
# 响应数据
{
"took": 20,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.7194062,
"hits": [
{
"_index": "study",
"_type": "_doc",
"_id": "SHjOVIIBpyNh4YQ4CVSN",
"_score": 1.7194062,
"_source": {
"name": "张三",
"age": 20,
"mail": "111@qq.com",
"hobby": "羽毛球、乒乓球、足球"
}
},
{
"_index": "study",
"_type": "_doc",
"_id": "SXjOVIIBpyNh4YQ4CVSN",
"_score": 1.6817665,
"_source": {
"name": "李四",
"age": 21,
"mail": "222@qq.com",
"hobby": "羽毛球、乒乓球、足球、篮球"
}
}
]
}
}
POST /study/_search
# 请求数据
{
"query": {
"bool": {
"should": [
{
"match": {
"hobby": "足球"
}
},
{
"match": {
"age": 23
}
}
]
}
}
}
# 响应数据
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 1.7194062,
"hits": [
{
"_index": "study",
"_type": "_doc",
"_id": "SHjOVIIBpyNh4YQ4CVSN",
"_score": 1.7194062,
"_source": {
"name": "张三",
"age": 20,
"mail": "111@qq.com",
"hobby": "羽毛球、乒乓球、足球"
}
},
{
"_index": "study",
"_type": "_doc",
"_id": "SXjOVIIBpyNh4YQ4CVSN",
"_score": 1.6817665,
"_source": {
"name": "李四",
"age": 21,
"mail": "222@qq.com",
"hobby": "羽毛球、乒乓球、足球、篮球"
}
},
{
"_index": "study",
"_type": "_doc",
"_id": "S3jOVIIBpyNh4YQ4CVSN",
"_score": 1.0,
"_source": {
"name": "赵六",
"age": 23,
"mail": "444@qq.com",
"hobby": "跑步、游泳"
}
},
{
"_index": "study",
"_type": "_doc",
"_id": "SnjOVIIBpyNh4YQ4CVSN",
"_score": 0.6038003,
"_source": {
"name": "王五",
"age": 22,
"mail": "333@qq.com",
"hobby": "羽毛球、篮球、游泳、听音乐"
}
}
]
}
}
更多推荐
已为社区贡献30条内容
所有评论(0)