ES的一些常用命令
1、增
##新增doc
1 POST /saas-mp-promotion-search_activity/_doc 2 { 3 "activityIdUnique": "999-1721299" 4 }
2、删
## 删除doc
1 POST /saas-mp-promotion-search_activity/_delete_by_query 2 { 3 "query":{ 4 "term":{ 5 "activityIdUnique":{ 6 "value":"3-1721299" 7 } 8 } 9 } 10 }
## 复杂条件删除doc
1 POST /saas-mp-promotion-search_activity/_delete_by_query 2 { 3 "query":{ 4 "bool":{ 5 "must":[ 6 { 7 "term":{ 8 "bosId":{ 9 "value":4000180336837 10 } 11 } 12 }, 13 { 14 "range":{ 15 "endTime":{ 16 "gte":1642517198004 17 } 18 } 19 }, 20 { 21 "terms":{ 22 "activityStatus":[ 23 "0", 24 "1", 25 "4", 26 "5", 27 "6" 28 ] 29 } 30 } 31 ], 32 "must_not":[ 33 { 34 "exists":{ 35 "field":"activityIdUnique" 36 } 37 } 38 ] 39 } 40 } 41 }
3、改
## 更新doc:
1 POST /saas-mp-promotion-search_activity_scope/_update/101_3-21000950540215 2 { 3 "doc":{ 4 "storeId":[ 5 1 6 ] 7 } 8 }
修改已有字段值方法1,设置默认值
1 POST index_test/_doc/_update_by_query 2 { 3 "script":{ 4 "lang":"painless", 5 "source":"if (ctx._source.skuNumber == null) {ctx._source.skuNumber = 'sss'}" 6 } 7 }
修改移除已有字段数据
POST saas-mp-promotion-search_goods/_update_by_query { "script": { "lang": "painless", "inline": """ctx._source.remove("titleList.vid")""" }, "query": { "ids": { "values" : ["103_120000_100313970052000"] } } }
修改已有字段值方法2
1 POST /saas-mp-promotion-search_activity_scope/_update_by_query 2 { 3 "query": { 4 "bool": { 5 "must": [ 6 { 7 "term": { 8 "activityId": { 9 "value": 2161 10 } 11 } 12 } 13 ] 14 } 15 }, 16 "script": { 17 "source": "ctx._source.payType = 1;ctx._source.deliveryType = [1,2]" 18 } 19 }
修改已有字段(数组对象新增一个成员)
POST test/_update_by_query { "query": { "ids": { "values": [ "103_120000_100313947052000" ] } }, "script": { "lang": "painless", "source": "if(ctx._source.goodsTitleList==null){List ls = new ArrayList();ls.add(params.member);ctx._source.goodsTitleList=ls;}else{ctx._source.goodsTitleList.add(params.member)}", "params": { "member": { "belongVid": 0, "title": "" } } } }
修改已有字段(数组对象for循环)
POST test/_update_by_query { "query": { "ids": { "values": ["103_120000_100313970052000"] } }, "script": { "lang": "painless", "source": "for(e in ctx._source.goodsTitleList){e.belongVid = ctx._source.goodsBelongVid;e.title = ctx._source.title;}" } }
修改已有字段(数组对象删除一个成员)
POST test/_update_by_query { "query": { "ids": { "values": ["103_120000_100313970052000"] } }, "script": { "lang": "painless", "source": "ctx._source.titleList.removeIf(list_item -> list_item.belongVid==120000)" } }
4、查
##查询
1 GET /saas-mp-promotion-search_activity/_search 2 { 3 "query":{ 4 "bool":{ 5 "must":[ 6 { 7 "term":{ 8 "bosId":{ 9 "value":4000180336837 10 } 11 } 12 }, 13 { 14 "range":{ 15 "endTime":{ 16 "gte":1642504062959 17 } 18 } 19 }, 20 { 21 "terms":{ 22 "activityStatus":[ 23 "0", 24 "1", 25 "4", 26 "5", 27 "6" 28 ] 29 } 30 } 31 ], 32 "must_not":[ 33 { 34 "exists":{ 35 "field":"activityIdUnique" 36 } 37 } 38 ] 39 } 40 } 41 }
5、查看索引配置
1 GET /saas-mp-promotion-search_activity/_settings
6、查看索引mapping
1 GET /saas-mp-promotion-search_activity/_mapping
7、创建索引
// 可以根据5拿到的配置创建索引
// number_of_shards-分片
// number_of_replicas-备份数
// analysis-分词分析器
1 PUT es_create_test 2 { 3 "settings":{ 4 "index":{ 5 "number_of_shards":"3", 6 "number_of_replicas":"0", 7 "analysis":{ 8 "analyzer":{ 9 "my_analyzer":{ 10 "tokenizer":"my_tokenizer" 11 } 12 }, 13 "tokenizer":{ 14 "my_tokenizer":{ 15 "token_chars":[ 16 "letter", 17 "digit", 18 "whitespace", 19 "punctuation", 20 "symbol" 21 ], 22 "type":"ngram" 23 } 24 } 25 } 26 } 27 } 28 }
8、创建mapping
1 PUT saas-mp-promotion-goods-sync-test/_doc/_mapping?include_type_name=true 2 { 3 "properties":{ 4 "activityId":{ 5 "type":"long" 6 }, 7 "activityIdUnique":{ 8 "type":"keyword" 9 }, 10 "activityLevelType":{ 11 "type":"integer" 12 }, 13 "activityName":{ 14 "type":"text", 15 "fields":{ 16 "cn":{ 17 "type":"text", 18 "analyzer":"ik_max_word" 19 }, 20 "en":{ 21 "type":"text", 22 "analyzer":"english" 23 }, 24 "raw":{ 25 "type":"keyword" 26 } 27 }, 28 "analyzer":"my_analyzer" 29 }, 30 "startTime":{ 31 "type":"date", 32 "format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" 33 }, 34 "timeEffect":{ 35 "type":"nested", 36 "properties":{ 37 "repeatDay":{ 38 "type":"keyword" 39 }, 40 "repeatEndInterval":{ 41 "type":"keyword" 42 }, 43 "repeatGmt":{ 44 "type":"integer" 45 }, 46 "repeatStartInterval":{ 47 "type":"keyword" 48 }, 49 "repeatType":{ 50 "type":"integer" 51 }, 52 "searchRepeatEndInterval":{ 53 "type":"keyword" 54 }, 55 "searchRepeatStartInterval":{ 56 "type":"keyword" 57 } 58 } 59 }, 60 "update_time":{ 61 "type":"date", 62 "format":"yyyy-MM-dd HH:mm:ss" 63 } 64 } 65 }
9、重建索引
1 POST _reindex 2 { 3 "source":{ 4 "index":"test1" 5 }, 6 "dest":{ 7 "index":"test_20220117" 8 }, 9 "script":{ 10 "inline":"ctx._routing= ctx._source.bosId", 11 "lang":"painless" 12 } 13 }
10、加别名
1 POST _aliases 2 { 3 "actions":[ 4 { 5 "add":{ 6 "index":"test", 7 "alias":"test_alias" 8 } 9 } 10 ] 11 }
11、nested(嵌套)
1 GET /saas-mp-promotion-search_goods/_search 2 { 3 "track_total_hits": true, 4 "query": { 5 "nested": { 6 "path": "goodsStock", 7 "query": { 8 "exists": { 9 "field": "goodsStock.sellQuantity" 10 } 11 } 12 } 13 } 14 }
12、判断es两个字段是否相等
1 POST /saas-mp-promotion-search_activity/_search 2 { 3 "track_total_hits": true, 4 "query": { 5 "bool": { 6 7 "must": [ 8 9 { 10 "term": { 11 "bizSource": { 12 "value": 101 13 } 14 } 15 }, 16 { 17 "exists": { 18 "field": "outActivityId" 19 } 20 }, 21 { 22 "script": { 23 "script": { 24 "inline": "doc['activityId'] != doc['outActivityId']", 25 "lang": "painless" 26 } 27 } 28 } 29 ] 30 } 31 } 32 }
13、修改原字段类型
重要:es不支持直接修改,需要重建索引
// 创建索引 PUT /saas-mp-promotion-search_goods_20220222_back { "settings":{ "index":{ "number_of_shards":"16", "number_of_replicas":"1", "analysis" : { "analyzer" : { "my_analyzer" : { "tokenizer" : "my_tokenizer" } }, "tokenizer" : { "my_tokenizer" : { "token_chars" : [ "letter", "digit", "whitespace", "punctuation", "symbol" ], "type" : "ngram" } } } } } } // 创建索引mapping PUT saas-mp-promotion-search_goods_20220222_back/_doc/_mapping?include_type_name=true { "properties" : { "abilityCodeList" : { "type" : "keyword" }, "bizSource" : { "type" : "long" },"createTime" : { "type" : "date", "format" : "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" },"deductStockType" : { "type" : "integer" }, "defaultPicUrl" : { "type" : "text", "fields" : { "cn" : { "type" : "text", "analyzer" : "ik_max_word" }, "en" : { "type" : "text", "analyzer" : "english" }, "raw" : { "type" : "keyword" } }, "analyzer" : "ik_max_word" },"goodsBrand" : { "type" : "nested", "properties" : { "brandId" : { "type" : "long" }, "log" : { "type" : "text", "fields" : { "cn" : { "type" : "text", "analyzer" : "ik_max_word" }, "en" : { "type" : "text", "analyzer" : "english" }, "raw" : { "type" : "keyword" } }, "analyzer" : "ik_max_word" }, "name" : { "type" : "text", "fields" : { "cn" : { "type" : "text", "analyzer" : "ik_max_word" }, "en" : { "type" : "text", "analyzer" : "english" }, "raw" : { "type" : "keyword" } }, "analyzer" : "ik_max_word" }, "type" : { "type" : "integer" } } },"vid" : { "type" : "long" } } } // 将原索引数据导入新索引 POST _reindex { "source":{ "index":"saas-mp-promotion-search_goods" }, "dest":{ "index":"saas-mp-promotion-search_goods_20220222_back" }, "script":{ "inline":"ctx._routing= ctx._source.bosId", "lang":"painless" } } // 将新索引设置别名 POST _aliases { "actions":[ { "add":{ "index":"saas-mp-promotion-search_goods_20220222_back", "alias":"saas-mp-promotion-search_goods" } } ] } // 将老索引删除 POST _aliases { "actions":[ { "remove":{ "index":"saas-mp-promotion-search_goods_20220117", "alias":"saas-mp-promotion-search_goods" } } ] }
14、查看后台任务
GET _tasks?detailed=true&actions=indices:data/write/update/byquery
15、nested字段判空
POST saas-mp-promotion-search_goods/_update_by_query { "query": { "bool": { "must_not": [ { "nested": { "path": "goodsVidInfoList", "query": { "exists": { "field": "goodsVidInfoList" } } } } ] } }, "script": { "lang": "painless", "source": "if(ctx._source.goodsVidInfoList==null){List ls = new ArrayList();ls.add(params.member);ctx._source.goodsVidInfoList=ls;}", "params": { "member": { "vid": 0, "title": "" } } } }
posted on 2022-01-24 13:41 Iversonstear 阅读(326) 评论(0) 编辑 收藏 举报