最近使用一套數據加工中間工具,查看es操作中的update操作。其中方法命名為updateOrInsert。但是沒發現代碼中有ES的insert方法調用。於是仔細分析了代碼邏輯。
經過一路追溯,直至ES java客戶端請求發送代碼。沒找到insert相關內容。
於是到官網查看究竟,可官網對 java Client相關說明比較少。查看不到具體api的說明。於是回到代碼調用處:
String jsonText = jsonBuild.endObject().string(); UpdateRequest request = (UpdateRequest)esClient.prepareUpdate(xxx.getDatabase(), xxx.getTable(), docId).setDoc(jsonText).setDetectNoop(true).setDocAsUpsert(true).setRetryOnConflict(this.retryOnConflict).request(); esClient.update(request).get();
代碼中屬於鏈式調用,由於太長沒有換行,竟然沒看到后邊的setDetectNoop,setDocAsUpsert參數的調用,於是思考,javaClient只是封裝和轉換了調用請求,於是再回到官網查看Document APIs,找到update操作的說明,就有了下邊關於 Detecting Noop Updates 以及 Upserts說明:
Detecting noop updatesedit
If
doc
is specified its value is merged with the existing_source
. By default the document is only reindexed if the new_source
field differs from the old. Settingdetect_noop
tofalse
will cause Elasticsearch to always update the document even if it hasn’t changed. For example:curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{ "doc" : { "name" : "new_name" }, "detect_noop": false }'
上邊這段的意思是當更新的文檔發生變化時進行更新,如果為fasle,則始終更新。
Upsertsedit
If the document does not already exist, the contents of the
upsert
element will be inserted as a new document. If the document does exist, then thescript
will be executed instead:curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{ "script" : { "inline": "ctx._source.counter += count", "params" : { "count" : 4 } }, "upsert" : { "counter" : 1 } }'
scripted_upsert
editIf you would like your script to run regardless of whether the document exists or not — i.e. the script handles initializing the document instead of the
upsert
element — then setscripted_upsert
totrue
:curl -XPOST 'localhost:9200/sessions/session/dh3sgudg8gsrgl/_update' -d '{ "scripted_upsert":true, "script" : { "id": "my_web_session_summariser", "params" : { "pageViewEvent" : { "url":"foo.com/bar", "response":404, "time":"2014-01-01 12:32" } } }, "upsert" : {} }'
doc_as_upsert
editInstead of sending a partial
doc
plus anupsert
doc, settingdoc_as_upsert
totrue
will use the contents ofdoc
as theupsert
value:curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{ "doc" : { "name" : "new_name" }, "doc_as_upsert" : true }'
上邊描述:upsert參數的使用,有三種方式:
- 指定upsert內容
- 指定打開腳本upsert開關使用腳本處理upsert
- 使用文檔內容做為upsert參數,則打開 doc_as_upsert。顯然我們上邊所說的javaClient調用中就是使用的 doc_as_upsert,這樣當文檔不存在時候,就會將傳遞過來的文檔內容insert進去。達到update or Insert 目的。
因此,對於ES java Clent使用不熟的完全可以參照 api命名查找官網的 api說明,java客戶端是用java語言對其進行了封裝。仔細閱讀便知道調用代碼邏輯的含義了。僅此記錄,為不熟悉ES的其他使用者 引個路子。
官網內容鏈接:https://www.elastic.co/guide/en/elasticsearch/reference/2.1/docs-update.html#upserts
可以根據自己使用的ES版本進行切換查看。