2 Star 2 Fork 17

weiyunhui789/BigData220212

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ES.txt 12.89 KB
一键复制 编辑 原始数据 按行查看 历史
weiyunhui 提交于 2022-06-28 15:42 . ES
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
# 集群整体状态查询
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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/weiyunhui789/Bigdata220212.git
git@gitee.com:weiyunhui789/Bigdata220212.git
weiyunhui789
Bigdata220212
BigData220212
master

搜索帮助