基本操作
查看集群健康状态
GET /_cluster/health
创建索引
//删除索引
DELETE <index_name>
//查看所有索引
GET _all
//创建索引:
/* 索引只是用来指向一个或多个分片的 逻辑命名空间。
一个分片是一个最小级别的“工作单元”,只是保存了索引的一部分,分片 是lucene 的一个实例 ,document 存在分片中。
分片可以是主分片或者复制分片(复制分片只是主分片的一个副本),每个文档属于一个单独的主分片,主分片决定了索引最多能存多少数据。【当索引创建完成时,主分片得数量固定了,但是复制分片的数量可以随时调整】
*/
//只分配3个主分片和一个复制分片(每个主分片都有一个复制分片):
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": "重点人员"
}
}
]
}
}
//
//index 一个文档
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
}
//id省略,则为自增
POST /website/blog/
{
"title": "My second blog entry",
"text": "Still trying this out...",
"date": "2017/08/25"
}
//->返回 id=UUID
{
"_index": "website",
"_type": "blog",
"_id": "AV4YQDOMSI2kIEo0OHJp",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
检索文档
//检索文档
//文档中有 _index, _type, _id ,可以使用 HTTP 方法 进行直接检索
GET /website?pretty
GET /website/blog/123?pretty
//检索文档的一部分,_soruce 显示一部分,过滤了不需要的字段
GET /website/blog/123?_source=title,text
//只显示_source的值
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"
}
//->返回 可以看到_version 变为2
{
"_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
//如文档被找到,Elasticsearch将返回 200 OK 状态码和以下响应体。注意 _version 数字已经增加了。
{
"found" : true,
"_index" : "website",
"_type" : "blog",
"_id" : "123",
"_version" : 3
}
//如果文档未找到,我们将得到一个 404 Not Found 状态码,响应体是这样的:
{
"found" : false,
"_index" : "website",
"_type" : "blog",
"_id" : "123",
"_version" : 4
}
//不管找到,或者没有找到,_version 都加1
文档局部更新
//它们不能被更改,只能被替换,处理流程:检索-修改-重建索引流程
//最简单的 update 请求表单接受一个局部文档参数 doc ,它会合并到现有文档中
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
}
}
//更新冲突。在update API 在检索阶段 检索文档前 _version,然后重建索引阶段通过index请求提交,如果其他程在检索(retrieve)和重建索引(reindex)阶段修改了文档, _version 将不能被匹配,然后更新失败。
POST /website/pageviews/1/_update?retry_on_conflict=5
{
"script": "ctx._source.views+=1",
"upsert": {
"views": 0
}
}
检索多个文档
//使用multi-get或者 mget API
/*
1.mget API参数是一个 docs 数组
2.数组的每个节点定义一个文档 _index 、 _type 、 _id 元数据
3.检索一个或几个确定的字段,也可以定义一个 _source 参数:
*/
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
}
}
]
}
//的文档在同一个 _index 中(甚至在同一个 _type 中),可以在URL中定义一个默认的 /_index 或者 _index/_type
POST /website/blog/_mget
{
"docs": [
{
"_id": 2
},
{
"_type": "pageviews",
"_id": 1
}
]
}
//所有文档相同,可以使用ids 数组来完成 docs数组
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" }
//覆盖数据行的 _index 和 _type ,没有覆盖时, URL 中的值 会被作为 默认值
POST /website/log/_bulk
{ "index": {}}
{ "event": "User logged in" }
{ "index": { "_type": "blog" }}
{ "title": "Overriding the default type" }