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

相关文章

SpringBoot整合OpenFeign的完整指南

《SpringBoot整合OpenFeign的完整指南》OpenFeign是由Netflix开发的一个声明式Web服务客户端,它使得编写HTTP客户端变得更加简单,本文为大家介绍了SpringBoot... 目录什么是OpenFeign环境准备创建 Spring Boot 项目添加依赖启用 OpenFeig

SpringBoot请求参数接收控制指南分享

《SpringBoot请求参数接收控制指南分享》:本文主要介绍SpringBoot请求参数接收控制指南,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Spring Boot 请求参数接收控制指南1. 概述2. 有注解时参数接收方式对比3. 无注解时接收参数默认位置

CentOS7更改默认SSH端口与配置指南

《CentOS7更改默认SSH端口与配置指南》SSH是Linux服务器远程管理的核心工具,其默认监听端口为22,由于端口22众所周知,这也使得服务器容易受到自动化扫描和暴力破解攻击,本文将系统性地介绍... 目录引言为什么要更改 SSH 默认端口?步骤详解:如何更改 Centos 7 的 SSH 默认端口1

SpringBoot多数据源配置完整指南

《SpringBoot多数据源配置完整指南》在复杂的企业应用中,经常需要连接多个数据库,SpringBoot提供了灵活的多数据源配置方式,以下是详细的实现方案,需要的朋友可以参考下... 目录一、基础多数据源配置1. 添加依赖2. 配置多个数据源3. 配置数据源Bean二、JPA多数据源配置1. 配置主数据

python中各种常见文件的读写操作与类型转换详细指南

《python中各种常见文件的读写操作与类型转换详细指南》这篇文章主要为大家详细介绍了python中各种常见文件(txt,xls,csv,sql,二进制文件)的读写操作与类型转换,感兴趣的小伙伴可以跟... 目录1.文件txt读写标准用法1.1写入文件1.2读取文件2. 二进制文件读取3. 大文件读取3.1

SpringBoot中配置Redis连接池的完整指南

《SpringBoot中配置Redis连接池的完整指南》这篇文章主要为大家详细介绍了SpringBoot中配置Redis连接池的完整指南,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以... 目录一、添加依赖二、配置 Redis 连接池三、测试 Redis 操作四、完整示例代码(一)pom.

Linux内核参数配置与验证详细指南

《Linux内核参数配置与验证详细指南》在Linux系统运维和性能优化中,内核参数(sysctl)的配置至关重要,本文主要来聊聊如何配置与验证这些Linux内核参数,希望对大家有一定的帮助... 目录1. 引言2. 内核参数的作用3. 如何设置内核参数3.1 临时设置(重启失效)3.2 永久设置(重启仍生效

Python列表去重的4种核心方法与实战指南详解

《Python列表去重的4种核心方法与实战指南详解》在Python开发中,处理列表数据时经常需要去除重复元素,本文将详细介绍4种最实用的列表去重方法,有需要的小伙伴可以根据自己的需要进行选择... 目录方法1:集合(set)去重法(最快速)方法2:顺序遍历法(保持顺序)方法3:副本删除法(原地修改)方法4:

在Spring Boot中浅尝内存泄漏的实战记录

《在SpringBoot中浅尝内存泄漏的实战记录》本文给大家分享在SpringBoot中浅尝内存泄漏的实战记录,结合实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录使用静态集合持有对象引用,阻止GC回收关键点:可执行代码:验证:1,运行程序(启动时添加JVM参数限制堆大小):2,访问 htt

PyInstaller打包selenium-wire过程中常见问题和解决指南

《PyInstaller打包selenium-wire过程中常见问题和解决指南》常用的打包工具PyInstaller能将Python项目打包成单个可执行文件,但也会因为兼容性问题和路径管理而出现各种运... 目录前言1. 背景2. 可能遇到的问题概述3. PyInstaller 打包步骤及参数配置4. 依赖