基本操作
查看集群健康状态
GET /_cluster/health
创建索引
DELETE <index_name>
GET _all
/* 索引只是用来指向一个或多个分片的 逻辑命名空间。
一个分片是一个最小级别的“工作单元”,只是保存了索引的一部分,分片 是lucene 的一个实例 ,document 存在分片中。
分片可以是主分片或者复制分片(复制分片只是主分片的一个副本),每个文档属于一个单独的主分片,主分片决定了索引最多能存多少数据。【当索引创建完成时,主分片得数量固定了,但是复制分片的数量可以随时调整】
*/
PUT /blogs
{
"settings" : {
"number_of_shards" : 3 ,
"number_of_replicas" : 1
}
}
GET /blogs
{
"blogs" : {
"aliases" : {},
"mappings" : {
"category" : {
"properties" : {
"category_id" : {
"type" : "long"
},
"categroy__name" : {
"type" : "text" ,
"fields" : {
"keyword" : {
"type" : "keyword" ,
"ignore_above" : 256
}
}
}
}
}
},
"settings" : {
"index" : {
"creation_date" : "1503644509430" ,
"number_of_shards" : "3" ,
"number_of_replicas" : "1" ,
"uuid" : "qXGpptmRSy6WmIJLMv8Vqw" ,
"version" : {
"created" : "5050099"
},
"provided_name" : "blogs"
}
}
}
}
GET blogs/_search
{
"took" : 0 ,
"timed_out" : false ,
"_shards" : {
"total" : 3 ,
"successful" : 3 ,
"failed" : 0
},
"hits" : {
"total" : 1 ,
"max_score" : 1 ,
"hits" : [
{
"_index" : "blogs" ,
"_type" : "category" ,
"_id" : "1" ,
"_score" : 1 ,
"_source" : {
"category_id" : 1 ,
"categroy__name" : "重点人员"
}
}
]
}
}
PUT /{index}/{type }/{id }
{
"field" : "value" ,
...
}
PUT /website/blog/123
{
"title" : "My first blog entry" ,
"text" : "Just trying this out..." ,
"date" : "2017/08/25"
}
{
"_index" : "website" ,
"_type" : "blog" ,
"_id" : "123" ,
"_version" : 1 ,
"result" : "created" ,
"_shards" : {
"total" : 2 ,
"successful" : 1 ,
"failed" : 0
},
"created" : true
}
POST /website/blog/
{
"title" : "My second blog entry" ,
"text" : "Still trying this out..." ,
"date" : "2017/08/25"
}
{
"_index" : "website" ,
"_type" : "blog" ,
"_id" : "AV4YQDOMSI2kIEo0OHJp" ,
"_version" : 1 ,
"result" : "created" ,
"_shards" : {
"total" : 2 ,
"successful" : 1 ,
"failed" : 0
},
"created" : true
}
检索文档
GET /website?pretty
GET /website/blog/123 ?pretty
GET /website/blog/123 ?_source=title,text
GET /website/blog/123 /_source
HEAD /website/blog/123
HEAD /website/blog/1234
更新文档
/**
ElasticSearch 已经标记就文档为删除并添加了一个完整的文档,就文档不会消失,但是不能去访问它。
过程:
1.从就文档中检索JSON
2.修改它
3.删除就文档
4.索引新文档
*/
PUT /website/blog/123
{
"title" : "My first blog entry" ,
"text" : "I am updateting this document..." ,
"date" : "2017/08/25"
}
{
"_index" : "website" ,
"_type" : "blog" ,
"_id" : "123" ,
"_version" : 2 ,
"result" : "updated" ,
"_shards" : {
"total" : 2 ,
"successful" : 1 ,
"failed" : 0
},
"created" : false
}
创建一个新文档
//创建一个新文档
POST /website/blog/
{ ... }
//使用 op_type 查询参数:
PUT /website/blog/123 ?op_type=create
{ ... }
//在URL后加 /_create 做为端点:
PUT /website/blog/123 /_create
{ ... }
//->返回
{
"error" : "DocumentAlreadyExistsException[[website][4] [blog][123]:
document already exists]" ,
"status" : 409
}
删除文档
DELETE /website/blog/123
{
"found" : true ,
"_index" : "website" ,
"_type" : "blog" ,
"_id" : "123" ,
"_version" : 3
}
{
"found" : false ,
"_index" : "website" ,
"_type" : "blog" ,
"_id" : "123" ,
"_version" : 4
}
文档局部更新
POST /website/blog/1 /_update
{
"doc" : {
"tags" : [ "testing" ],
"views" : 0
}
}
{
"_index" : "website" ,
"_type" : "blog" ,
"_id" : "1" ,
"_version" : 2 ,
"found" : true ,
"_source" : {
"title" : "My first blog entry" ,
"text" : "I am updateting this document..." ,
"date" : "2017/08/25" ,
"views" : 0 ,
"tags" : [
"testing"
]
}
}
POST /website/blog/1 /_update
{
"script" : "ctx._source.views+=1"
}
POST /website/pageviews/1 /_update
{
"script" : "ctx._source.views+=1" ,
"upsert" : {
"views" : 1
}
}
{
"_index" : "website" ,
"_type" : "pageviews" ,
"_id" : "1" ,
"_version" : 1 ,
"result" : "created" ,
"_shards" : {
"total" : 2 ,
"successful" : 1 ,
"failed" : 0
}
}
POST /website/pageviews/1 /_update?retry_on_conflict=5
{
"script" : "ctx._source.views+=1" ,
"upsert" : {
"views" : 0
}
}
检索多个文档
POST /_mget
{
"docs" : [
{
"_index" : "website" ,
"_type" : "blog" ,
"_id" : 2
},
{
"_index" : "website" ,
"_type" : "pageviews" ,
"_id" : 1 ,
"_source" : "views"
}
]
}
{
"docs" : [
{
"_index" : "website" ,
"_type" : "blog" ,
"_id" : "2" ,
"found" : false
},
{
"_index" : "website" ,
"_type" : "pageviews" ,
"_id" : "1" ,
"_version" : 2 ,
"found" : true ,
"_source" : {
"views" : 2
}
}
]
}
POST /website/blog/_mget
{
"docs" : [
{
"_id" : 2
},
{
"_type" : "pageviews" ,
"_id" : 1
}
]
}
POST /website/blog/_mget
{
"ids" :["1" ,"2" ]
}
更新时,批量操作
POST /_bulk
{ action: { metadata }}\n
{ request body }\n
{ action: { metadata }}\n
{ request body }\n
POST /_bulk
{ "delete" : { "_index" : "website" , "_type" : "blog" , "_id" : 123 }}
{ "create" : { "_index" : "website" , "_type" : "blog" , "_id" : 123 }}
{ "title" : "My first blog post" }
{ "index" : { "_index" : "website" , "_type" : "blog" }}
{ "title" : "My second blog post" }
{ "update" : {"_index" : "website" , "_type" : "blog" , "_id" : 123 }}
{ "doc" : {"title" : "My updated blog post" } }
POST /_bulk
{ "create" : { "_index" : "website" , "_type" : "blog" , "_id" : "123" }}
{ "title" : "Cannot create - it already exists" }
{ "index" : { "_index" : "website" , "_type" : "blog" , "_id" : "123" }}
{ "title" : "But we can update it" }
/**
可以看到,
一个或多个请求失败。
这个请求的HTTP状态码被报告为 409 CONFLICT 。
错误消息说明了什么请求错误。
第二个请求成功了,状态码是 200 OK 。
**这些说明 bulk 请求不是原子操作——它们不能实现事务。**
*/
{
"took" : 157 ,
"errors" : true ,
"items" : [
{
"create" : {
"_index" : "website" ,
"_type" : "blog" ,
"_id" : "123" ,
"status" : 409 ,
"error" : {
"type" : "version_conflict_engine_exception" ,
"reason" : "[blog][123]: version conflict, document already exists (current version [13])" ,
"index_uuid" : "-c47RmMbTUC43gFFI3Iwhg" ,
"shard" : "0" ,
"index" : "website"
}
}
},
{
"index" : {
"_index" : "website" ,
"_type" : "blog" ,
"_id" : "123" ,
"_version" : 14 ,
"result" : "updated" ,
"_shards" : {
"total" : 2 ,
"successful" : 1 ,
"failed" : 0
},
"created" : false ,
"status" : 200
}
}
]
}
POST /website/_bulk
{ "index" : { "_type" : "log" }}
{ "event" : "User logged in" }
POST /website/log/_bulk
{ "index" : {}}
{ "event" : "User logged in" }
{ "index" : { "_type" : "blog" }}
{ "title" : "Overriding the default type" }