分布式系列教程(32) -ElasticSearch条件查询

ElasticSearch可以执行复杂的条件查询,下面直接举例子:

首先先添加文档:

PUT /user_dao/user_table/1
{
  "name":"baby",
  "sex":0,
  "age":1
}
PUT /user_dao/user_table/2
{
  "name":"father",
  "sex":0,
  "age":26
}
PUT /user_dao/user_table/3
{
  "name":"mother",
  "sex":1,
  "age":24
}
PUT /user_dao/user_table/4
{
  "name":"grandfather",
  "sex":1,
  "age":60
}
PUT /user_dao/user_table/5
{
  "name":"grandmother",
  "sex":1,
  "age":58
}

1. 根据id进行查询

GET /user_dao/user_table/1

在这里插入图片描述
2.查询当前所有类型的文档

GET /user_dao/user_table/_search

在这里插入图片描述
3.根据多个ID批量查询 ,查询多个id分别为1、2

GET /user_dao/user_table/_mget
{
  "ids":["1","2"]
}

在这里插入图片描述

4.查询年龄为年龄24岁

GET /user_dao/user_table/_search?q=age:24

在这里插入图片描述
5. 查询年龄20岁-60岁之间(注意:TO 一定要大写)

GET /user_dao/user_table/_search?q=age[20 TO 60]

返回结果:

{
  "took": 77,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 4,
    "max_score": 1,
    "hits": [
      {
        "_index": "user_dao",
        "_type": "user_table",
        "_id": "5",
        "_score": 1,
        "_source": {
          "name": "grandmother",
          "sex": 1,
          "age": 58
        }
      },
      {
        "_index": "user_dao",
        "_type": "user_table",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "father",
          "sex": 0,
          "age": 26
        }
      },
      {
        "_index": "user_dao",
        "_type": "user_table",
        "_id": "4",
        "_score": 1,
        "_source": {
          "name": "grandfather",
          "sex": 1,
          "age": 60
        }
      },
      {
        "_index": "user_dao",
        "_type": "user_table",
        "_id": "3",
        "_score": 1,
        "_source": {
          "name": "mother",
          "sex": 1,
          "age": 24
        }
      }
    ]
  }
}

6.查询年龄20岁-60岁之间并且年龄降序、从0条数据到第1条数据

GET /user_dao/user_table/_search?q=age[30 TO 60]&sort=age:desc&from=0&size=1

返回内容:

{
  "took": 57,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": null,
    "hits": [
      {
        "_index": "user_dao",
        "_type": "user_table",
        "_id": "4",
        "_score": null,
        "_source": {
          "name": "grandfather",
          "sex": 1,
          "age": 60
        },
        "sort": [
          60
        ]
      }
    ]
  }
}

7.查询年龄20岁-60岁之间并且年龄降序、从0条数据到第1条数据,展示name和age字段

GET /user_dao/user_table/_search?q=age[30 TO 60]&sort=age:desc&from=0&size=1&_source=name,age

返回结果:

{
  "took": 48,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": null,
    "hits": [
      {
        "_index": "user_dao",
        "_type": "user_table",
        "_id": "4",
        "_score": null,
        "_source": {
          "name": "grandfather",
          "age": 60
        },
        "sort": [
          60
        ]
      }
    ]
  }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值