1.add an index
创建index的primary shards 和 复制shards
primary shards 只能在index创建之前设置 原因是因为 document route 通过id可以计算出此document存在哪个shards 如果动态变化 primary shards 则数据会混乱。默认5个分片
PUT /blogs查看集群的健康状况
{
"settings" : {
"number_of_shards" : 3,
"number_of_replicas" : 1
}
}
GET /_cluster/health水平扩展情况
However, read requests—searches or document retrieval—can be handled by a primary or a replica shard, so the more copies of data that you have, the more search throughput you can handle.
primary分片或复制分片都可以用来查询请求或者文档取回。所以越多的copy就会增加越大的搜索吞吐量
当然如果只在一个node上增加复制分片不会提高吞吐量,原因是每个shard获得了更少的机器资源
In Elasticsearch, all data in every field is indexed by default.
index api
put /indexName/typeName/{id} source
get api
get /indexName/type/id
check document exist
head /indexName/type/id
一个document不存在,不代表他在接下来的几毫秒内不存在,有可能一个progress正在创建此document
document in es are are immutable;
index 在es采用不变模式存储。
we introduce the update
API, which can be used to make partial updates to a document. This API appears to change documents in place, but actually Elasticsearch is following exactly the same process as described previously:
- Retrieve the JSON from the old document
- Change it
- Delete the old document
- Index a new document
The only difference is that the update
API achieves this through a single client request, instead of requiring separate get
and index
requests.
GET /index/type/id/_create 如果此id文档存在,则409创建失败 如果不存在创建成功201
DELETE /index/type/id 删除id的对应文档。(假删除,但不能被搜索到)
解决冲突:
悲观锁。乐观锁
PUT /website/blog/1?version=1当id为1的文档version为1的时候才更新
partial update a document
POST /website/blog/1/_update
{
"doc" : {
"tags" : [ "testing" ],
"views": 0
}
}
partial update a document use script
POST /website/blog/1/_updateupdate a document may not yet exist
{
"script" : "ctx._source.tags+=new_tag",
"params" : {
"new_tag" : "search"
}
}
POST /website/pageviews/1/_updateupdate and conflicts
{
"script" : "ctx._source.views+=1",
"upsert": {
"views": 1
}
}
POST /website/pageviews/1/_update?retry_on_conflict=5
{
"script" : "ctx._source.views+=1",
"upsert": {
"views": 0
}
}
取回多个文档
GET /_mget
{
"docs" : [
{
"_index" : "website",
"_type" : "blog",
"_id" : 2
},
{
"_index" : "website",
"_type" : "pageviews",
"_id" : 1,
"_source": "views"
}
]
}
GET /website/blog/_mget
{
"docs" : [
{ "_id" : 2 },
{ "_type" : "pageviews", "_id" : 1 }
]
}
GET /website/blog/_mget
{
"ids" : [ "2", "1" ]
}
文档不存在 返回值200 因为mget的请求是成功的。具体文档存在否,请检查response中found属性
批量更新,删除,创建,索引
{ action: { metadata }}\n
{ request body }\n
{ action: { metadata }}\n
{ request body }\n
...
The action
must be one of the following:
- Create a document only if the document does not already exist. See Creating a New Document.
- Create a new document or replace an existing document. See Indexing a Document and Updating a Whole Document.
- Do a partial update on a document. See Partial Updates to Documents.
- Delete a document. See Deleting a Document.
create
index
update
delete
POST /website/_bulk
{ "index": { "_type": "log" }}
{ "event": "User logged in" }