代码拉取完成,页面将自动刷新
# 集群整体状态查询
GET _cat/health?v
# 各个节点状态
GET _cat/nodes?v
# 各个索引状态
GET _cat/indices?v
# 某个索引的分片情况
GET _cat/shards/.kibana_1?v
# 各个节点分片情况
GET _cat/allocation?v
# 查看文档总数
GET _cat/count?v
# 参数说明:
# v : 显示表头
GET _cat/count?v
# & : 拼接多个参数
# help: 返回表头的解释
GET _cat/count?help
# h: 指定要显示的列
GET _cat/count?v&h=count
# format: 指定结果的格式
GET _cat/count?v&format=json
# s: 指定排序
GET _cat/indices?v&s=docs.count:desc
#ES存储数据结构
# Movie(id ,name, doubanScore, actorList)
# Actor(id ,name)
# Mysql存储
#Movie表
# id name doubanScore
# 1001 红海行动 8.0
# 1002 战狼 9.0
#Actor表
# id name
# 101 张译
# 102 海清
# 103 吴京
# m_a
# movieId actorId
# 1001 101
# 1001 102
# 1002 101
# 1002 103
#ES存储: 所见即所得.
#{
# "id":"1",
# "name":"operation red sea",
# "doubanScore":"8.5",
# "actorList":[
# {"id":"1","name":"zhangyi"},
# {"id":"2","name":"haiqing"},
# {"id":"3","name":"zhanghanyu"}
# ]
#}
# 创建索引(不指定字段信息,ES按照第一条数据自动推断)
PUT movie_index
# 创建索引(指定字段信息,后面讲)
# 删除索引
DELETE movie_index
# 写入数据(幂等写 ,需要指定docId)
PUT /movie_index/_doc/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/_doc/2
{
"id":2,
"name":"operation meigong river",
"doubanScore":8.0,
"actorList":[
{"id":3,"name":"zhang han yu"}
]
}
PUT /movie_index/_doc/3
{
"id":3,
"name":"incident red sea",
"doubanScore":5.0,
"actorList":[
{"id":4,"name":"atguigu"}
]
}
# 查询索引中的数据
GET movie_index/_search
# 写入数据 (非幂等写,不指定docId)
POST /movie_index/_doc
{
"id":3,
"name":"incident red sea",
"doubanScore":5.0,
"actorList":[
{"id":4,"name":"atguigu"}
]
}
# 修改
# 幂等写可以实现修改(整体替换)
# 单个字段修改
POST movie_index/_update/3
{
"doc": {
"doubanScore":"7.0"
}
}
# 基于查询修改
#POST movie_index/_update_by_query
# 删除
# 基于docid删除
DELETE movie_index/_doc/1
# 基于查询删除
#POST movie_index/_delete_by_query
# 基于docId查询
GET movie_index/_doc/1
# 查询所有数据
GET movie_index/_search
# 分词查询
# 按照关键词"operation red sea"查询
GET movie_index/_search
{
"query": {
"match": {
"name": "operation red sea"
}
}
}
# ES会为字符串的字段默认添加倒排索引。
# 倒排索引:
# opreation 1 2
# red 1 3
# sea 1 3
# meigong 2
# river 2
# incident 3
# 搜索关键词 :"operation red sea"
# 分词: operation red sea
# 匹配结果: 1 2 1 3 1 3
# 1(3次) 3(2次) 2 (1次)
# 子属性
# 分词查询"zhang yi"
GET movie_index/_search
{
"query": {
"match": {
"actorList.name": "zhang yi"
}
}
}
# 短语匹配
GET movie_index/_search
{
"query": {
"match_phrase": {
"actorList.name": "zhang yi"
}
}
}
# 字符串的列存
# ES中的每个字段都是列存.
GET movie_index/_search
{
"query": {
"term": {
"actorList.name.keyword": {
"value": "zhang yi"
}
}
}
}
GET movie_index/_search
{
"query": {
"term": {
"name.keyword": {
"value": "operation red sea"
}
}
}
}
# 索引的mapping
GET movie_index/_mapping
# 条件过滤name=”operation red sea”
GET movie_index/_search
{
"query": {
"bool": {
"filter": [
{
"term": {
"name.keyword": "operation red sea"
}
}
]
}
}
}
# 范围过滤:查询doubanScore大于等于7小于等于8
GET movie_index/_search
{
"query": {
"bool": {
"filter": [
{
"range": {
"doubanScore": {
"gte": 7,
"lte": 8
}
}
}
]
}
}
}
# must 和 should
#分词匹配”red sea” , 条件过滤演员名为”zhang han yu”
GET movie_index/_search
{
"query": {
"bool": {
"filter": [
{
"term": {
"actorList.name.keyword": "zhang han yu"
}
}
]
,
"must": [
{
"match": {
"name": "red sea"
}
}
]
}
}
}
GET movie_index/_search
{
"query": {
"bool": {
"filter": [
{
"term": {
"actorList.name.keyword": "zhang han yu"
}
}
]
,
"should": [
{
"match": {
"name": "red sea"
}
}
]
}
}
}
#过滤修改
POST movie_index/_update_by_query
{
"query": {
"term": {
"name.keyword": {
"value": "incident red sea"
}
}
}
,
"script": {
"source": "ctx._source['actorList'][0]['name']=params.newName",
"params": {
"newName":"shangguigu"
},
"lang": "painless"
}
}
POST movie_index/_update/3
{
"script": {
"source": "ctx._source['actorList'][0]['name']=params.newName",
"params": {
"newName":"atguigu"
},
"lang": "painless"
}
}
# 过滤删除
POST movie_index/_delete_by_query
{
"query": {
"bool": {
"filter": [
{
"range": {
"doubanScore": {
"lte": 8.0
}
}
}
]
}
}
}
# 排序
GET movie_index/_search
{
"sort": [
{
"doubanScore": {
"order": "asc"
}
}
]
}
# 分页查询
# 当前页码: pageNum
# 每页条数: pageSize
# 每页从哪条数据取(from) ,取多少条(pageSize)
# from = (pageNum-1) * pageSize
GET movie_index/_search
{
"from": 2
,
"size": 2
}
# 高亮
GET movie_index/_search
{
"query": {
"match": {
"name": "red sea"
}
}
,
"highlight": {
"fields": {
"name": {}
}
}
}
# 聚合
# 1)取出每个演员共参演了多少部电影
# 分组: 演员
# 统计: count
# count的结果白送。
GET movie_index/_search
{
"aggs": {
"groupbyactorname": {
"terms": {
"field": "actorList.name.keyword",
"size": 10
}
}
}
,
"size": 0
}
# 每个演员参演电影的平均分是多少,并按平均分排序
# 分组: 演员
# 统计: avg
GET movie_index/_search
{
"aggs": {
"groupbyactorname": {
"terms": {
"field": "actorList.name.keyword",
"size": 10,
"order": {
"avgscore": "asc"
}
}
,
"aggs": {
"avgscore": {
"avg": {
"field": "doubanScore"
}
}
}
}
}
,
"size": 0
}
#SQL
# 每个演员参演电影的平均分是多少,并按评分排序
GET _sql?format=txt
{
"query":
"""
select
actorList.name.keyword ,
avg(doubanScore) avgscore
from
movie_index
where
match(name, 'red sea')
group by
actorList.name.keyword
having
avgscore >= 8.0
order by
avgscore desc
"""
}
# 中文分词
PUT /movie_index_cn/_doc/1
{ "id":1,
"name":"红海行动",
"doubanScore":8.5,
"actorList":[
{"id":1,"name":"张译"},
{"id":2,"name":"海清"},
{"id":3,"name":"张涵予"}
]
}
PUT /movie_index_cn/_doc/2
{
"id":2,
"name":"湄公河行动",
"doubanScore":8.0,
"actorList":[
{"id":3,"name":"张涵予"}
]
}
PUT /movie_index_cn/_doc/3
{
"id":3,
"name":"红海事件",
"doubanScore":5.0,
"actorList":[
{"id":4,"name":"尚硅谷"}
]
}
# 分词匹配"红海行动"
GET movie_index_cn/_search
{
"query": {
"match": {
"name": "红海行动"
}
}
}
GET movie_index_cn/_search
{
"query": {
"match": {
"name": "上海银行"
}
}
}
GET _analyze
{
"text": "红海行动"
}
GET _analyze
{
"text": "上海银行"
}
# 中文语义
# 我也想过过 过过 过过的生活
# 测试ik分词器
# ik_smart
# ik_max_word
GET _analyze
{
"text": "红海行动",
"analyzer": "ik_smart"
}
GET _analyze
{
"text": "红海行动",
"analyzer": "ik_max_word"
}
GET _analyze
{
"text": "我是中国人",
"analyzer": "ik_smart"
}
GET _analyze
{
"text": "我是中国人",
"analyzer": "ik_max_word"
}
GET _analyze
{
"text": "上海银行"
, "analyzer": "ik_smart"
}
# 基于中分分词创建索引(指定mapping)
DELETE movie_index_cn
PUT movie_index_cn
{
"settings": {
"number_of_shards": 1
},
"mappings": {
"properties": {
"id":{
"type": "long"
},
"name":{
"type": "text"
, "analyzer": "ik_smart"
},
"doubanScore":{
"type": "double"
},
"actorList":{
"properties": {
"id":{
"type":"long"
},
"name":{
"type":"keyword"
}
}
}
}
}
}
GET movie_index_cn/_mapping
# 索引别名
# 给索引加别名
# 建索引的时候加, 或者给已存在的索引加
POST _aliases
{
"actions": [
{ "add":
{
"index": "movie_index",
"alias": "movie_index_2022"
}
}
]
}
POST _aliases
{
"actions": [
{ "add":
{
"index": "movie_index_cn",
"alias": "movie_index_2022"
}
}
]
}
GET movie_index/_search
GET movie_index_cn/_search
GET movie_index_2022/_search
GET movie_index/_search
{
"query": {
"term": {
"actorList.name.keyword": {
"value": "zhang han yu"
}
}
}
}
POST _aliases
{
"actions": [
{ "add":
{
"index": "movie_index",
"alias": "movie_index_2022_zhy"
,
"filter": {
"term": {
"actorList.name.keyword": "zhang han yu"
}
}
}
}
]
}
GET movie_index_2022_zhy/_search
POST _aliases
{
"actions": [
{ "add":
{
"index": "movie_index",
"alias": "movie_index_test"
}
}
]
}
POST _aliases
{
"actions": [
{ "remove":
{
"index": "movie_index",
"alias": "movie_index_test"
}
}
,
{ "add":
{
"index": "movie_index_cn",
"alias": "movie_index_test"
}
}
]
}
GET movie_index_test/_search
# 索引模板
PUT _template/template_movie2022
{
"index_patterns": ["movie_test*"],
"settings": {
"number_of_shards": 1
},
"aliases" : {
"{index}-query": {},
"movie_test-query":{}
},
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"movie_name": {
"type": "text",
"analyzer": "ik_smart"
}
}
}
}
PUT movie_test1
GET movie_test1/_mapping
PUT movie1
GET movie1/_mapping
GET _cat/templates?v
GET _template/template_movie2022
# 段合并
POST movie_index/_forcemerge?max_num_segments=1
GET _cat/indices/?s=segmentsCount:desc&v&h=index,segmentsCount,segmentsMemory,memoryTotal,storeSize,p,r
GET movietest/_search
POST movietest/_delete_by_query
{
"query": {
"bool": {
"filter": [
{
"term": {
"movieName.keyword": "速度与激情"
}
}
]
}
}
}
POST movietest/_update/1001
{
"doc": {
"movieName" : "湄公河行动"
}
}
POST movietest/_update_by_query
{
"query": {
"term": {
"movieName.keyword": {
"value": "战狼"
}
}
}
,
"script": {
"source": "ctx._source['movieName']=params.newName",
"params": {
"newName":"战狼2"
},
"lang": "painless"
}
}
GET movietest/_doc/1001
GET movie_index/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "red sea"
}
}
]
,
"filter": [
{
"range": {
"doubanScore": {
"gte": 5.0
}
}
}
]
}
}
,
"highlight": {
"fields": {
"name": {}
}
}
,
"from": 0
,
"size": 2
,
"sort": [
{
"doubanScore": {
"order": "desc"
}
}
]
}
GET movie_index/_search
{
"aggs": {
"groupbyactorname": {
"terms": {
"field": "actorList.name.keyword",
"size": 10
, "order": {
"avgscore": "desc"
}
}
,
"aggs": {
"avgscore": {
"avg": {
"field": "doubanScore"
}
}
}
}
}
, "size": 0
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。