Elasticsearch(二)——Rest API
时间:2022-06-22
本文章向大家介绍Elasticsearch(二)——Rest API,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
RESTful 接口 URL 的格式是: http://cluster的地址: 9200/<index>I<type>I [<id>] 其中,index, type 是必须提供的( index 可以理解为数据库;type 理解为数据表); id 是可选的(相当于数据库表中记 录的主键是唯一的。如果不提供, Elasticsearch 会向动生成。增 、删、改,查分别对应 HTTP 请求的 PUT 、DELETE、POST、GET方法。
Kibana DevTools
image.png
创建数据
创建index是province,type是citys ,当前id为1的document数据
PUT /province/citys/1
{
"name":"北京",
"code":"010"
}
返回结果
{
"_index": "province",
"_type": "citys",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
es自动生成id创建数据,自动生成的id 长度是20个字符,采用GUID算法,保证分布式系统,不同节点同一个时间点创建_id`生成不可能发生冲突
POST /province/citys/
{
"name":"上海",
"code":"021"
}
返回结果
{
"_index": "province",
"_type": "citys",
"_id": "VV6_G2YBajYJa8_UqerL",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
修改数据
只想修改id唯一的field是name的值
POST /province/citys/1/_update
{
"doc":{
"name":"广州"
}
}
删除字段
curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
"script" : "ctx._source.remove("name_of_field")"
}
替换数据
PUT /province/citys/1
{
"name":"广州"
}
这个行为是替换数据,和修改数据是两种不同形式的数据更新
删除数据
DELETE /province/citys/1
返回结果
{
"_index": "province",
"_type": "citys",
"_id": "1",
"_version": 7,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 6,
"_primary_term": 1
}
curl命令行
创建索引
$ curl -XPUT "http://127.0.0.1:9200/school"
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "school"
}
删除索引
curl -XDELETE http://localhost:9200/school
{"acknowledged":true}
插入数据
指定id
$ curl -H "Content-Type:application/json" -XPUT http://localhost:9200/school/student/1 -d '{"name":"xiaoming","age":20}'
{
"_index": "school",
"_type": "student",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
不指定id
curl -H "Content-Type:application/json" -XPOST http://localhost:9200/school/student/ -d '{"name":"xiaowang","age":18}'
{
"_index": "school",
"_type": "student",
"_id": "kYeii2UBT-VMLOAXEoWx",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
修改数据
curl -H "Content-Type:application/json" -XPOST http://localhost:9200/school/student/1/_update -d '{"doc":{"name":"xiaohong","age":21}}'
{
"_index": "school",
"_type": "student",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
查询数据
curl -XGET http://localhost:9200/school/student/1
{
"_index": "school",
"_type": "student",
"_id": "1",
"_version": 2,
"found": true,
"_source": {
"name": "xiaohong",
"age": 21
}
}
删除
删除数据
curl -XDELETE http://localhost:9200/school/student/1
{
"_index": "school",
"_type": "student",
"_id": "1",
"_version": 3,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}
删除Type
curl -H "Content-Type:application/json" -XPOST http://localhost:9200/school/student/_delete_by_query -d '{"query":{"match_all":{}}}'
{
"took": 40,
"timed_out": false,
"total": 1,
"deleted": 1,
"batches": 1,
"version_conflicts": 0,
"noops": 0,
"retries": {
"bulk": 0,
"search": 0
},
"throttled_millis": 0,
"requests_per_second": -1,
"throttled_until_millis": 0,
"failures": []
}
bulk批量导入
POST _bulk
{ "index" : { "_index" : "test", "_type" : "_doc", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_type" : "_doc", "_id" : "2" } }
{ "create" : { "_index" : "test", "_type" : "_doc", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_type" : "_doc", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }
导入结果
{
"took": 176,
"errors": false,
"items": [
{
"index": {
"_index": "test",
"_type": "_doc",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1,
"status": 201
}
},
{
"delete": {
"_index": "test",
"_type": "_doc",
"_id": "2",
"_version": 1,
"result": "not_found",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1,
"status": 404
}
},
{
"create": {
"_index": "test",
"_type": "_doc",
"_id": "3",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1,
"status": 201
}
},
{
"update": {
"_index": "test",
"_type": "_doc",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1,
"status": 200
}
}
]
}
reindex数据迁移
POST _reindex
{
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter"
}
}
批量导入数据
POST /blog/paper/_bulk
{"index":{"_id":1}}
{"pID":"7ec0e0e5","uID":1,"publish":false,"date":"2018-01-02"}
{"index":{"_id":2}}
{"pID":"69590fe7","uID":2,"publish":true,"date":"2018-10-11"}
{"index":{"_id":3}}
{"pID":"aec30fa2","uID":1,"publish":false,"date":"2018-06-25"}
{"index":{"_id":4}}
{"pID":"a8733527","uID":3,"publish":false,"date":"2017-01-16"}
查看es状态
查看es的健康状态
GET _cat/health?v
查看es的index
GET _cat/indices
- ASP.NET MVC是如何运行的(3): Controller的激活
- ASP.NET MVC是如何运行的[2]: URL路由
- 一个简单的小程序演示Unity的三种依赖注入方式
- 在Entity Framework中使用存储过程(三):逻辑删除的实现与自增长列值返回
- 在Entity Framework中使用存储过程(四):如何为Delete存储过程参数赋上Current值?
- ASP.NET MVC是如何运行的(4): Action的执行
- ASP.NET MVC是如何运行的[1]: 建立在“伪”MVC框架上的Web应用
- 在Entity Framework中使用存储过程(五):如何通过存储过程维护多对多关系?
- ASP.NET MVC下基于异常处理的完整解决方案
- 不到40行代码构建正则表达式引擎
- 随便写一篇文章
- Java 10新特性解密
- Android组件化框架项目详解
- ScheduledThreadPoolExecutor原理探究
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- python实现线性回归之岭回归
- 操作系统实验之存储管理
- MySQL EXPLAIN 的使用
- mybatis文件映射之关联查询初探(一)
- python实现线性回归之弹性网回归
- 【原创】python倒排索引之查找包含某主题或单词的文件
- python实现逻辑回归
- Linux文件管理参考
- CloudBase Framework丨第一个 Deno 部署工具是如何打造的?
- 关于null通过+" ",String.ValueOf转换为字符串的问题!!!
- Java实现尺取法
- 【自然语言处理】利用朴素贝叶斯进行新闻分类(自己处理数据)
- mybatis文件映射之select操作返回Map
- shm进程间通信失败了!!!
- Vue3.0 beta源码学习笔记(二)