4.1 ES 回顾
4.1.1 基本概念
cluster |
整个elasticsearch 默认就是集群状态,整个集群是一份完整、互备的数据。 |
node |
集群中的一个节点,一般只一个进程就是一个node |
shard |
分片,即使是一个节点中的数据也会通过hash算法,分成多个片存放,默认是5片。(7.0默认改为1片) |
index |
相当于rdbms的database, 对于用户来说是一个逻辑数据库,虽然物理上会被分多个shard存放,也可能存放在多个node中。 |
type |
类似于rdbms的table,但是与其说像table,其实更像面向对象中的class , 同一Json的格式的数据集合。(6.x只允许建一个,7.0被废弃,造成index实际相当于table级) |
document |
类似于rdbms的 row、面向对象里的object |
field |
相当于字段、属性 |
4.1.2 启动脚本
#!/bin/bash
es_home=/opt/module/elasticsearch-6.3.1
kibana_home=/opt/module/kibana-6.3.1
case $1 in
"start") {
for i in hadoop201 hadoop202 hadoop203
do
ssh $i "source /etc/profile;${es_home}/bin/elasticsearch >/dev/null 2>&1 &"
done
nohup ${kibana_home}/bin/kibana >kibana.log 2>&1 &
};;
"stop") {
ps -ef|grep ${kibana_home} |grep -v grep|awk '{print $2}'|xargs kill
for i in hadoop201 hadoop202 hadoop203
do
ssh $i "ps -ef|grep $es_home |grep -v grep|awk '{print \$2}'|xargs kill" >/dev/null 2>&1
done
};;
*){
echo "你启动的姿势不正确, 请使用参数 start 来启动es集群, 使用参数 stop 来关闭es集群"
};;
esac
4.1.3 常用操作
# 查询健康状态
GET /_cat/health?v
# 查看所有的索引
GET /_cat/indices?v
# 查看索引分片情况
# 增加一个索引
PUT /movie_index
# 删除一个索引
DELETE /movie_index
# 新增文档
PUT /movie_index/movie/1
{ "id":1,
"name":"operation red sea",
"doubanScore":8.5,
"actorList":[
{"id":1,"name":"zhang yi"},
{"id":2,"name":"hai qing"},
{"id":3,"name":"zhang han yu"}
]
}
PUT /movie_index/movie/2
{
"id":2,
"name":"operation meigong river",
"doubanScore":8.0,
"actorList":[
{"id":3,"name":"zhang han yu"}
]
}
PUT /movie_index/movie/3
{
"id":3,
"name":"incident red sea",
"doubanScore":5.0,
"actorList":[
{"id":4,"name":"zhang chen"}
]
}
# 根据 _id 查找
GET /movie_index/movie/1
# 修改某一个 field 的值
POST /movie_index/movie/1/_update
{
"doc": {
"doubanScore": 10
}
}
# 搜索 匹配 过滤
# 分词匹配
GET /movie_index/movie/_search
{
"query": {
"match": {
"name": "sea"
}
}
}
# 子查询匹配, 层级结构
GET /movie_index/movie/_search
{
"query": {
"match": {
"actorList.name": "zhang"
}
}
}
# 短语匹配: 不拆词 比如查询人名
GET /movie_index/movie/_search
{
"query": {
"match_phrase": {
"name": "operation meigong"
}
}
}
# 过滤
## 查询后过滤
GET /movie_index/movie/_search
{
"query": {
"match": {
"name": "red sea"
}
}
, "post_filter": {
"term": {
"actorList.id": 4
}
}
}
## 查询前过滤
GET /movie_index/movie/_search
{
"query": {
"bool": {
"filter": {
"term": {
"actorList.name": "zhang"
}
}
, "must": [
{"match": {
"actorList.id": 3
}},{
"match": {
"actorList.name": "hai"
}
}
]
}
}
}
# 排序
GET /movie_index/movie/_search
{
"query": {
"match": {
"actorList.name": "zhang"
}
}
, "sort": [
{
"doubanScore": {
"order": "asc"
}
}
]
}
# 分页
GET /movie_index/movie/_search
{
"from": 1
, "size": 2
}
# 高亮
GET /movie_index/movie/_search
{
"query": {
"match": {
"name": "sea"
}
}
, "highlight": {
"fields": {
"name": {}
}
}
}
# 聚合
GET /movie_index/movie/_search
{
"aggs": {
"groupby_actor": {
"terms": {
"field": "actorList.name.keyword",
"size": 10
}
, "aggs": {
"avg_douban": {
"avg": {
"field": "doubanScore"
}
}
}
}
}
}