如何运用CURL命令来操作Elasticsearch
最编程
2024-07-25 18:40:02
...
使用CURL命令操作ES
当前文档所用ES版本 6.4.3
ElasticSearch 提供了一系列的Restful风格的API,我们可以使用curl命令进行使用,也可以在kibana中使用。
Restful风格
它是一种网络应用程序的设计风格和开发方式,基于HTTP,可以使用XML格式定义或JSON格式定义。 RESTFUL适用于移动互联网厂商作为业务使能接口的场景,实现第三方OTT调用移动网络资源的功能,动作类型为新增、变更、删除所调用资源。通过不同的请求方法来实现不同的功能。
GET
获取数据 POST
添加数据 PUT
添加数据 DELETE
删除数据
ElasticSearch的核心概念与关系数据库对比
集群常用命令
- 查看版本
curl -XGET 'http://hadoop137:9200'
- 查看集群状态
curl -XGET 'http://hadoop137:9200/_cluster/state?pretty'
#这里在url后面添加了pretty是为了让其在控制台上输出的结果是一个优美的json格式
索引库常用命令
- 查看所有索引信息
curl -XGET 'http://hadoop137:9200/_cat/indices?pretty&v'
- 创建索引
curl -XPUT 'http://hadoop137:9200/upuptop?pretty'
- 删除索引
curl -XDELETE 'http://hadoop137:9200/upuptop?pretty'
文档常用命令
- 创建文档
# 9200/索引库名/文档类型/id/ -d 文档内容
# id可以忽略,ES会自动生成id,如果id存在,那么就是更新数据,字段可以增加
curl -XPOST 'http://hadoop137:9200/upuptop/stu/1?pretty' -H 'Content-Type: application/json' -d '
{
"name":"shaofei",
"age":22,
"sex":1
}'
- 修改文档
# 给id为1的学生修改姓名为upuptop,年龄修改为25,添加学号字段
curl -XPOST 'http://hadoop137:9200/upuptop/stu/1?pretty' -H 'Content-Type: application/json' -d '
{
"name":"upuptop",
"age":25,
"sex":0,
"number":"1501260222"
}'
- 查看所有文档
curl -XGET 'http://hadoop137:9200/upuptop/stu/_search?pretty'
- 根据id查看文档
curl -XGET 'http://hadoop137:9200/upuptop/stu/1?pretty'
- 删除文档
curl -XDELETE 'http://hadoop137:9200/upuptop/stu/1?pretty'
查询命令
ES最主要的功能,搜索,也是就是查询文档。下面我们来看看主要的查询命令吧。
首先搞点数据到ElasticSearch中
这里使用logstash工具将mysql数据库中的数据导入到ES中
对于LogStash的介绍请查看这篇文章:《LogStash的安装部署与应用》。
使用查询命令对数据进行查询。
- 根据field来查询数据:
查询name含有‘upuptop'字符串的数据、模糊查询
curl -XGET http://hadoop137:9200/upuptop/stu/_search?q=name="upuptop"
- 根据field来查询数据:match
查询name含有‘upuptop'字符串的数据、模糊查询
curl -XGET http://hadoop137:9200/upuptop/stu/_search?pretty -H 'Content-Type: application/json' -d '
{
"query":
{"match":
{"name":"upuptop"}
}
}'
- 对多个field发起查询:multi_match
查询lastname、firstname含有‘upuptop'字符串的数据
curl -XGET http://hadoop137:9200/upuptop/stu/_search?pretty -d '
{
"query":
{"multi_match":
{
"query":"upuptop",
"fields":["last_name","first_name"],
"operator":"and"
}
}
}'
- 多个term对多个field发起查询:bool(boolean)
# 组合查询,must,must_not,should
# must + must : 交集
# must +must_not :差集
# should+should : 并集
# 查询 first_name为upuptop并且年龄是33的数据
curl -XGET http://hadoop137:9200/upuptop/stu/_search?pretty -d '
{
"query":
{"bool" :
{
"must" :
{"match":
{"first_name":"upuptop"}
},
"must" :
{"match":
{"age":33}
}
}
}
}'
# 查询 first_name为upuptop并且年龄不是33的数据
curl -XGET http://hadoop137:9200/upuptop/stu/_search?pretty -d '
{
"query":
{"bool" :
{
"must" :
{"match":
{"first_name":"upuptop"}
},
"must_not" :
{"match":
{"age":33}
}
}
}
}'
# 查询 first_name不为upuptop并且年龄不是33的数据
curl -XGET http://hadoop137:9200/upuptop/stu/_search?pretty -d '
{
"query":
{"bool" :
{
"must_not" :
{"match":
{"first_name":"upuptop"}
},
"must_not" :
{"match":
{"age":33}
}
}
}
}'
- 查询first_name=upuptop的,或者年龄在20岁到33岁之间的
curl -XGET http://hadoop137:9200/upuptop/stu/_search -d '
{
"query":
{"bool" :
{
"must" :
{"term" :
{ "first_name" : "upuptop" }
}
,
"must_not" :
{"range":
{"age" : { "from" : 20, "to" : 33 }
}
}
}
}
}'
- 修改配置
## 创建索引库并设置副本数为2个,默认5个主分片,每个分片有两个副本,一共15个片
curl -XPUT 'http://hadoop137:9200/test2/' -d'{"settings":{"number_of_replicas":2}}'
## 设置3个主分片、3个从分片
curl -XPUT 'http://hadoop137:9200/test3/' -d'{"settings":{"number_of_shards":3,"number_of_replicas":3}}'
curl -XPUT 'http://hadoop137:9200/test4/' -d'{"settings":{"number_of_shards":6,"number_of_replicas":4}}'
## 设置索引的mapping,设置数据类型,分词规则等
curl -XPOST http://192.168.9.11:9200/upuptop/person/_mapping -d'
{
"person": {
"properties": {
"content": {
"type": "string",
"store": "no",
"term_vector": "with_positions_offsets",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word",
"include_in_all": "true",
"boost": 8
}
}
}
}'
推荐阅读
-
使用AnyCAD C#开发:如何通过RenderWindow3d::ExecuteCommand来实现鼠标移动对象的命令操作?
-
在 Kubernetes 中,如何使用 kubectl exec 命令来执行容器内的操作
-
如何在Linux中使用Shell命令来操作
-
如何运用CURL命令来操作Elasticsearch
-
实用指南:如何运用Elasticsearch Snapshot进行备份操作
-
大聪明教你学Java|Mybatis的一级缓存和二级缓存--🍊作者简介:不愿过河东,一个来自二线城市的程序员,致力于用 "猥琐 "的方法解决琐碎的问题,让复杂的问题变得简单易懂。支持作者:喜欢👍,关注💖,留言💌~! 前言。 在计算机世界中,缓存无处不在;操作系统有操作系统缓存,数据库会有数据库缓存,我们还可以利用中间件(如 Redis)来充当缓存。MyBatis 作为一个优秀的 ORM 框架,也用于缓存,所以今天我们就来谈谈 Mybatis 的一级缓存和二级缓存。 Mybatis 一级缓存 首先,我们来看一张图片👇。 我们在开发项目的过程中,如果打开Mybatis的SQL语句打印,经常会看到这样一句话:创建一个新的 SqlSession,其实这就是我们常说的 Mybatis 一级缓存。 Mybatis 的一级缓存也就是在执行一次 SQL 查询或 SQL 更新后,这条 SQL 语句并不会消失,而是被 MyBatis 缓存起来,当再次执行同样的 SQL 语句时,就会直接从缓存中提取出来,而不用再次执行 SQL 命令。第一级缓存也称为 SqlSession 级缓存。对数据库进行操作时,需要构建一个 SqlSession 对象,其中有一个用于存储缓存数据的数据结构(HashMap)。对象中有一个用于存储缓存数据的数据结构(HashMap)。不同 SqlSession 之间的缓存数据区域(HashMap)互不影响。 在我们的应用系统运行过程中,我们可能会在一个数据库会话中,执行多条查询条件相同的 SQL 语句,那么对于这种情况,你来进行设计,那么你会如何考虑呢?没错,就是加入缓存,MyBatis也是这么来处理的,如果是同一条SQL语句,会优先打入一级缓存,避免直接查询数据库,给数据库造成压力,提高性能。具体实现过程如下图所示👇 SqlSession 是一个接口,提供了一些 CRUD 方法,SqlSession 的默认实现类是 DefaultSqlSession,DefaultSqlSession 类持有 Executor 接口对象,Executor 的默认实现是 BaseExecutor 对象,每个 BaseExecutor 对象都有一个 PerpetualCache 缓存,即上图中的本地缓存。当用户发起查询时,MyBatis 会根据当前执行的语句生成一个 MappedStatement,并在本地缓存中进行查询,如果缓存被命中,查询结果会直接返回给用户;如果缓存未被命中,查询结果会直接返回给用户。如果缓存未命中,则查询数据库,将结果写入本地缓存,最后将结果返回给用户。这时候可能有小伙伴要说了:我还在控制台中看到 "关闭非事务性 SqlSession "这句话,说明我每次创建一个 SqlSession 到 SqlSession 结束都是关闭的,那么我的缓存还是有毛线用!😥 事情肯定不是我们想的那样,让我们继续👇。 🍊 getSqlSession 源代码
-
详解Linux Tar命令中如何运用绝对路径进行打包操作
-
在SQL Server中实际操作:如何用sp_password命令来重设SA账号的密码示例
-
如何在 Linux 中运用curl命令通过各类代理,详解各类代理类型
-
在 Linux 操作系统中,如何运用 lsblk 和 blkid 命令?