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

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 分布式系列教程(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
        ]
      }
    ]
  }
}



相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
目录
相关文章
|
2月前
|
存储 固态存储 Java
Elasticsearch中查询性能优化
Elasticsearch中查询性能优化
196 0
|
3月前
Elasticsearch之RestClient查询文档
Elasticsearch之RestClient查询文档
139 1
|
3月前
|
缓存 JSON API
Elasticsearch-05Elasticsearch之查询与过滤
Elasticsearch-05Elasticsearch之查询与过滤
165 0
|
28天前
|
canal 消息中间件 关系型数据库
【分布式技术专题】「分布式技术架构」MySQL数据同步到Elasticsearch之N种方案解析,实现高效数据同步
【分布式技术专题】「分布式技术架构」MySQL数据同步到Elasticsearch之N种方案解析,实现高效数据同步
78 0
|
1月前
|
算法 数据处理 异构计算
CatBoost高级教程:分布式训练与大规模数据处理
CatBoost高级教程:分布式训练与大规模数据处理【2月更文挑战第15天】
244 14
|
2月前
|
缓存 算法 索引
【Elasticsearch专栏 07】深入探索:Elasticsearch的倒排索引如何进行模糊查询和通配符查询
Elasticsearch的倒排索引支持模糊查询和通配符查询,通过特定的算法和数据结构,能够实现对关键词的模糊匹配和通配符匹配。这两种查询类型提供了更灵活的搜索功能,但可能影响查询性能,需结合优化策略使用。
|
2月前
|
缓存 自然语言处理 数据挖掘
一篇文章让你学会Elasticsearch中的查询
一篇文章让你学会Elasticsearch中的查询
137312 118
|
2月前
|
测试技术 定位技术 API
万字长文:一文彻底搞懂Elasticsearch中Geo数据类型查询、聚合、排序
万字长文:一文彻底搞懂Elasticsearch中Geo数据类型查询、聚合、排序
94614 140
|
2月前
|
JSON 前端开发 API
【Elasticsearch】搜索结果处理和RestClient查询文档
【Elasticsearch】搜索结果处理和RestClient查询文档
338 0
|
2月前
|
JSON 自然语言处理 算法
【Elasticsearch】DSL查询文档
【Elasticsearch】DSL查询文档
315 0

热门文章

最新文章