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

相关文章

Python正则表达式匹配和替换的操作指南

《Python正则表达式匹配和替换的操作指南》正则表达式是处理文本的强大工具,Python通过re模块提供了完整的正则表达式功能,本文将通过代码示例详细介绍Python中的正则匹配和替换操作,需要的朋... 目录基础语法导入re模块基本元字符常用匹配方法1. re.match() - 从字符串开头匹配2.

MyBatis分页查询实战案例完整流程

《MyBatis分页查询实战案例完整流程》MyBatis是一个强大的Java持久层框架,支持自定义SQL和高级映射,本案例以员工工资信息管理为例,详细讲解如何在IDEA中使用MyBatis结合Page... 目录1. MyBATis框架简介2. 分页查询原理与应用场景2.1 分页查询的基本原理2.1.1 分

JavaScript中的高级调试方法全攻略指南

《JavaScript中的高级调试方法全攻略指南》什么是高级JavaScript调试技巧,它比console.log有何优势,如何使用断点调试定位问题,通过本文,我们将深入解答这些问题,带您从理论到实... 目录观点与案例结合观点1观点2观点3观点4观点5高级调试技巧详解实战案例断点调试:定位变量错误性能分

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java

Python实现精确小数计算的完全指南

《Python实现精确小数计算的完全指南》在金融计算、科学实验和工程领域,浮点数精度问题一直是开发者面临的重大挑战,本文将深入解析Python精确小数计算技术体系,感兴趣的小伙伴可以了解一下... 目录引言:小数精度问题的核心挑战一、浮点数精度问题分析1.1 浮点数精度陷阱1.2 浮点数误差来源二、基础解决

Java实现在Word文档中添加文本水印和图片水印的操作指南

《Java实现在Word文档中添加文本水印和图片水印的操作指南》在当今数字时代,文档的自动化处理与安全防护变得尤为重要,无论是为了保护版权、推广品牌,还是为了在文档中加入特定的标识,为Word文档添加... 目录引言Spire.Doc for Java:高效Word文档处理的利器代码实战:使用Java为Wo

SpringBoot 多环境开发实战(从配置、管理与控制)

《SpringBoot多环境开发实战(从配置、管理与控制)》本文详解SpringBoot多环境配置,涵盖单文件YAML、多文件模式、MavenProfile分组及激活策略,通过优先级控制灵活切换环境... 目录一、多环境开发基础(单文件 YAML 版)(一)配置原理与优势(二)实操示例二、多环境开发多文件版

Three.js构建一个 3D 商品展示空间完整实战项目

《Three.js构建一个3D商品展示空间完整实战项目》Three.js是一个强大的JavaScript库,专用于在Web浏览器中创建3D图形,:本文主要介绍Three.js构建一个3D商品展... 目录引言项目核心技术1. 项目架构与资源组织2. 多模型切换、交互热点绑定3. 移动端适配与帧率优化4. 可

从入门到精通详解Python虚拟环境完全指南

《从入门到精通详解Python虚拟环境完全指南》Python虚拟环境是一个独立的Python运行环境,它允许你为不同的项目创建隔离的Python环境,下面小编就来和大家详细介绍一下吧... 目录什么是python虚拟环境一、使用venv创建和管理虚拟环境1.1 创建虚拟环境1.2 激活虚拟环境1.3 验证虚