【ElasticSearch】01 CRUD操作
1、资料:
ES官网最新版本下载地址:
https://www.elastic.co/cn/downloads/elasticsearch
历史版本下载:
https://www.elastic.co/cn/downloads/past-releases/elasticsearch-7-16-0
https://www.elastic.co/cn/downloads/past-releases/elasticsearch-7-8-0
2、参考教程:
# 尚硅谷 7.8.X
https://www.bilibili.com/video/BV1hh411D7sb # 狂神 7.6.x
https://www.bilibili.com/video/BV17a4y1x7zq # 尚学堂 7.4.2
https://www.bilibili.com/video/BV1x54y15775 # 黑马 7.4.0
https://www.bilibili.com/video/BV1Sy4y1G7LL
3、安装:
入门暂时不涉及Linux及集群部署问题,快速上手API
直接在官网上先下载windows平台的压缩包,解压即可
elasticsearch-7.16.3-windows-x86_64.zip
4、启动ElasticSearch:
E:\elasticsearch-7.16.3\bin\elasticsearch.bat
双击脚本文件启动ES
访问地址:
http://localhost:9200/
展示下面信息说明运行正常
{
"name" : "DESKTOP-VA4SKMT",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "KH3taZdhQ5KqYfMJQoyZDQ",
"version" : {
"number" : "7.16.3",
"build_flavor" : "default",
"build_type" : "zip",
"build_hash" : "4e6e4eab2297e949ec994e688dad46290d018022",
"build_date" : "2022-01-06T23:43:02.825887787Z",
"build_snapshot" : false,
"lucene_version" : "8.10.1",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
5、索引操作
一、CREATE INDEX 创建索引:
# PUT
http://localhost:9200/索引名称
# 案例
http://localhost:9200/student
索引创建成功:
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "student"
}
索引创建失败,索引已经存在
{
"error": {
"root_cause": [
{
"type": "resource_already_exists_exception",
"reason": "index [student/FXObUphUQP65KZEJQk7TgQ] already exists",
"index_uuid": "FXObUphUQP65KZEJQk7TgQ",
"index": "student"
}
],
"type": "resource_already_exists_exception",
"reason": "index [student/FXObUphUQP65KZEJQk7TgQ] already exists",
"index_uuid": "FXObUphUQP65KZEJQk7TgQ",
"index": "student"
},
"status": 400
}
二、DELETE INDEX 删除索引
# DELETE
http://localhost:9200/索引名称
# 案例
http://localhost:9200/student
索引删除成功:
{
"acknowledged": true
}
索引删除失败,不存在该索引:
{
"error": {
"root_cause": [
{
"type": "index_not_found_exception",
"reason": "no such index [student]",
"resource.type": "index_or_alias",
"resource.id": "student",
"index_uuid": "_na_",
"index": "student"
}
],
"type": "index_not_found_exception",
"reason": "no such index [student]",
"resource.type": "index_or_alias",
"resource.id": "student",
"index_uuid": "_na_",
"index": "student"
},
"status": 404
}
三、GET INDEX INFO 查看指定索引信息
# GET
http://localhost:9200/索引名称
响应该索引的所有信息
{
"student": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_content"
}
}
},
"number_of_shards": "1",
"provided_name": "student",
"creation_date": "1643182895035",
"number_of_replicas": "1",
"uuid": "KmLC3oT9Te2lUmOQVf8iXw",
"version": {
"created": "7160399"
}
}
}
}
}
如果索引不存在则报错:
{
"error": {
"root_cause": [
{
"type": "index_not_found_exception",
"reason": "no such index [student3]",
"resource.type": "index_or_alias",
"resource.id": "student3",
"index_uuid": "_na_",
"index": "student3"
}
],
"type": "index_not_found_exception",
"reason": "no such index [student3]",
"resource.type": "index_or_alias",
"resource.id": "student3",
"index_uuid": "_na_",
"index": "student3"
},
"status": 404
}
四、INDEX LIST 查看所有索引
# GET
http://localhost:9200/_cat/indices?v
v参数表示展示表头,下面展示了所有的索引
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open .geoip_databases SHVb70kCSJqFhkKIUNyV4g 1 0 42 37 43mb 43mb
yellow open student KmLC3oT9Te2lUmOQVf8iXw 1 1 0 0 226b 226b
yellow open student1 6qudU33pT5C5-6CDdeFRbw 1 1 0 0 226b 226b
yellow open employee3 Fq3VTWk3Q0eT5iEbYsZXIA 1 1 0 0 226b 226b
yellow open employee IihIiOzbR8-44mL8qeiDpw 1 1 2 0 17.7kb 17.7kb
yellow open employee2 vIHhlBnDRYuWw-B3OYB5cA 1 1 0 0 226b 226b
6、文档操作
一、创建文档 CREATE DOC
# POST
http://localhost:9200/索引名/_doc # JSON 参数
{ "name" : "Cloud9", "gender" : "male", "age" : 24 }
案例:
文档的主键是ES自动生成的,要查询该文档则需要通过id或者文档的属性作为条件
# POST
http://localhost:9200/student/_doc # 响应
{
"_index": "student",
"_type": "_doc",
"_id": "ui1dlX4BrIYsUbCa81Wh",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
二、 CREATE & UPDATE DOC 创建/修改文档
创建或者修改时,指定文档的ID(自定义)
# POST / PUT 两种请求都支持
http://localhost:9200/索引名/_doc/文档ID # JSON参数
{ "name" : "Cloud8", "gender" : "male", "age" : 24 }
案例:
# POST
http://localhost:9200/student/_doc/1001 # JSON参数
{ "name" : "Cloud9", "gender" : "male", "age" : 24 } # 首次创建
{
"_index": "student",
"_type": "_doc",
"_id": "1001",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
# 重复请求,将变成更新
{
"_index": "student",
"_type": "_doc",
"_id": "1001",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}
更新附带校验操作
# POST
# 注意这个操作只能POST请求,地址附加后缀 /_update
# JSON参数需要包括在doc属性种
http://127.0.0.1:9200/索引名/_doc/文档ID/_update # JSON
{
"doc": {
"key1": "value1",
"key2": "value2",
"key3": "value3",
}
}
案例:
# POST
http://127.0.0.1:9200/student/_doc/1005/_update # JSON
{
"doc": {
"name": "zhangsan2",
"nickname": "zhangsan2",
"sex": "女",
"age": 30
}
} # 响应结果
{
"_index": "student",
"_type": "_doc",
"_id": "1005",
"_version": 2,
"result": "noop",
"_shards": {
"total": 0,
"successful": 0,
"failed": 0
},
"_seq_no": 7,
"_primary_term": 1
}
三、GET DOC BY ID 查看文档
# GET
http://localhost:9200/student/_doc/1001
案例:
# GET
http://localhost:9200/student/_doc/1002 # 查询成功
{
"_index": "student",
"_type": "_doc",
"_id": "1002",
"_version": 2,
"_seq_no": 5,
"_primary_term": 1,
"found": true,
"_source": {
"name": "Cloud9",
"gender": "male",
"age": 24
}
} # 查询未找到
{
"_index": "student",
"_type": "_doc",
"_id": "1003",
"found": false
}
四、DELETE DOC 删除文档
# DELETE
http://127.0.0.1:9200/索引名/_doc/文档ID
案例:
# DELETE
http://127.0.0.1:9200/student/_doc/1005 # 删除成功
{
"_index": "student",
"_type": "_doc",
"_id": "1005",
"_version": 3,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 8,
"_primary_term": 1
}
# 删除失败 文档不存在
{
"_index": "student",
"_type": "_doc",
"_id": "1005",
"_version": 4,
"result": "not_found",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 9,
"_primary_term": 1
}
7、映射操作
一、给指定索引创建映射:
# PUT
http://127.0.0.1:9200/索引名/_mapping
# JSON参数
{
"properties": {
"key1": {
"type": "text",
"index": false
},
"key2": {
"type": "boolean",
"index": false
},
"key3": {
"type": "long"
}
}
}
映射设定支持的K键类型:
Text
Keyword
Long
Integer
Short
Byte
Double
Float
Half Float
Boolean
Date
Binary
二、查看映射:
# GET
http://127.0.0.1:9200/索引名/_mapping
案例:
# GET
http://127.0.0.1:9200/student/_mapping # 结果
{
"student": {
"mappings": {
"properties": {
"age": {
"type": "long"
},
"gender": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"nickname": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"sex": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
三、创建索引时直接设定映射
# PUT
# 和第一个差不多,没有 /_mapping路径要求
http://127.0.0.1:9200/索引名 # JSON参数
{
"properties": {
"key1": {
"type": "text",
"index": false
},
"key2": {
"type": "boolean",
"index": false
},
"key3": {
"type": "long"
}
}
}
【ElasticSearch】01 CRUD操作的更多相关文章
- ElasticSearch基础+文档CRUD操作
本篇博客是上一篇的延续,主要用来将年前学习ES的知识点做一个回顾,方便日后进行复习和汇总!因为近期项目中使用ES出现了点小问题,因此在这里做一个详细的汇总! [01]全文检索和Lucene (1)全文 ...
- 第三百六十二节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)基本的索引和文档CRUD操作、增、删、改、查
第三百六十二节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)基本的索引和文档CRUD操作.增.删.改.查 elasticsearch(搜索引擎)基本的索引 ...
- Elasticsearch技术解析与实战(二)文档的CRUD操作
启动Elasticsearch和kibana 访问Elasticsearch:http://localhost:9200/?pretty 访问kibana:http://localhost:5601 ...
- 四十一 Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)基本的索引和文档CRUD操作、增、删、改、查
elasticsearch(搜索引擎)基本的索引和文档CRUD操作 也就是基本的索引和文档.增.删.改.查.操作 注意:以下操作都是在kibana里操作的 elasticsearch(搜索引擎)都是基 ...
- elasticsearch入门(简单的crud操作)
记录一下,elasticsearch从创建索引到插入数据的一个crud操作. 一.创建索引 curl -XPUT "http://192.168.99.1:9200/productindex ...
- Elasticsearch的CRUD:REST与Java API
CRUD(Create, Retrieve, Update, Delete)是数据库系统的四种基本操作,分别表示创建.查询.更改.删除,俗称"增删改查".Elasticsearch ...
- ElasticSearch第二步-CRUD之Sense
ElasticSearch系列学习 ElasticSearch第一步-环境配置 ElasticSearch第二步-CRUD之Sense ElasticSearch第三步-中文分词 ElasticSea ...
- 【JAVA与DOM4J实现对XML文档的CRUD操作】
一.简介 1.网上下载DOM4J 1.6.1压缩包,解压开之后,发现几个目录和一个jar文件,jar文件是必须的文件其它目录: docs目录:帮助文档的目录,单击index.html: Quick s ...
- 【ELK】4.spring boot 2.X集成ES spring-data-ES 进行CRUD操作 完整版+kibana管理ES的index操作
spring boot 2.X集成ES 进行CRUD操作 完整版 内容包括: ============================================================ ...
- SpringMVC 使用 RESTful 架构实现 CRUD 操作
软件152 余建强 源码下载:http://download.csdn.net/detail/qq_35318576/9826210 1 使用框架 SpringMVC.Maven.Ajax.JSTL. ...
随机推荐
- 你知道键盘是如何工作的吗?(xv6键盘驱动程序)
键盘驱动程序 公众号:Rand_cs 键盘如何工作的前文曾经说过,当时是以 Linux 0.11 为基础讲的但不系统,本文以 xv6 的键盘驱动程序为例来系统地讲述键盘是如何工作的.关于驱动程序前文磁 ...
- undefined,null 和 undeclared 有什么区别?
a.null表示"没有对象",即该处不应该有值,转为数值时为0.典型用法是:作为函数的参数,表示该函数的参数不是对象.作 为对象原型链的终点. b.undefined表 ...
- Maven常用命令有哪些?
a.install 本地安装, 包含编译,打包,安装到本地仓库编译 - javac 打包 - jar, 将java代码打包为jar文件 安装到本地仓库 - 将打包的jar文件,保存到本地仓库目录中. ...
- 增补博客 第七篇 python 比较不同Python图形处理库或图像处理库的异同点
OpenCV.Pillow 和 scikit image OpenCV(OpenCV 是一个强大的计算机视觉库,它提供了各种图像处理和计算机视觉算法的实现,可以处理各种图像和视频数据. 异同点 跨平台 ...
- 彻底解决C盘不够用的问题(Windows 10)- 常规方法——清垃圾、转虚拟内存、挪大文件
1.清垃圾 2.转虚拟内存 3.挪大文件
- Imdeploy笔记
Smiling & Weeping ---- 天气不好的时候,我会小心地把自己心上的裂缝补起来.为什么?... LMDeploy 的量化和部署 1 环境配置 2 服务部署 2.1 模型转换 2 ...
- FolkMQ 1.6.0(纯血国产,适合信创)
FolkMQ 是个"新式"的消息中间件.强调:"简而强".可内嵌,可单机,可集群(部署包为 9Mb). 功能简表 角色 功能 生产者(客户端) 发布普通消息.Q ...
- Linux下安装PHP环境
Tips:当你看到这个提示的时候,说明当前的文章是由原emlog博客系统搬迁至此的,文章发布时间已过于久远,编排和内容不一定完整,还请谅解` Linux下安装PHP环境 日期:2018-5-7 阿珏 ...
- 使用 TensorRT C++ API 调用GPU加速部署 YOLOv10 实现 500FPS 推理速度——快到飞起!!
NVIDIA TensorRT 是一款用于高性能深度学习推理的 SDK,包含深度学习推理优化器和运行时,可为推理应用程序提供低延迟和高吞吐量.YOLOv10是清华大学研究人员近期提出的一种实时目标 ...
- 防止unordered_map 被卡方法
codeforces 上看到的,mark 一下代码.原作者:neal,原链接:https://codeforces.com/blog/entry/62393 struct custom_hash { ...