1.es操作

1.安装
1.1安装es的服务端elasticsearch
1.2安装head插件,需要依赖node.js
1.3 安装kibana插件
修改kibana的配置文件,server.host url
Systemctl start|stop|restart|status kibana
默认kibana的端口为 5601
注意:linux中安装时候,默认不能使用root用户启动。
1)groupadd es 创建组
2)useradd es -g es 创建用户es属于es组
3)passwd es 修改密码
4)使用普通用户登录 /home/es
5)在普通用户中解压文件 安装 (默认启动要1g内存)
./elasticsearch -d 后台方式启动
6)开启远程连接权限,修改配置文件 network.host 0.0.0.0
2.ElasticSearch核心概念:
索引 类型 字段 映射 文档
1)索引index: 用户索引 订单索引 商品索引 分类索引,索引类似于一个数据库
索引中放的
2)类型type:一个类型是索引上的一个逻辑的分区或分类,可以定义多个类型。但是在 6.x中,一个索引只包含一个类型。
3)映射Mapping:定义索引中类型的数据格式约束
4)文档document:json格式的文档
如果只创建了索引,也可以添加文档,这时候es会自动创建类型,自动创建mapping。

3.创建索引库
3.1使用postman创建一个空的索引库(不带有mapping)
http://localhost:9200/blog blog(索引库的名称)put请求
3.2使用postman 创建一个带有mapping映射的索引库
http://localhost:9200/blog1 put请求
{
“mappings”:{
“article”:{ #第一个表名(type)
“properties”:{
“id”:{ #字段的名称
“type”:“long”,
“store”:true
},
“title”:{
“type”:“text”,
“store”:true,
“index”:true,
“analyzer”:“standard”
},
“content”:{
“type”:“text”,
“store”:true,
“index”:true,
“analyzer”:“standard”
}
}
}
}
}

3.3为已经创建好的空的索引库,添加mapping
http://localhost:9200/blog/hello/_mappings
Post请求 blog是索引库名称 hello是type的名称(对哪个type进行mapping)
{
“hello”:{
“properties”:{
“id”:{
“type”:“long”,
“store”:true
},
“title”:{
“type”:“text”,
“store”:true,
“index”:true,
“analyzer”:“standard”
},
“content”:{
“type”:“text”,
“store”:true,
“index”:true,
“analyzer”:“standard”
}
}
}
}
3.4删除索引库
http://localhost:9200/blog2 delete请求

4.创建文档
4.1创建文档document
http://localhost:9200/blog1/article/1 post请求
blog1:索引库
article:哪个type
1:文档的id,每个文档有一个真正的_id(如果不指定,会默认为每个文档得到一随机id),这个_id是主键,可以对文档进行操作
请求体
{
“id”:1,
“title”:“新添加的文档1”,
“content”:“新添加的文档的内容”
}
4.2删除document
http://localhost:9200/blog1/article/1 delete请求
4.3修改document
原理:先删除 再添加
http://localhost:9200/blog1/article/1 post请求
如果_id=1存在了,内容就是修改 如果_id=1不存在,就是添加
请求体:
{
“id”:1,
“title”:“修改后的文档1”,
“content”:“修改后文档的内容”
}
4.4查询document:
4.4.1根据_id查询document
http://localhost:9200/blog/hello/1 get请求
4.4.2根据关键词查询document
http://localhost:9200/blog/hello/_search post请求
请求体
{
“query”:{
“term”:{ # 关键词查询(不对关键词分词)
“title”:“修” #根据mapping中的某个字段–包含这个词语查询
}
}
}
4.4.3query_string查询
http://localhost:9200/blog/hello/_search post请求
请求体
{
“query”:{
“query_string”:{
“default_field”:“title”, #(指定一个默认的查询域)
“query”:“修改你好啊” #查询的时候会先把query进行分词后,再去查询
}
}
}

5.分词器
5.1查看分词器效果
http://localhost:9200/_analyze post请求
{
“text”:“老用户有礼了”,
“analyzer”:“standard”
}
5.2 es集成ik分词器
把ik分词器的安装包放在es的plugins目录下重启es服务即可
Ik分词器有两种算法:ik_smart最少切分 ik_max_word最细粒度划分
5.3 查看ik分词器效果
http://localhost:9200/_analyze post请求
{
“text”:“老用户有礼了”,
“analyzer”:“ik_smart”
}

6.es集群
6.1集群的相关概念
6.1.1集群cluster

7.Kibana的基本操作
7.1索引操作
1)创建一个索引 PUT /ems
2)查看所有索引 GET /_cat/indices?v
3)查看单个索引 GET /ems
4)删除一个索引 DELETE /ems

7.2创建索引和类型的映射
PUT /ems
{
“mappings”:{ #固定写法
“emp”:{ #类型名称
“properties”:{ #固定写法
“id”: {“type”:”text”}, #类型下面有哪些字段,字段可以用对象格式,也可用一个值 如”id”:”long”,
“name”: {“type”:”text”},
“age”:{“type”:”integer”},
“bir”:{“type”:”date”}
}
}
}
}
7.3查看索引类型和映射
GET /索引名称/_mapping
7.4文档操作
1)添加文档
PUT /索引名/类型名/1 #这个1是es的默认_id
{
“id”:”21”,
“name”:”xiaohei”
}
添加文档,不指定_id
POST /索引名/类型名

2)查询文档
GET /索引名/类型名/_id
3)更新文档
先删除,再更新
POST /索引名/类型名/_id
{
xxx
}
更新原始文档的执行字段
POST /索引名/类型名/_id/_update
{
xxx
}
4)删除文档
DELETE /索引名/类型名/_id
5)批量操作 多种类型的操作
POST /索引名/类型名/_bulk
{“index”:{“_id”:”3”}}
{“name”:”zs”,”content”:”good”}
{“index”:{“_id”:”4”}}
{“name”:”zs”,”content”:”good”}
{“delete”:{“_id”:”5”}}
{“update”:{“_id”:”4”}}
{“doc”:{“name”:”xxx”}}

7.5检索操作
1)查询所有
GET /索引名/类型名/_search
{
“query”:{
“match_all”:{}
}
}
2)查询所有 返回指定条数
GET /索引名/类型名/_search
{
“query”:{
“match_all”:{}
},
“size”:4
}
3)分页查询
GET /索引名/类型名/_search
{
“query”:{
“match_all”:{}
},
“sort”:[ #排序
{
“age”:{“order”:”desc”}
}
],
“size”:4,
“from”:0 #这个数字是记录的条数开始数,不是页数
}
4)查询指定字段内容
GET /索引名/类型名/_search
{
“query”:{
“match_all”:{}
},
“_source”:[“name”,”age”,”content”]
}

5)关键词term查询 除了text类型,其他类型不分词。
GET /索引名/类型名/_search
{
“query”:{
“term”:{
“content”:{ #某个字段
“value”:”黑”
}
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值