match_all
match_all的查询方式简单粗暴,就是匹配所有,不需要传递任何参数:
Post:localhost:9200/tml-userinfo/_doc/_search
请求body
{ "query": { "match_all": { } } }
返回结果也是全部的12条数据
{ "took": 7, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 12, "relation": "eq" }, "max_score": 1.0, "hits": [ { "_index": "tml-userinfo", "_type": "_doc", "_id": "5", "_score": 1.0, "_source": { "age": 28, "sex": "男", "name": "李小林" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "7", "_score": 1.0, "_source": { "age": 8, "sex": "女", "name": "李小美" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "2", "_score": 1.0, "_source": { "age": 28, "sex": "男", "name": "森小林" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "3", "_score": 1.0, "_source": { "age": 8, "sex": "男", "name": "森小贤" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "4", "_score": 1.0, "_source": { "age": 8, "sex": "男", "name": "李小贤" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "10", "_score": 1.0, "_source": { "age": 18, "sex": "女", "name": "李小玲" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "12", "_score": 1.0, "_source": { "age": 18, "sex": "男", "name": "李小辰" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "1", "_score": 1.0, "_source": { "age": 18, "sex": "男", "name": "森小辰" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "6", "_score": 1.0, "_source": { "age": 28, "sex": "男", "name": "李小林" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "8", "_score": 1.0, "_source": { "age": 8, "sex": "女", "name": "森小美" } } ] } }
match_phrase
match_phrase属于短语匹配,能保证分词间的邻近关系,相当于对文档的关键词进行重组以匹配查询内容,对于匹配了短语"森 小 林"的文档,下面的条件必须为true:
- 森 、小、 林必须全部出现在某个字段中
- 小的位置必须比森的位置大1
- 林的位置必须比森的位置大2
我们来尝试下对姓名进行检索,请求头和上边完全一样,就不再赘述,直接看请求体,先来看一个不按顺序的
{ "query": { "match_phrase": { "name": "森林小" } } }
没有返回任何数据
{ "took": 7, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 0, "relation": "eq" }, "max_score": null, "hits": [] } }
再来看一个正确按顺序的:
{ "query": { "match_phrase": { "name": "森小林" } } }
返回了我们对应的文档:
{ "took": 8, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 1, "relation": "eq" }, "max_score": 2.3487744, "hits": [ { "_index": "tml-userinfo", "_type": "_doc", "_id": "2", "_score": 2.3487744, "_source": { "age": 28, "sex": "男", "name": "森小林" } } ] } }
muti_match
muti_match标识了只要有一个字段里有匹配短语的词就返回,我们试着在name和sex里找,只要包含男的我们就返回该数据,为了验证该特性,我们特意插入一条数据
这样,即使性别为女,如果能返回该数据,说明在两个字段都检索了
{ "query": { "multi_match": { "query": "男", "fields": ["name","sex"] } } }
查看返回结果,我们可以看到这条数据返回了:
{ "took": 114, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 8, "relation": "eq" }, "max_score": 0.9808291, "hits": [ { "_index": "tml-userinfo", "_type": "_doc", "_id": "5", "_score": 0.9808291, "_source": { "age": 28, "sex": "男", "name": "李小林" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "13", "_score": 0.9808291, "_source": { "age": 18, "sex": "女", "name": "李小男" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "1", "_score": 0.6931471, "_source": { "age": 18, "sex": "男", "name": "森小辰" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "6", "_score": 0.6931471, "_source": { "age": 28, "sex": "男", "name": "李小林" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "2", "_score": 0.44183272, "_source": { "age": 28, "sex": "男", "name": "森小林" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "3", "_score": 0.44183272, "_source": { "age": 8, "sex": "男", "name": "森小贤" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "4", "_score": 0.44183272, "_source": { "age": 8, "sex": "男", "name": "李小贤" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "12", "_score": 0.44183272, "_source": { "age": 18, "sex": "男", "name": "李小辰" } } ] } }
range
range查询,顾明思意就是范围查询,例如我们这里要查询年龄在19到28的人的数据:
{ "query": { "range": { "age" : { "gte" : 19, "lt" : 29 } } } }
返回结果为28岁的员工信息:
{ "took": 8, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 4, "relation": "eq" }, "max_score": 1.0, "hits": [ { "_index": "tml-userinfo", "_type": "_doc", "_id": "5", "_score": 1.0, "_source": { "age": 28, "sex": "男", "name": "李小林" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "2", "_score": 1.0, "_source": { "age": 28, "sex": "男", "name": "森小林" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "6", "_score": 1.0, "_source": { "age": 28, "sex": "男", "name": "李小林" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "11", "_score": 1.0, "_source": { "age": 28, "sex": "女", "name": "森小捷" } } ] } }
这里需要解释下:
range 查询可同时提供包含(inclusive)和不包含(exclusive)这两种范围表达式,可供组合的选项如下:
- gt: > 大于(greater than)
- lt: < 小于(less than)
- gte: >= 大于或等于(greater than or equal to)
- lte: <= 小于或等于(less than or equal to)
可以依据自己的需求自由进行组合。
exists
exists允许你过滤文档,只查找那些在特定字段有值的文档,无论其值是多少,为了验证,需要注意,这里的有值即使是空值也算有值,只要不是null,我们新建一个性别为空值的数据【14】,和一个性别为null的数据【15】
可以看到无该字段:
然后进行查询:
{ "query": { "exists": { "field": "sex" } } }
命中14条,第15条没有命中
{ "took": 3, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 14, "relation": "eq" }, "max_score": 1.0, "hits": [ { "_index": "tml-userinfo", "_type": "_doc", "_id": "5", "_score": 1.0, "_source": { "age": 28, "sex": "男", "name": "李小林" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "7", "_score": 1.0, "_source": { "age": 8, "sex": "女", "name": "李小美" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "13", "_score": 1.0, "_source": { "age": 18, "sex": "女", "name": "李小男" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "2", "_score": 1.0, "_source": { "age": 28, "sex": "男", "name": "森小林" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "3", "_score": 1.0, "_source": { "age": 8, "sex": "男", "name": "森小贤" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "4", "_score": 1.0, "_source": { "age": 8, "sex": "男", "name": "李小贤" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "10", "_score": 1.0, "_source": { "age": 18, "sex": "女", "name": "李小玲" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "12", "_score": 1.0, "_source": { "age": 18, "sex": "男", "name": "李小辰" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "14", "_score": 1.0, "_source": { "age": 18, "sex": "", "name": "李小男" } }, { "_index": "tml-userinfo", "_type": "_doc", "_id": "1", "_score": 1.0, "_source": { "age": 18, "sex": "男", "name": "森小辰" } } ] } }