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

本文涉及的产品
Elasticsearch Serverless通用抵扣包,测试体验金 200元
简介: 分布式系列教程(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
        ]
      }
    ]
  }
}



相关实践学习
以电商场景为例搭建AI语义搜索应用
本实验旨在通过阿里云Elasticsearch结合阿里云搜索开发工作台AI模型服务,构建一个高效、精准的语义搜索系统,模拟电商场景,深入理解AI搜索技术原理并掌握其实现过程。
ElasticSearch 最新快速入门教程
本课程由千锋教育提供。全文搜索的需求非常大。而开源的解决办法Elasricsearch(Elastic)就是一个非常好的工具。目前是全文搜索引擎的首选。本系列教程由浅入深讲解了在CentOS7系统下如何搭建ElasticSearch,如何使用Kibana实现各种方式的搜索并详细分析了搜索的原理,最后讲解了在Java应用中如何集成ElasticSearch并实现搜索。  
目录
相关文章
|
6月前
|
安全 Java Linux
Linux安装Elasticsearch详细教程
Linux安装Elasticsearch详细教程
890 1
|
7月前
|
数据采集 JSON 数据挖掘
Elasticsearch 的DSL查询,聚合查询与多维度数据统计
Elasticsearch的DSL查询与聚合查询提供了强大的数据检索和统计分析能力。通过合理构建DSL查询,用户可以高效地搜索数据,并使用聚合查询对数据进行多维度统计分析。在实际应用中,灵活运用这些工具不仅能提高查询效率,还能为数据分析提供深入洞察。理解并掌握这些技术,将显著提升在大数据场景中的分析和处理能力。
318 20
|
5月前
|
存储 安全 Linux
Elasticsearch Enterprise 9.0 发布 - 分布式搜索和分析引擎
Elasticsearch Enterprise 9.0 (macOS, Linux, Windows) - 分布式搜索和分析引擎
224 0
|
5月前
|
存储 Linux iOS开发
Elasticsearch Enterprise 8.18 发布 - 分布式搜索和分析引擎
Elasticsearch Enterprise 8.18 (macOS, Linux, Windows) - 分布式搜索和分析引擎
150 0
|
11月前
|
存储 索引
Elasticsearch分布式架构
【11月更文挑战第2天】
160 1
|
自然语言处理 搜索推荐 数据库
高性能分布式搜索引擎Elasticsearch详解
高性能分布式搜索引擎Elasticsearch详解
308 4
高性能分布式搜索引擎Elasticsearch详解
|
12月前
|
存储 JSON 监控
大数据-167 ELK Elasticsearch 详细介绍 特点 分片 查询
大数据-167 ELK Elasticsearch 详细介绍 特点 分片 查询
538 4
|
12月前
|
SQL NoSQL MongoDB
一款基于分布式文件存储的数据库MongoDB的介绍及基本使用教程
一款基于分布式文件存储的数据库MongoDB的介绍及基本使用教程
306 0
|
12月前
|
自然语言处理 搜索推荐 Java
SpringBoot 搜索引擎 海量数据 Elasticsearch-7 es上手指南 毫秒级查询 包括 版本选型、操作内容、结果截图(一)
SpringBoot 搜索引擎 海量数据 Elasticsearch-7 es上手指南 毫秒级查询 包括 版本选型、操作内容、结果截图
236 0
|
12月前
|
存储 自然语言处理 搜索推荐
SpringBoot 搜索引擎 海量数据 Elasticsearch-7 es上手指南 毫秒级查询 包括 版本选型、操作内容、结果截图(二)
SpringBoot 搜索引擎 海量数据 Elasticsearch-7 es上手指南 毫秒级查询 包括 版本选型、操作内容、结果截图(二)
213 0

热门文章

最新文章