建模
PUT test
{
"settings": {
"number_of_shards": 10,
"number_of_replicas": 1,
"refresh_interval": "1s"
},
"mappings": {
"_doc": {
"properties": {
"uid": {
"type": "long"
},
"phone": {
"type": "long"
},
"message": {
"type": "keyword"
},
"msgcode": {
"type": "long"
},
"sendtime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"date_more":{
"type":"date",
"format":"yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || yyyy/MM/dd HH:mm:ss|| yyyy/MM/dd ||epoch_millis"
}
}
}
}
}
修改模型
#控制台
POST /test/_mapping
{
"properties": {
"message": {
"type": "keyword",
"ignore_above": 256
}
}
}
#curl
curl -XPOST "localhost:9200/test/_mapping?pretty" -H 'content-Type:application/json' -d '
{
"properties": {
"message": {
"type": "keyword",
"ignore_above": 256
}
}
}
'
- 修改同类型,加属性,可以通过DSL直接修改
- 修改不同类型,不允许修改。可以通过索引重建后,reindex的方式进行数据迁移。
新增
PUT test/_doc/1 { "counter" : 1, "tags" : ["red"] }
修改
doc修改
POST test/_update/1 #版本6写法是:POST test/_doc/1/_update { "doc" : { "name" : "new_name" }, "doc_as_upsert" : true #true 新增或更新 }
script修改
需要确保有该字段,否则脚本运行报错POST test/_update/1 #版本6写法是:POST test/_doc/1/_update { "script" : { "source": "if (ctx._source.tags.contains(params.tag)) { ctx.op = 'delete' } else { ctx.op = 'none' }", "lang": "painless", "params" : { "tag" : "green" } }, "upsert" : { #默认值 "counter" : 1 } }
script修改:常用
POST test/_doc/1/_update { "script": { "source": "if(ctx._source.containsKey('teacher_resource_k_ids')){for (def i =0;i< ctx._source.teacher_resource_k_ids.length;i++){def item = ctx._source.teacher_resource_k_ids[i]; if(item.inner_es_id_str !=null && item.inner_es_id_str.equals(params.inner_es_id_str)){ctx._source.teacher_resource_k_ids.remove(i)}}}else{ctx._source.teacher_resource_k_ids=[]} if(params.newValue != null){ctx._source.teacher_resource_k_ids.add(params.newValue)}", "lang": "painless", "params": { "inner_es_id": "111", "newValue": { "id": 1 } } }, "upsert": { "counter": 1 } }
查询修改_update_by_query
POST teset/_update_by_query?conflicts=proceed #conflicts=proceed 失败继续执行 { "script": { "source": "ctx._source.likes++", "lang": "painless" }, "query": { "term": { "user": "kimchy" } } }
按脚本条件更新 默认值
# POST teacher-center-school_question/_update_by_query?conflicts=proceed { "script":{ "lang":"painless", "source":"if (ctx._source.groupId== null) {ctx._source.groupId= ctx._id}" } }
查询
ES高版本,需要返回总数,需要增加参数 ```bash和query同级
“track_total_hits”:true
<a name="dwFz8"></a>
## 删除
<a name="dXIN6"></a>
### doc删除
```json
DELETE /test/_doc/1?routing=123456 #routing可以不指定
delete_by_query
POST /twitter/_delete_by_query
{
"query": {
"match": {
"message": "some message"
}
}
}
bulk操作
doc bulk
POST _bulk
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_id" : "2" } }
{ "create" : { "_index" : "test", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }
script bulk
POST _bulk
{ "update" : {"_id" : "1", "_index" : "index1", "retry_on_conflict" : 3} }
{ "doc" : {"field" : "value"} }
{ "update" : { "_id" : "0", "_index" : "index1", "retry_on_conflict" : 3} }
{ "script" : { "source": "ctx._source.counter += params.param1", "lang" : "painless", "params" : {"param1" : 1}}, "upsert" : {"counter" : 1}}
{ "update" : {"_id" : "2", "_index" : "index1", "retry_on_conflict" : 3} }
{ "doc" : {"field" : "value"}, "doc_as_upsert" : true }
{ "update" : {"_id" : "3", "_index" : "index1", "_source" : true} }
{ "doc" : {"field" : "value"} }
{ "update" : {"_id" : "4", "_index" : "index1"} }
{ "doc" : {"field" : "value"}, "_source": true}
mget
GET /_mget
{
"docs" : [
{
"_index" : "twitter",
"_id" : "1"
},
{
"_index" : "twitter",
"_id" : "2"
}
]
}
reindex
#本地
POST _reindex
{
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter"
}
}
# 2:远程
# A-->B 需要在B节点设置白名单 A
#在elasticSearch.yml 添加 :reindex.remote.whitelist: A:9200
POST _reindex
{
"source":{
"remote":{
"host":"http://A:9200"
},
"index":"twitter"
},
"dest":{
"index":"new_twitter"
}
}
别名修改_aliases
#操作 remove和add
POST _aliases
{
"actions": [
{
"add": {
"index": "test1",
"alias": "alias1"
}
}
]
}
分词效果
#es7
GET /twitter/_termvectors/1?fields=message
#es2
/index/type/1/_termvectors?fields=name
#文本
GET [indexe可选]/_analyze
{
"text":"elastic search",
"analyzer": "pinyin"
}
聚合
<深入理解ElasticSearch>(第三版) 第四章-数据建模与分析
修改副本
PUT /twitter/_settings
{
"number_of_replicas": 1
}
#可初始化时设置0,初始化完成设置1
集群修改
#GET
PUT _cluster/settings
#PUT GET的内容可以直接复制
PUT _cluster/settings
{
"persistent": {
"threadpool": {
"bulk": {
"largest": "48",
"queue_size": "3000",
"size": "48",
"type": "fixed",
"max": "48"
},
"search": {
"queue_size": "2000",
"size": "400",
"type": "fixed"
}
}
},
"transient": {
"cluster": {
"routing": {
"allocation": {
"enable": "none"
}
}
},
"threadpool": {
"bulk": {
"largest": "48",
"queue_size": "3000",
"size": "48",
"type": "fixed",
"max": "48"
},
"search": {
"queue_size": "2000",
"size": "400",
"type": "fixed"
}
}
}
}
# 关闭monitor监控
PUT /_cluster/settings
{
"transient": {
"xpack.monitoring.collection.enabled": false
}
}
# 关闭自动分配分片(会导致新建索引有问题,一般用于停止节点前设置一下,提高重启时间;或者临时设置)
PUT /_cluster/settings
{
"persistent": {
"cluster.routing.allocation.enable": "none" #打开设置成all
}
}
#临时关闭自动分片
PUT /_cluster/settings
{
"transient": {
"cluster.routing.allocation.enable": "none" #打开设置成all
}
}
curl
查询:curl localhost:10200/test/_doc/111
修改:curl -XPOST http://localhost:10200/test/_doc/111/_update -H ‘content-Type:application/json’ -d ‘{“doc” : {“name”:”咣咣咣” }}’
指标聚合
{
"size": 0,
"aggs": {
"orgCount": {
"terms": {
"field": "orgId"
}
}
}
}
es7断路器
curl -XPUT "localhost:10200/_cluster/settings" -H 'content-Type:application/json' -d '
{
"persistent" : {
"indices.breaker.fielddata.limit" : "10%",
"indices.breaker.request.limit" : "60%",
"indices.breaker.total.limit" : "70%"
}
}
'
indices.breaker.fielddata.limit:fielddata的内存限制,默认60%
indices.breaker.request.limit:执行聚合的内存限制,默认40%
indices.breaker.total.limit:综合上面两个,限制在70%以内
其他设置:(目前配置再yml中)
indices.breaker.request.limit: 10%
#设置单个request请求的内存熔断限制,默认是jvm堆的60%(es7.0引入了新的内存熔断机制,会智能判断,规避OOM)。
#index.merge.scheduler.max_thread_count: 1
##设置segment合并时占用的线程数,配置线程数越多对磁盘io消耗就越大(SSD忽略)。
indices.queries.cache.size: 20%
##query请求可使用的jvm内存限制,默认是10%。
indices.requests.cache.size: 2%
##查询request请求的DSL语句缓存,被缓存的DSL语句下次请求时不会被二次解析,可提升检索性能,默认值是1%。
indices.fielddata.cache.size: 10%
##设置字段缓存的最大值,默认无限制。
refresh间隔
curl -XPUT “localhost:10200/cms_single_question_screen/_settings” -H ‘content-Type:application/json’ -d ‘
{ “refresh_interval”: “30s” }’
PUT index/_settings
{
"refresh_interval": "1s"
}
只读
PUT index/_settings
{
"index.blocks.read_only_allow_delete":"false"
}
官网
以下是7.x版本的官网手册,以官网为准 ↓↓↓
手册首页:https://www.elastic.co/guide/en/elasticsearch/reference/7.x/getting-started.html
restAPI:https://www.elastic.co/guide/en/elasticsearch/reference/7.x/rest-apis.html
indexApi:https://www.elastic.co/guide/en/elasticsearch/reference/7.x/indices-aliases.html
java highLevel https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.x/java-rest-high.html