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

2023-12-03 12:58

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

文章目录

  • 一、Single document APIs
    • 1.1、Index
    • 1.2、Get
    • 1.3、Delete
    • 1.4、Delete by query API
    • 1.5、Update API
  • 二、Multi-document APIs
    • 2.1、Multi get
    • 2.2、Bulk
  • 3、More

一、Single document APIs

1.1、Index

Adds a JSON document to the specified index and makes it searchable. If the document already exists, updates the document and increments its version.

  • Request
PUT /<index>/_doc/<_id>
POST /<index>/_doc/
PUT /<index>/_create/<_id>
POST /<index>/_create/<_id>
  • Example

由于高版本不再需要指定type,因此默认为_doc。

  • 使用默认id
curl -XPOST 'http://10.143.228.25:9200/student/_doc?pretty' -H "Content-Type: application/json" -d'
{"id": 1,"name": "门捷列夫","age": 20,"address": "俄罗斯","birthday": "1990-02-03"
}'
  • 指定自定义id
curl -XPOST 'http://10.143.228.25:9200/student/_doc/1?pretty' -H "Content-Type: application/json" -d'
{"id": 1,"name": "门捷列夫","age": 20,"address": "俄罗斯","birthday": "1990-02-03"
}
'

response示例:

{"_index" : "student","_type" : "_doc","_id" : "k1mNQHIBrm6ikcYVeiqR","_version" : 1,"result" : "created","_shards" : {"total" : 3,"successful" : 3,"failed" : 0},"_seq_no" : 0,"_primary_term" : 1
}

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

1.2、Get

Retrieves the specified JSON document from an index.

  • Request
GET <index>/_doc/<_id>
HEAD <index>/_doc/<_id>
GET <index>/_source/<_id>
HEAD <index>/_source/<_id>
  • 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}
}

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

1.3、Delete

Removes a JSON document from the specified index.

  • Request
DELETE /<index>/_doc/<_id>
  • Example
curl -XDELETE 'http://10.143.228.25:9200/student/_doc/1?pretty'

response示例:

{"_index" : "student","_type" : "_doc","_id" : "1","_version" : 2,"result" : "deleted","_shards" : {"total" : 3,"successful" : 3,"failed" : 0},"_seq_no" : 5,"_primary_term" : 1
}

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

1.4、Delete by query API

Removes a JSON document from the specified index.

  • Request
POST /<index>/_delete_by_query
  • Example
curl -XPOST 'http://10.143.228.25:9200/student/_delete_by_query?pretty' -H 'Content-Type: application/json' -d'
{"query": {"match": {"name": "门捷列夫"}}
}
'

response示例:

{"took" : 993,"timed_out" : false,"total" : 4,"deleted" : 4,"batches" : 1,"version_conflicts" : 0,"noops" : 0,"retries" : {"bulk" : 0,"search" : 0},"throttled_millis" : 0,"requests_per_second" : -1.0,"throttled_until_millis" : 0,"failures" : [ ]
}

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

1.5、Update API

Updates a document using the specified script.

  • Request
POST /<index>/_update/<_id>
  • Example
curl -XPOST 'http://10.143.228.25:9200/student/_update/nlmkQHIBrm6ikcYVUir7?pretty' -H 'Content-Type: application/json' -d'
{"doc": {"name": "门捷列夫","age": 22}
}
'

response示例:

{"_index" : "student","_type" : "_doc","_id" : "nlmkQHIBrm6ikcYVUir7","_version" : 2,"result" : "updated","_shards" : {"total" : 3,"successful" : 3,"failed" : 0},"_seq_no" : 2,"_primary_term" : 1
}

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

二、Multi-document APIs

2.1、Multi get

Retrieves multiple JSON documents by ID.

  • Request
GET /_mget
GET /<index>/_mget
  • Example
curl -XGET 'http://10.143.228.25:9200/student/_mget?pretty' -H 'Content-Type: application/json' -d'
{"docs": [{"_type": "_doc","_id": "nlmkQHIBrm6ikcYVUir7"}]
}
'

response示例:

{"docs" : [{"_index" : "student","_type" : "_doc","_id" : "nlmkQHIBrm6ikcYVUir7","_version" : 2,"_seq_no" : 2,"_primary_term" : 1,"found" : true,"_source" : {"id" : 1,"name" : "门捷列夫","age" : 22,"address" : "俄罗斯","birthday" : "1990-02-03"}}]
}

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

2.2、Bulk

Performs multiple indexing or delete operations in a single API call. This reduces overhead and can greatly increase indexing speed.

  • Request
POST /_bulk
POST /<index>/_bulk
  • Example
curl -X POST "localhost:9200/_bulk?pretty" -H 'Content-Type: application/json' -d'
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_id" : "2" } }
{ "create" : { "_index" : "test", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }
'

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

3、More

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

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

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



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

相关文章

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. 依赖