elasticsearch Java API 索引API
生活随笔
收集整理的這篇文章主要介紹了
elasticsearch Java API 索引API
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
對于索引API,總得來說,就是三步:
1、一個構建JSON格式的doc(文檔);
2、調用API來創建文件即可(此處將JSON傳入)。
3、查看結果。
分步說明:
1、一個構建JSON格式的doc(文檔)
構建JSON的字符串的方式有很多種,也有很多第三方的插件,這里就不再累贅。
此處介紹elasticsearch本身的創建文檔的方式:
import?static?org.elasticsearch.common.xcontent.XContentFactory.*;XContentBuilder?builder?=?jsonBuilder().startObject().field("user",?"kimchy").field("postDate",?new?Date()).field("message",?"trying?out?Elasticsearch").endObject()String?json?=?builder.string();2、調用API來創建文件即可(此處將JSON傳入)
import?static?org.elasticsearch.common.xcontent.XContentFactory.*;IndexResponse?response?=?client.prepareIndex("twitter",?"tweet",?"1").setSource(jsonBuilder().startObject().field("user",?"kimchy").field("postDate",?new?Date()).field("message",?"trying?out?Elasticsearch").endObject()).execute().actionGet(); String?json?=?"{"?+"\"user\":\"kimchy\","?+"\"postDate\":\"2013-01-30\","?+"\"message\":\"trying?out?Elasticsearch\""?+"}";IndexResponse?response?=?client.prepareIndex("twitter",?"tweet").setSource(json).execute().actionGet();3、查看結果
//?Index?name?索引名稱 String?_index?=?response.getIndex(); //?Type?name?類型名稱 String?_type?=?response.getType(); //?Document?ID?(generated?or?not)?文檔ID。可以在創建時自定義 String?_id?=?response.getId(); //?Version?(if?it's?the?first?time?you?index?this?document,?you?will?get:?1)?版本號 long?_version?=?response.getVersion();? //?isCreated()?is?true?if?the?document?is?a?new?one,?false?if?it?has?been?updated?可以看是否創建或者是被更新 boolean?created?=?response.isCreated();多線程
默認情況下,operation threaded設置為true,這意味著運行在不同的線程上執行。
轉載于:https://my.oschina.net/claireliu/blog/464233
總結
以上是生活随笔為你收集整理的elasticsearch Java API 索引API的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: footer固定到底部
- 下一篇: sed基本用法