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

相关文章

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

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

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

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

从原理到实战深入理解Java 断言assert

《从原理到实战深入理解Java断言assert》本文深入解析Java断言机制,涵盖语法、工作原理、启用方式及与异常的区别,推荐用于开发阶段的条件检查与状态验证,并强调生产环境应使用参数验证工具类替代... 目录深入理解 Java 断言(assert):从原理到实战引言:为什么需要断言?一、断言基础1.1 语

Java MQTT实战应用

《JavaMQTT实战应用》本文详解MQTT协议,涵盖其发布/订阅机制、低功耗高效特性、三种服务质量等级(QoS0/1/2),以及客户端、代理、主题的核心概念,最后提供Linux部署教程、Sprin... 目录一、MQTT协议二、MQTT优点三、三种服务质量等级四、客户端、代理、主题1. 客户端(Clien

Linux中SSH服务配置的全面指南

《Linux中SSH服务配置的全面指南》作为网络安全工程师,SSH(SecureShell)服务的安全配置是我们日常工作中不可忽视的重要环节,本文将从基础配置到高级安全加固,全面解析SSH服务的各项参... 目录概述基础配置详解端口与监听设置主机密钥配置认证机制强化禁用密码认证禁止root直接登录实现双因素

在Spring Boot中集成RabbitMQ的实战记录

《在SpringBoot中集成RabbitMQ的实战记录》本文介绍SpringBoot集成RabbitMQ的步骤,涵盖配置连接、消息发送与接收,并对比两种定义Exchange与队列的方式:手动声明(... 目录前言准备工作1. 安装 RabbitMQ2. 消息发送者(Producer)配置1. 创建 Spr

深度解析Spring Boot拦截器Interceptor与过滤器Filter的区别与实战指南

《深度解析SpringBoot拦截器Interceptor与过滤器Filter的区别与实战指南》本文深度解析SpringBoot中拦截器与过滤器的区别,涵盖执行顺序、依赖关系、异常处理等核心差异,并... 目录Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现

MySQL追踪数据库表更新操作来源的全面指南

《MySQL追踪数据库表更新操作来源的全面指南》本文将以一个具体问题为例,如何监测哪个IP来源对数据库表statistics_test进行了UPDATE操作,文内探讨了多种方法,并提供了详细的代码... 目录引言1. 为什么需要监控数据库更新操作2. 方法1:启用数据库审计日志(1)mysql/mariad

深度解析Spring AOP @Aspect 原理、实战与最佳实践教程

《深度解析SpringAOP@Aspect原理、实战与最佳实践教程》文章系统讲解了SpringAOP核心概念、实现方式及原理,涵盖横切关注点分离、代理机制(JDK/CGLIB)、切入点类型、性能... 目录1. @ASPect 核心概念1.1 AOP 编程范式1.2 @Aspect 关键特性2. 完整代码实

MySQL中的索引结构和分类实战案例详解

《MySQL中的索引结构和分类实战案例详解》本文详解MySQL索引结构与分类,涵盖B树、B+树、哈希及全文索引,分析其原理与优劣势,并结合实战案例探讨创建、管理及优化技巧,助力提升查询性能,感兴趣的朋... 目录一、索引概述1.1 索引的定义与作用1.2 索引的基本原理二、索引结构详解2.1 B树索引2.2