ES學習筆記1-基本操作篇

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 andUpdating 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" }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章