ElasticSearch7.3.2-Rest实战指南-Search APIs

2023-12-03 12:58

本文主要是介绍ElasticSearch7.3.2-Rest实战指南-Search APIs,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 1、Search APIs
    • 1.1、 Search
      • Doc Value Fields
      • From / Size
      • Sort
    • 1.2、Request Body Search
    • 1.4、Search Shards API
  • 3、More
  • 4、elasticsearch-header插件

1、Search APIs

Most search APIs are multi-index, with the exception of the Explain API endpoints.

1.1、 Search

Returns search hits that match the query defined in the request.

  • Request
GET /<index>/_search
GET /_search
POST /<index>/_search
POST /_search
  • Example

  • query parameter

curl -XGET 'http://10.143.228.25:9200/student/_search?q=age:20&pretty'# 查询指定返回字段
curl -XGET 'http://10.143.228.25:9200/student/_search?_source=name,age&pretty'
{"took" : 7,"timed_out" : false,"_shards" : {"total" : 3,"successful" : 3,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 2,"relation" : "eq"},"max_score" : 1.0,"hits" : [{"_index" : "student","_type" : "_doc","_id" : "oFmkQHIBrm6ikcYVWSpb","_score" : 1.0,"_source" : {"id" : 1,"name" : "门捷列夫","age" : 20,"address" : "俄罗斯","birthday" : "1990-02-03"}},{"_index" : "student","_type" : "_doc","_id" : "oVmkQHIBrm6ikcYVXCpE","_score" : 1.0,"_source" : {"id" : 1,"name" : "门捷列夫","age" : 20,"address" : "俄罗斯","birthday" : "1990-02-03"}}]}
}
  • request body
curl -XPOST 'http://10.143.228.25:9200/student/_search?pretty' -H "Content-Type: application/json" -d'
{"query" : {"term" : { "age" : "20" }}
}
'

response示例:

{"took" : 10,"timed_out" : false,"_shards" : {"total" : 3,"successful" : 3,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 4,"relation" : "eq"},"max_score" : 1.0,"hits" : [{"_index" : "student","_type" : "_doc","_id" : "oFmkQHIBrm6ikcYVWSpb","_score" : 1.0,"_source" : {"id" : 1,"name" : "门捷列夫","age" : 20,"address" : "俄罗斯","birthday" : "1990-02-03"}},{"_index" : "student","_type" : "_doc","_id" : "oVmkQHIBrm6ikcYVXCpE","_score" : 1.0,"_source" : {"id" : 1,"name" : "门捷列夫","age" : 20,"address" : "俄罗斯","birthday" : "1990-02-03"}}]}
}

Doc Value Fields

curl -XPOST 'http://10.143.228.25:9200/student/_search?pretty' -H "Content-Type: application/json" -d'
{"query" : {"term" : { "age" : "20" }},"_source":["name","age"]
}'

From / Size

curl -XPOST 'http://10.143.228.25:9200/student/_search?pretty' -H "Content-Type: application/json" -d'
{"from" : 0, "size" : 10,"query" : {"term" : { "age" : "20" }},"_source":["name","age"]
}'

Sort

curl -XPOST 'http://10.143.228.25:9200/student/_search?pretty' -H "Content-Type: application/json" -d'
{"from" : 0, "size" : 10,"query" : {"term" : { "age" : "20" }},"_source":["name","age"],"sort":[{"age":"asc"}]
}'

更多阅读:https://www.elastic.co/guide/en/elasticsearch/reference/7.x/docs-index_.html

1.2、Request Body Search

Specifies search criteria as request body parameters.

  • Request
GET /<index>/_search
{"query": {<parameters>}
}
  • Example
curl -XGET 'http://10.143.228.25:9200/student/_doc/1?pretty'

response示例:

{"_index" : "student","_type" : "_doc","_id" : "1","_version" : 1,"_seq_no" : 4,"_primary_term" : 1,"found" : true,"_source" : {"id" : 1,"name" : "门捷列夫","age" : 20,"address" : "俄罗斯","birthday" : "1990-02-03"}
}
  • 返回指定字段
curl -XGET 'http://10.143.228.25:9200/student/_doc/1?pretty&_source=name,age'

response示例:

{"_index" : "student","_type" : "_doc","_id" : "1","_version" : 1,"_seq_no" : 4,"_primary_term" : 1,"found" : true,"_source" : {"name" : "门捷列夫","age" : 20}
}
  • 使用bool(must、must_not、should)

filter可以实现对结果的过滤

curl -X GET "10.143.228.25:9200/student/_search?pretty" -H 'Content-Type: application/json' -d'
{"query": {"bool": {"must": [{ "match": { "age": 20 } }],"must_not": [{ "match": { "address": "美国" } }],"should": [{ "match": { "age": 20 } }],"filter": {"range": {"age": {"gte": 20,"lte": 30}}}}}
}
'>更多阅读:https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search-request-body.html## 1.3、Count API>Gets the number of matches for a search query.- Request```shell
GET /<index>/_count
  • Example

1、基于URI形式

curl -XGET 'http://10.143.228.25:9200/student/_count?q=age:20&pretty'

response示例:

{"count" : 4,"_shards" : {"total" : 3,"successful" : 3,"skipped" : 0,"failed" : 0}
}

2、基于request body形式

curl -XGET 'http://10.143.228.25:9200/student/_count?pretty' -H 'Content-Type: application/json' -d'
{"query" : {"term" : { "age" : 20 }}
}'

response示例:

{"count" : 4,"_shards" : {"total" : 3,"successful" : 3,"skipped" : 0,"failed" : 0}
}

更多阅读:https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search-count.html

1.4、Search Shards API

Returns the indices and shards that a search request would be executed against.

  • Request
GET /<index>/_search_shards
  • Example
curl -XGET 'http://10.143.228.25:9200/student/_search_shards?pretty'

response示例:

{                                                                                                                                                       "nodes" : {                                                                                                                                           "fkdv_0G4SCW5YLqxkWL1qg" : {                                                                                                                        "name" : "node2",                                                                                                                                 "ephemeral_id" : "MBLiX3TKRJihyd7aiGLjnA",                                                                                                        "transport_address" : "10.143.228.26:9300",                                                                                                       "attributes" : {                                                                                                                                  "ml.machine_memory" : "8350584832",                                                                                                             "ml.max_open_jobs" : "20",                                                                                                                      "xpack.installed" : "true"                                                                                                                      }                                                                                                                                                 },                                                                                                                                                  "E6pmMAUdShurP50uAV3kIA" : {                                                                                                                        "name" : "node3",                                                                                                                                 "ephemeral_id" : "7eSlOGxnSw65qIEGktBaDQ",                                                                                                        "transport_address" : "10.143.228.198:9300",                                                                                                      "attributes" : {                                                                                                                                  "ml.machine_memory" : "8350584832",                                                                                                             "ml.max_open_jobs" : "20",                                                                                                                      "xpack.installed" : "true"                                                                                                                      }                                                                                                                                                 },                                                                                                                                                  "XiTKce8fST2vhReZvE-1dA" : {                                                                                                                        "name" : "node1",                                                                                                                                 "ephemeral_id" : "rQYF0SGZRWmNe23mXyKWEw",                                                                                                        "transport_address" : "10.143.228.25:9300",                                                                                                       "attributes" : {                                                                                                                                  "ml.machine_memory" : "8350576640",                                                                                                             "xpack.installed" : "true",                                                                                                                     "ml.max_open_jobs" : "20"                                                                                                                       }                                                                                                                                                 }                                                                                                                                                   },                                                                                                                                                    "indices" : {                                                                                                                                         "student" : { }                                                                                                                                     },                                                                                                                                                    "shards" : [                                                                                                                                          [                                                                                                                                                   {                                                                                                                                                 "state" : "STARTED",                                                                                                                            "primary" : false,                                                                                                                              "node" : "E6pmMAUdShurP50uAV3kIA",                                                                                                              "relocating_node" : null,                                                                                                                       "shard" : 0,                                                                                                                                    "index" : "student",                                                                                                                            "allocation_id" : {                                                                                                                             "id" : "3iTj8tTNSdiDjaUvBgb3gA"                                                                                                               }                                                                                                                                               },                                                                                                                                                {                                                                                                                                                 "state" : "STARTED","primary" : false,"node" : "XiTKce8fST2vhReZvE-1dA","relocating_node" : null,"shard" : 0,"index" : "student","allocation_id" : {"id" : "rOYuBhe7ToqugfDwBifSEQ"}},{"state" : "STARTED","primary" : true,"node" : "fkdv_0G4SCW5YLqxkWL1qg","relocating_node" : null,"shard" : 0,"index" : "student","allocation_id" : {"id" : "9jSRbREKS9eCFDrEKrOeOQ"}}],[{"state" : "STARTED","primary" : false,"node" : "fkdv_0G4SCW5YLqxkWL1qg","relocating_node" : null,"shard" : 1,"index" : "student","allocation_id" : {"id" : "erg_YsbmQPWQFrXAm_icpQ"}},{"state" : "STARTED","primary" : true,"node" : "E6pmMAUdShurP50uAV3kIA","relocating_node" : null,"shard" : 1,"index" : "student","allocation_id" : {"id" : "Hvd9J2IcTmOPRESGtruAAQ"}},{"state" : "STARTED","primary" : false,"node" : "XiTKce8fST2vhReZvE-1dA","relocating_node" : null,"shard" : 1,"index" : "student","allocation_id" : {"id" : "Wgl0mc6zQmG4qIwvLJMS8Q"}}],[{"state" : "STARTED","primary" : false,"node" : "fkdv_0G4SCW5YLqxkWL1qg","relocating_node" : null,"shard" : 2,"index" : "student","allocation_id" : {"id" : "T9dZf0nATfCKlN0EbDca6w"}},{"state" : "STARTED","primary" : false,"node" : "E6pmMAUdShurP50uAV3kIA","relocating_node" : null,"shard" : 2,"index" : "student","allocation_id" : {"id" : "CRmJU1_2RBegt8mPZXQt-A"}},{"state" : "STARTED","primary" : true,"node" : "XiTKce8fST2vhReZvE-1dA","relocating_node" : null,"shard" : 2,"index" : "student","allocation_id" : {"id" : "mTGFJhjmS0yIbsioZRxnsg"}}]]
}

更多阅读:https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search-shards.html

3、More

https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search.html

4、elasticsearch-header插件

elasticsearch-header插件就像mysql之navicat或者sqlYog,一个客户端插件,如上的一些curl命令可以通过界面操作,提高易用性。

下载地址:https://gitee.com/suze/elasticsearch-head

安装步骤详见源码的readme即可。

部分截图:
数据浏览
基本查询
概览

下面的是我的公众号二维码图片,欢迎关注。
秋夜无霜

这篇关于ElasticSearch7.3.2-Rest实战指南-Search APIs的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/449532

相关文章

PostgreSQL中rank()窗口函数实用指南与示例

《PostgreSQL中rank()窗口函数实用指南与示例》在数据分析和数据库管理中,经常需要对数据进行排名操作,PostgreSQL提供了强大的窗口函数rank(),可以方便地对结果集中的行进行排名... 目录一、rank()函数简介二、基础示例:部门内员工薪资排名示例数据排名查询三、高级应用示例1. 每

SpringBoot结合Docker进行容器化处理指南

《SpringBoot结合Docker进行容器化处理指南》在当今快速发展的软件工程领域,SpringBoot和Docker已经成为现代Java开发者的必备工具,本文将深入讲解如何将一个SpringBo... 目录前言一、为什么选择 Spring Bootjavascript + docker1. 快速部署与

MySQL 多列 IN 查询之语法、性能与实战技巧(最新整理)

《MySQL多列IN查询之语法、性能与实战技巧(最新整理)》本文详解MySQL多列IN查询,对比传统OR写法,强调其简洁高效,适合批量匹配复合键,通过联合索引、分批次优化提升性能,兼容多种数据库... 目录一、基础语法:多列 IN 的两种写法1. 直接值列表2. 子查询二、对比传统 OR 的写法三、性能分析

创建Java keystore文件的完整指南及详细步骤

《创建Javakeystore文件的完整指南及详细步骤》本文详解Java中keystore的创建与配置,涵盖私钥管理、自签名与CA证书生成、SSL/TLS应用,强调安全存储及验证机制,确保通信加密和... 目录1. 秘密键(私钥)的理解与管理私钥的定义与重要性私钥的管理策略私钥的生成与存储2. 证书的创建与

Python办公自动化实战之打造智能邮件发送工具

《Python办公自动化实战之打造智能邮件发送工具》在数字化办公场景中,邮件自动化是提升工作效率的关键技能,本文将演示如何使用Python的smtplib和email库构建一个支持图文混排,多附件,多... 目录前言一、基础配置:搭建邮件发送框架1.1 邮箱服务准备1.2 核心库导入1.3 基础发送函数二、

Python包管理工具pip的升级指南

《Python包管理工具pip的升级指南》本文全面探讨Python包管理工具pip的升级策略,从基础升级方法到高级技巧,涵盖不同操作系统环境下的最佳实践,我们将深入分析pip的工作原理,介绍多种升级方... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

PowerShell中15个提升运维效率关键命令实战指南

《PowerShell中15个提升运维效率关键命令实战指南》作为网络安全专业人员的必备技能,PowerShell在系统管理、日志分析、威胁检测和自动化响应方面展现出强大能力,下面我们就来看看15个提升... 目录一、PowerShell在网络安全中的战略价值二、网络安全关键场景命令实战1. 系统安全基线核查

Java操作Word文档的全面指南

《Java操作Word文档的全面指南》在Java开发中,操作Word文档是常见的业务需求,广泛应用于合同生成、报表输出、通知发布、法律文书生成、病历模板填写等场景,本文将全面介绍Java操作Word文... 目录简介段落页头与页脚页码表格图片批注文本框目录图表简介Word编程最重要的类是org.apach

Python设置Cookie永不超时的详细指南

《Python设置Cookie永不超时的详细指南》Cookie是一种存储在用户浏览器中的小型数据片段,用于记录用户的登录状态、偏好设置等信息,下面小编就来和大家详细讲讲Python如何设置Cookie... 目录一、Cookie的作用与重要性二、Cookie过期的原因三、实现Cookie永不超时的方法(一)

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.