总结工作中常用ES脚本,方便日后查阅,此次脚本应用于es5
增删操作
建立索引
curl -XPUT 'http://192.168.6.122:9200/major_info'
完整创建
curl -X PUT --header 'Content-Type: application/json' --header 'Accept: application/json' 'http://127.0.0.1:9200/farer_index' -d@test.json
test.json
{
"settings":{
"number_of_shards":5,
"number_of_replicas":0
},
"mappings":{
"article":{
"dynamic":"strict",
"_all":{
"enabled":false
},
"properties":{
"title":{
"type":"string",
"index":"analyzed",
"analyzer":"ik_max_word",
"search_analyzer":"ik_max_word"
},
"describe":{
"type":"string",
"index":"analyzed",
"analyzer":"ik_max_word",
"search_analyzer":"ik_max_word"
},
"content":{
"type":"string",
"index":"analyzed",
"analyzer":"ik_max_word",
"search_analyzer":"ik_max_word"
},
"author":{
"type":"string",
"index":"no"
},
"time":{
"type":"date",
"index":"not_analyzed",
"format":"yyyy-MM-dd HH:mm:ss"
}
}
}
}
}
删除索引
curl -XDELETE 'http://192.168.6.122:9200/major_info_202006'
mapping操作
设置mapping
curl -XPUT '192.168.10.183:9200/test2/entities/_mapping' -d@test.json
mapping文件样例
{
"entities" : {
"dynamic" : "false",
"properties" : {
"concept_id" : {
"type" : "long",
"store" : true
},
"entity_id" : {
"type" : "long",
"store" : true
},
"entity_name" : {
"type" : "text",
"store" : true
}
}
}
}
拉取mapping
curl -XGET '192.168.6.122:9200/weibo_info_202002/_mapping?pretty' > weibo_info_202002.json
整体状况
查看实体以及数据量
curl -XGET 'http://192.168.6.122:9200/_cat/indices?v'
查看所有节点
curl '10.128.55.188:9200/_cat/nodes?v'
健康状况检查
来源
elasticsearch 查看集群健康、节点状态_u011250186的博客-CSDN博客_es查看集群健康状态
elasticsearch启动后查看是否启动成功:
curl -XGET"http://$(hostname):9200/_cluster/health?pretty=true"2. 停止elasticsearch应用:
curl -XPOST "http:// $(hostname):9200/_shutdown"3. 查看集群健康:
curl $(hostname):9200/_cluster/health?pretty4. 检查集群状态:
curl $(hostname):9200/_cluster/stats?pretty5. 节点状态:
curl $(hostname):9200/_nodes/process?prettycurl $(hostname):9200/_nodes/node1/process?pretty
6. 当你不知道有那些属性可以查看时:
curl '$(hostname):9200/_cat/',会返回可以查看的属性
查看指定索引库的健康状态
http://localhost:9200/_cluster/health/index_name?pretty
http://localhost:9200/_cluster/health/index_name,index_name2?pretty
模板相关操作
来源
es 使用curl创建索引模板_laimao8079的博客-CSDN博客_es创建索引模板
创建模板
curl -X PUT --header 'Content-Type: application/json' --header 'Accept: application/json' 'http://127.0.0.1:9200/_template/people_trail' -d@tmp_people_trail.json
tmp_people_trail.json
{
"template":"people_trail*",
"settings":{
"index":{
"number_of_shards":"1",
"number_of_replicas":"0",
"analysis":{
"analyzer":{
"pinyin":{
"tokenizer":"pinyin_tokenizer"
}
},
"tokenizer":{
"pinyin_tokenizer":{
"type":"pinyin",
"keep_joined_full_pinyin":true
}
}
}
}
},
"mappings":{
"_default_":{
"_all":{
"enabled":true,
"analyzer":"standard"
},
"dynamic_templates":[
{
"xms":{
"mapping":{
"fields":{
"py":{
"analyzer":"pinyin",
"type":"text"
}
},
"type":"keyword"
},
"match_mapping_type":"string",
"match":"XM"
}
},
{
"strings":{
"match_mapping_type":"string",
"mapping":{
"type":"keyword",
"fields":{
"full":{
"type":"text",
"analyzer":"standard"
}
}
}
}
}
]
}
}
}
新版本
{
"index_patterns": ["my_index*"],
"aliases": {
"my_alias": {}
},
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"field1": {
"type": "text"
},
"field2": {
"type": "keyword"
}
}
}
}
旧版本
{
"template":"log_info*",
"mappings":{
"article":{
"dynamic":"strict",
"_all":{
"enabled":false
},
"properties":{
"title":{
"type":"string",
"index":"analyzed",
"analyzer":"ik_max_word",
"search_analyzer":"ik_max_word"
},
"author":{
"type":"string",
"index":"no"
},
"itemId":{
"type":"long"
},
"site":{
"type":"keyword"
},
"time":{
"type":"date",
"index":"not_analyzed",
"format":"yyyy-MM-dd HH:mm:ss"
},
"laudio":{
"type":"nested",
"properties":{
}
},
"nfri":{
"type":"long"
}
}
}
}
}
查询所有模板
curl -XGET 127.0.0.1:9200/_cat/templates
查看某一个模板
curl -XGET http://127.0.0.1:9200/_template/article_type?pretty
删除一个索引模板
curl -XDELETE http://127.0.0.1:9200/_template/article_type
添加别名
curl -H "Content-Type: application/json" -XPOST 'http://192.168.6.122:9200/_aliases' -d '
{
"actions": [
{"add": {"index": "major_info_202007_v2", "alias": "main_info"}}
]
}'
查看索引别名
curl -XGET http://192.168.6.122:9200/major_info_202007/_aliases
查看别名下的索引
curl -XGET '192.168.6.122:9200/_alias/major_info'
删除别名
curl -XPOST 'http://192.168.6.122:9200/_aliases' -d '
{
"actions" : [
{ "remove" : { "index" : "major_info_202007_v2","alias" : "major_info" } }
]
}'
查看所有别名
curl -XGET '192.168.6.122:9200/_alias'
查询操作
查询
curl -XGET http://192.168.6.122:9200/major_info_202007/major_info_202007/e7af0e55d1307e021f8eb7594bfd3737?pretty
搜索
curl -XGET 'http://192.168.6.122:9200/major_info/major_info/_search' -d '{"query":{"match":{"uuid":""}}}'
reindex
curl -XPOST 'localhost:9200/_reindex?pretty' -H 'Content-Type: application/json' -d'
{
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter"
}
}
'
按照pubtime倒叙排列
curl -XGET 'http://10.128.55.7:6200/news_ref/news_ref/_search' -d '{ "sort" : [{ "pubtime" : {"order" :"desc"}} ],"from":0,"size":10 }'
term搜索以后排序
curl -XGET 'http://192.168.10.183:9200/info_data/info_data/_search' -d '{"query" : {"term" : { "site_group" : "106" } }, "sort" : [{ "id" : {"order" :"asc"}} ],"from":0,"size":1 }'
根据site_group查询
curl -XGET 'http://192.168.10.183:9200/info_data/info_data/_search' -d '
{
"query":{
"term":{
"site_group":"106"
}
},
"sort":[
{
"id":{
"order":"asc"
}
}
],
"from":0,
"size":1
}
'
根据复合条件查询
curl -XGET 'http://192.168.10.183:9200/info_data/info_data/_search' -d '
{
"query":{
"bool":{
"must":[
{
"term":{
"data_type":"1"
}
},
{
"term":{
"site_group":"106"
}
}
]
}
},
"sort":[
{
"id":{
"order":"asc"
}
}
],
"from":0,
"size":1
}
'
查询删除 delete_by_query
这种删除最快,但是一定要慎重,先查询确认,再删除
curl -X POST "localhost:9200/twitter/_delete_by_query?scroll_size=5000" -H 'Content-Type: application/json' -d'
{
"query": {
"term": {
"user": "kimchy"
}
}
}
'
range query 范围查询
curl -XGET 'x.x.55.188:9200/second_short_video_info_ref/second_short_video_info_ref/_search' -d'
{
"query": {
"range": {
"pubtime": {
"gte": "2022-03-23 00:00:00",
"lte": "2022-03-23 23:59:00"
}
}
},
"size":0
}'