您当前的位置: 首页 >  ar

陈橙橙丶

暂无认证

  • 2浏览

    0关注

    107博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

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

陈橙橙丶 发布时间:2022-02-21 13:55:15 ,浏览量:2

范围查询

在上一章介绍了批量导入和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
      }
      '
      
布尔查询 typedescriptionmust必须出现在文档中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"
                      ]
                  }
              ]
          }
      }
      
关注
打赏
1648473527
查看更多评论
立即登录/注册

微信扫码登录

0.0395s