1、term查询
term
主要用于精确匹配哪些值,比如数字,日期,布尔值或 not_analyzed
的字符串(未经分析的文本数据类型):
{ "term": { "age": 26 }}
{ "term": { "date": "2014-09-01" }}
{ "term": { "public": true }}
{ "term": { "tag": "full_text" }}
POST http://127.0.0.1:9200/user/_search
{
"query" : {
"term" : {
"age" : 20
}
}
}
返回的json
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "user",
"_type": "_doc",
"_id": "CCbjHW0BImzcoITOwYQl",
"_score": 1.0,
"_source": {
"name": "赵六",
"age": 23,
"mail": "444@qq.com",
"hobby": "跑步、游泳"
}
},
{
"_index": "user",
"_type": "_doc",
"_id": "CibmHW0BImzcoITOjYQL",
"_score": 1.0,
"_source": {
"name": "胡八",
"age": 23,
"mail": "888@qq.com",
"hobby": "乒乓球、足球"
}
}
]
}
}
2、terms查询
terms
跟 term
有点类似,但 terms
允许指定多个匹配条件。 如果某个字段指定了多个值,那么文档需要一起去做匹配:
POST http://127.0.0.1:9200/user/_search
{
"query" : {
"terms" : {
"age" : [20,21]
}
}
}
响应的json
{
"took": 27,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "user",
"_type": "_doc",
"_id": "BSbjHW0BImzcoITOwYQl",
"_score": 1.0,
"_source": {
"name": "张三",
"age": 20,
"mail": "111@qq.com",
"hobby": "羽毛球、乒乓球、足球"
}
},
{
"_index": "user",
"_type": "_doc",
"_id": "BibjHW0BImzcoITOwYQl",
"_score": 1.0,
"_source": {
"name": "李四",
"age": 21,
"mail": "222@qq.com",
"hobby": "羽毛球、乒乓球、足球、篮球"
}
}
]
}
}
3、range查询
range
过滤允许我们按照指定范围查找一批数据:
{
"range": {
"age": {
"gte": 20,
"lt": 30
}
}
}
范围操作符包含:
gt
:: 大于
gte
:: 大于等于
lt
:: 小于
lte
:: 小于等于
POST http://127.0.0.1:9200/user/_search
请求体:
{
"query": {
"range": {
"age": {
"gte": 20,
"lte": 22
}
}
}
}
4、exists查询
exists
查询可以用于查找文档中是否包含指定字段或没有某个字段,类似于SQL语句中的IS_NULL
条件
如下,查找age
不为空的文档
POST http://127.0.0.1:9200/user/_search
{
"query": {
"exists": {
"field": "age"
}
}
}
5、match查询
match
查询是一个标准查询,不管你需要全文本查询还是精确查询基本上都要用到它。
如果你使用 match
查询一个全文本字段,它会在真正查询之前用分析器先分析match
一下查询字符:
{
"query": {
"match": {
"hobby": "音乐"
}
}
}
响应的json
{
"took": 58,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 0.40618476,
"hits": [
{
"_index": "user",
"_type": "_doc",
"_id": "CSbjHW0BImzcoITOwYQl",
"_score": 0.40618476,
"_source": {
"name": "孙七",
"age": 24,
"mail": "555@qq.com",
"hobby": "听音乐、看电影"
}
},
{
"_index": "user",
"_type": "_doc",
"_id": "BybjHW0BImzcoITOwYQl",
"_score": 0.33081028,
"_source": {
"name": "王五",
"age": 22,
"mail": "333@qq.com",
"hobby": "羽毛球、篮球、游泳、听音乐"
}
}
]
}
}
如果用match
下指定了一个确切值,在遇到数字,日期,布尔值或者not_analyzed
的字符串时,它将为你搜索你给定的值:
{ "match": { "age": 26 }}
{ "match": { "date": "2014-09-01" }}
{ "match": { "public": true }}
{ "match": { "tag": "full_text" }}
6、bool查询
bool
查询可以用来合并多个条件查询结果的布尔逻辑,它包含以下操作符:
must
:: 多个查询条件的完全匹配,相当于and
。must_not
:: 多个查询条件的相反匹配,相当于not
。should
:: 至少有一个查询条件匹配, 相当于or
。
这些参数可以分别继承一个查询条件或者一个查询条件的数组:
{
"query": {
"bool": {
"must_not": {
"match": {
"name": "胡八"
}
},
"must": {
"term": {
"age": 23
}
},
"should": [
{
"match": {
"hobby": "跑步"
}
}
]
}
}
}
查询结果:
{
"took": 9,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 3.889535,
"hits": [
{
"_index": "user",
"_type": "_doc",
"_id": "CCbjHW0BImzcoITOwYQl",
"_score": 3.889535,
"_source": {
"name": "赵六",
"age": 23,
"mail": "444@qq.com",
"hobby": "跑步、游泳"
}
}
]
}
}
7、过滤查询
{
"query": {
"bool": {
"filter": {
"term": {
"age": 20
}
}
}
}
}
查询和过滤的对比
- 一条过滤语句会询问每个文档的字段值是否包含着特定值。
- 查询语句会询问每个文档的字段值与特定值的匹配程度如何。
- 一条查询语句会计算每个文档与查询语句的相关性,会给出一个相关性评分 _score,并且 按照相关性对匹配到的文档进行排序。 这种评分方式非常适用于一个没有完全配置结果的全文本搜索。
- 一个简单的文档列表,快速匹配运算并存入内存是十分方便的, 每个文档仅需要1个字节。这些缓存的过滤结果集与后续请求的结合使用是非常高效的。
- 查询语句不仅要查找相匹配的文档,还需要计算每个文档的相关性,所以一般来说查询语句要比过滤语句更耗时,并且查询结果也不可缓存。
建议:
做精确匹配搜索时,最好用过滤语句,因为过滤语句可以缓存数据。