elasticsearch 基礎語句


1.  doucument id 的兩種生成方式

自動生成document id
自動生成的id,長度為20個字符,URL安全,base64編碼,GUID,分布式系統並行生成時不可能會發生沖突

POST /test_index/test_type (這里沒有標識id)
{
"test_content": "my test"
}

GET /test_index/test_type/_search


手動指定document id

PUT /test_index/test_type/2
{
"test_content":"ding-jiang"
}

GET /test_index/test_type/2

 

注意點:1 我們的{}不能和 PUT寫到一行 否則會報錯 failed to parse, document is empty
    2 GET /test_index/test_type/_search 會得到該節點下所有的數據列表
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
2.    doucument的_source元數據以及定制返回結果解析

put /test_index/test_type/1
{
"test":"test11",
"test1":"test22"
}
GET /test_index/test_type/1

返回
{
"_index": "test_index",
"_type": "test_type",
"_id": "1",
"_version": 2,
"found": true,
"_source": {
"test": "test11",
"test1": "test22"
}
}

_source元數據:就是創建document時我們存入的數據

如果我們不想返回全部的數據,只想返回一部分數據,比如有test 和test11 而我們只用test那么

GET /test_index/test_type/1?_source=test 則_source只返回
{
"_index": "test_index",
"_type": "test_type",
"_id": "1",
"_version": 2,
"found": true,
"_source": {
"test": "test11"
}
}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

3.    doucument的全量替換 強制創建和 delete 機制

1、document的全量替換
(1)語法與創建文檔是一樣的,如果document id不存在,那么就是創建;如果document id已經存在,那么就是全量替換操作,替換document的json串內容
(2)document是不可變的,如果要修改document的內容,第一種方式就是全量替換,直接對document重新建立索引,替換里面所有的內容
(3)es會將老的document標記為deleted,然后新增我們給定的一個document,當我們創建越來越多的document的時候,es會在適當的時機在后台自動刪除標記為deleted的document

就是說全量替換會將_source的內容替換,但是之前的內容並沒有從庫中刪除而是被標記為了deleted,es在恰當的時候會在動刪除這些deleted數據(document)
創建的標識就是GET /test_index/test_type/1 中的_version會加1
{
"_index": "test_index",
"_type": "test_type",
"_id": "1",
"_version": 3,
"found": true,
"_source": {
"test": "test11",
"test1": "test22",
"test_version": 3
}
}

2. 強制創建
(1)創建文檔與全量替換的語法是一樣的,有時我們只是想新建文檔,不想替換文檔,如果強制進行創建呢?
PUT /test_index/test_type/1/_create
{
"test333":"ding"
}
會報錯 因為id是不能重復的 如果我們想創建那么id必須修改
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[test_type][1]: version conflict, document already exists (current version [3])",
"index_uuid": "XtO4uL9HTo2v34qmximdLg",
"shard": "3",
"index": "test_index"
}
],
"type": "version_conflict_engine_exception",
"reason": "[test_type][1]: version conflict, document already exists (current version [3])",
"index_uuid": "XtO4uL9HTo2v34qmximdLg",
"shard": "3",
"index": "test_index"
},
"status": 409
}

3. 刪除

DELETE /test_index/test_type/11
GET /test_index/test_type/11

這個和全量替換一樣不會立即物理刪除,只會將其標記為deleted,當數據越來越多的時候,在后台自動刪除

 

 

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

4   partial update

什么是partial update?

PUT /index/type/id,創建文檔&替換文檔,一樣的語法

post /index/type/id/_update
{
"doc": {
"要修改的少數幾個field即可,不需要全量的數據"
}
}

 例如:

POST /test_index/test_type/8/_update
{
"doc":{
"test_field":"partial update",
"test2":"test22"
}
}

GET /test_index/test_type/8

{
"_index": "test_index",
"_type": "test_type",
"_id": "8",
"_version": 7,
"found": true,
"_source": {
"test_field": "partial update",
"test2": "test22"
}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM