ES学习笔记1-基本操作篇

10 篇文章 0 订阅

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:

  1. Retrieve the JSON from the old document
  2. Change it
  3. Delete the old document
  4. 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.

文档不能更新。只能被重新索引。update API将取回文档,重新索引新文档变成了一步

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/_update
{
   
"script" : "ctx._source.tags+=new_tag",
   
"params" : {
     
"new_tag" : "search"
   
}
}
update a document may not yet exist

POST /website/pageviews/1/_update
{
   
"script" : "ctx._source.views+=1",
   
"upsert": {
       
"views": 1
   
}
}
update and conflicts

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
Create a document only if the document does not already exist. See  Creating a New Document.
index
Create a new document or replace an existing document. See  Indexing a Document and Updating a Whole Document.
update
Do a partial update on a document. See  Partial Updates to Documents.
delete
Delete a document. See  Deleting a Document.
POST /website/_bulk
{ "index": { "_type": "log" }}
{ "event": "User logged in" }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值