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

简介: 分布式系列教程(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并实现搜索。  
目录
相关文章
|
5月前
|
存储 Linux iOS开发
Elasticsearch Enterprise 9.1.5 发布 - 分布式搜索和分析引擎
Elasticsearch Enterprise 9.1.5 (macOS, Linux, Windows) - 分布式搜索和分析引擎
441 0
|
6月前
|
JSON 监控 Java
Elasticsearch 分布式搜索与分析引擎技术详解与实践指南
本文档全面介绍 Elasticsearch 分布式搜索与分析引擎的核心概念、架构设计和实践应用。作为基于 Lucene 的分布式搜索引擎,Elasticsearch 提供了近实时的搜索能力、强大的数据分析功能和可扩展的分布式架构。本文将深入探讨其索引机制、查询 DSL、集群管理、性能优化以及与各种应用场景的集成,帮助开发者构建高性能的搜索和分析系统。
463 0
|
11月前
|
安全 Java Linux
Linux安装Elasticsearch详细教程
Linux安装Elasticsearch详细教程
1961 64
|
12月前
|
数据采集 JSON 数据挖掘
Elasticsearch 的DSL查询,聚合查询与多维度数据统计
Elasticsearch的DSL查询与聚合查询提供了强大的数据检索和统计分析能力。通过合理构建DSL查询,用户可以高效地搜索数据,并使用聚合查询对数据进行多维度统计分析。在实际应用中,灵活运用这些工具不仅能提高查询效率,还能为数据分析提供深入洞察。理解并掌握这些技术,将显著提升在大数据场景中的分析和处理能力。
627 20
|
10月前
|
存储 安全 Linux
Elasticsearch Enterprise 9.0 发布 - 分布式搜索和分析引擎
Elasticsearch Enterprise 9.0 (macOS, Linux, Windows) - 分布式搜索和分析引擎
429 0
|
10月前
|
存储 Linux iOS开发
Elasticsearch Enterprise 8.18 发布 - 分布式搜索和分析引擎
Elasticsearch Enterprise 8.18 (macOS, Linux, Windows) - 分布式搜索和分析引擎
387 0
|
存储 索引
Elasticsearch分布式架构
【11月更文挑战第2天】
296 1
|
SQL NoSQL MongoDB
一款基于分布式文件存储的数据库MongoDB的介绍及基本使用教程
一款基于分布式文件存储的数据库MongoDB的介绍及基本使用教程
451 0
|
7月前
|
存储 负载均衡 NoSQL
【赵渝强老师】Redis Cluster分布式集群
Redis Cluster是Redis的分布式存储解决方案,通过哈希槽(slot)实现数据分片,支持水平扩展,具备高可用性和负载均衡能力,适用于大规模数据场景。
499 2
|
7月前
|
存储 缓存 NoSQL
【📕分布式锁通关指南 12】源码剖析redisson如何利用Redis数据结构实现Semaphore和CountDownLatch
本文解析 Redisson 如何通过 Redis 实现分布式信号量(RSemaphore)与倒数闩(RCountDownLatch),利用 Lua 脚本与原子操作保障分布式环境下的同步控制,帮助开发者更好地理解其原理与应用。
453 6