栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

elasticSearch核心概念的介绍(九):范围、布尔、排序查询

elasticSearch核心概念的介绍(九):范围、布尔、排序查询

范围查询

在上一章介绍了批量导入和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
}
'
布尔查询
typedescription
must必须出现在文档中
filter必须出现在文档中,但是不打分(_score为0)
must_root不能出现在文档中
should应该出现在文档中

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
                        }
                    }
                }
            ]
        }
    }
}
'
排序查询

按照打球时间排序

请求

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"
                ]
            }
        ]
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/746264.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号