范围查询
在上一章介绍了批量导入和Term多种查询,有兴趣的可以参考一下 elasticSearch核心概念的介绍(七):常见的数据类型 这里我们来介绍一下另外几种查询方法
查找指定字段在指定范围内包含值(日期、数字或字符串)的文档
-
查找nba在打球在2年到10年以为的的球员
-
请求
curl -X POST "http://172.25.45.150:9200/nba/_search" -H 'Content-Type:application/json' -d ' { "query":{ "range":{ "play_year":{ "gte":2, "lte":15 } } }, "from":0, "size":100 } '
-
-
查找1980年到1999年出生的球员
-
请求
curl -X POST "http://172.25.45.150:9200/nba/_search" -H 'Content-Type:application/json' -d ' { "query":{ "range":{ "birthDay":{ "gte":"01-01-1999", "lte":"2022", "format":"dd-MM-yy||yyyy" } } }, "from":0, "size":100 } '
-
-
must(查找名字叫做库里的球员)
-
请求
curl -X POST "http://172.25.45.150:9200/nba/_search" -H 'Content-Type:application/json' -d ' { "query": { "bool": { "must": [ { "match": { "name": "库里" } } ] } } } '
-
-
must_not
-
请求
curl -X POST "http://172.25.45.150:9200/nba/_search" -H 'Content-Type:application/json' -d ' { "query": { "bool": { "must": [ { "match": { "name": "库里" } } ], "must_not":[ { "term":{ "name":"库里" } } ] } } } '
-
-
should
查找名字叫做库里的打球时间应该在11到20年的球员
should即使匹配不到也返回,只是评分不同
- 如果mininum_should_match =1,则变成要查出名字叫做库里的打球时间在11到20年的球员
- mininum_should_match代表了最小匹配精度,如果设置了,那么should语法语句中至少需要一个满足条件
-
请求
curl -X POST "http://172.25.45.150:9200/nba/_search" -H 'Content-Type:application/json' -d ' { "query": { "bool": { "must": [ { "match": { "name": "库里" } } ], "should":[ { "range":{ "playYear":{ "gte":11, "lte":20 } } } ] } } } '
- 如果mininum_should_match =1,则变成要查出名字叫做库里的打球时间在11到20年的球员
-
按照打球时间排序
-
请求
curl -X POST "http://172.25.45.150:9200/nba/_search" -H 'Content-Type:application/json' -d ' { "query":{ "match":{ "name":"库里" } }, "sort":[ { "jerse_no":{ "order":"desc" } } ], "from":0, "size":100 } '
-
响应
{ "took": 1, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 2, "relation": "eq" }, "max_score": null, "hits": [ { "_index": "nba", "_type": "_doc", "_id": "5", "_score": null, "_source": { "name": "库里", "team_name": "勇士", "position": "组织后卫", "play_year": "10", "jerse_no": "30", "ip_addr": "192.168.0.1" }, "sort": [ "30" ] }, { "_index": "nba", "_type": "_doc", "_id": "kodFGn8B4RxnBaUYxdlU", "_score": null, "_source": { "name": "库里", "team_name": "勇士", "position": "控球后卫", "play_year": "10", "jerse_no": "22", "title": "the best shooter" }, "sort": [ "22" ] } ] } }
-