一、查看集群信息以及索引监控
GET /_cat/health
Get /{index}/_cat/_settings
GET /index1,index2/_segments //n查看索引段信息
二、创建索引
//创建索引 ik分词器 多重字段
PUT /my_index10{
"settings": {
"number_of_shards": "3",
"number_of_replicas": "1",
"analysis": {
"analyzer": {
"default": {
"tokenizer": "ik_smart",
"filter": [
"synonym"
]
}
},
"filter": {
"synonym": {
"type": "synonym",
"synonyms_path": "analysis/synonym.txt"
}
}
}
},
"mappings": {
"_doc": {
"properties": {
"@timestamp": {
"type": "date"
},
"beat": {
"properties": {
"hostname": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"version": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"message": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"offset": {
"type": "long"
},
"prospector": {
"properties": {
"type": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"source": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
三、文档操作
3.1 新建
//id指定
PUT twitter/_doc/1
{"id": 1, "user" : "kimchy", "post_date": "2009-11-15T14:12:12", "message" : "trying out Elasticsearch"}
//id自动生成
POST twitter/_doc/ { "id": 1, "user" : "kimchy", "post_date" : "2009-11-15T14:12:12", "message" : "trying out Elasticsearch" }
3.2 根据id获取
HEAD twitter/_doc/11 //11是_id
GET twitter/_doc/1 //1是_id
GET twitter/_doc/1?_source=false
GET /twitter/_mget { "docs" : [ { "_type" : "_doc", "_id" : "1" }, { "_type" : "_doc", "_id" : "2" }] }
GET /twitter/_doc/_mget { "ids" : ["1", "2"] }
3.3 删除
DELETE twitter/_doc/1 //指定_id删除
DELETE twitter/_doc/1?version=1 //使用版本号控制
POST twitter/_delete_by_query { "query": { "match": { "message": "some message" }}} //查询删除
3.4 修改
指定文档id进行修改
PUT twitter/_doc/1 { "id": 1,"user" : "kimchy", "post_date": "2009-11-15T14:12:12", "message" : "trying out Elasticsearch"}
// 乐观锁并发更新控制
PUT twitter/_doc/1?version=1 { "id": 1, "user" : "kimchy", "post_date" : "2009-11-15T14:12:12","message" : "trying out Elasticsearch" }
//
POST uptest/_doc/1/_update
{
"script" : {
"source": "ctx._source.counter += params.count",
"lang":"painless",
"params" : {
"count" : 4
}
}
}
POST twitter/_update_by_query { "script": { "source": "ctx._source.likes++", "lang": "painless" }, "query": { "term": { "user": "kimchy" } } }
脚本说明:painless是es内置的一种脚本语言,ctx执行上下文对象(通过它还可访问_index, _type, _id, _version, _routing and _now (the current timestamp) ),params是参数集合
说明:脚本更新要求索引的_source 字段是启用的。更新执行流程: 1、获取到原文档
2 、通过_source字段的原始数据,执行脚本修改。
3、删除原索引文档
4、索引修改后的文档 它只是降低了一些网络往返,并减少了get和索引之间版本冲突的可能性。
网友评论