能夠經過json工具把java對象轉換成json字符串進行保存,也能夠經過內置的幫助類直接構建json格式java
/** * 獲取客戶端 * * @return */ public static TransportClient getClient() { Settings settings = Settings.builder().put("cluster.name", "es_cluster").build(); TransportClient client = null; try { client = new PreBuiltTransportClient(settings) .addTransportAddress(new TransportAddress(InetAddress.getByName("10.118.213.215"), 9300)); } catch (UnknownHostException e) { e.printStackTrace(); } return client; } /** * 保存文檔到索引中 * * @param indexName * @param typeName * @param id * @param docJson */ public static void insertDoc(String indexName, String typeName, String id, String docJson) { IndexResponse response = getClient().prepareIndex(indexName, typeName, id).setSource(docJson, XContentType.JSON) .get(); System.out.println(response.status()); }
測試保存json
public static void main(String[] args) throws Exception { // 時間格式轉換 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); // 保存數據 Telegraph telegraph = new Telegraph(); telegraph.setAuthor("財政部部長"); telegraph.setTitle("中方正在加快推動各項開放措施的落地"); telegraph.setContent("財聯社7月23日訊,財政部部長劉昆指出,中方正在加快推動各項開放措施的落地。中國開放的大門將越開越大,不只促進中國高質量發展,也爲全球經濟增加提供重要動力"); telegraph.setPubdate(formatter.format(new Date())); // 對象轉換爲json字符串 String json = JSONObject.toJSONString(telegraph); System.out.println(json); insertDoc("telegraph", "msg", "1", json); // 使用內置幫助類XContentFactory構造json格式文檔 XContentBuilder builder = XContentFactory.jsonBuilder().startObject().field("title", "長生生物官網被黑客攻擊") .field("author", "長生生物") .field("content", "財聯社7月23日訊,長生科技官網首頁被黑客攻擊,並配圖「不搞你,對不起祖國的花朵」。打開長生生物的官網,顯示網站已沒法打開,錯誤提示404,截至發稿,網站還沒有恢復") .field("pubdate", formatter.format(new Date())).endObject(); System.out.println(builder.string()); insertDoc("telegraph", "msg", "2", builder.string()); }
根據文檔索引、類型、id獲取文檔內容工具
/** * 獲取文檔 * @param indexName * @param typeName * @param id * @return */ public static String getIndexDoc(String indexName, String typeName, String id) { GetResponse response = getClient().prepareGet(indexName, typeName, id).get(); System.out.println(response.getVersion()); return response.getSourceAsString(); }
測試獲取文檔測試
public static void main(String[] args) { String source = getIndexDoc("telegraph", "msg", "2"); System.out.println(source); }
/** * 刪除文檔 * @param indexName * @param typeName * @param id */ public static void deleteDoc(String indexName,String typeName,String id){ DeleteResponse response = getClient().prepareDelete(indexName, typeName, id).get(); System.out.println(response.status()); }
測試網站
public static void main(String[] args) { deleteDoc("telegraph", "msg", "1"); }
/** * 更新文檔內容 * * @param indexName * @param typeName * @param id * @param fields * @throws Exception */ public static void updateDoc(String indexName, String typeName, String id, Map<String, Object> fields) throws Exception { UpdateRequest request = new UpdateRequest(); XContentBuilder builder = XContentFactory.jsonBuilder().startObject(); for (String key : fields.keySet()) { builder.field(key, fields.get(key)); } builder.endObject(); request.index(indexName).type(typeName).id(id).doc(builder); UpdateResponse response = getClient().update(request).get(); System.out.println(response.toString()); }
測試ui
public static void main(String[] args) { Map<String, Object> fields = new HashMap<String, Object>(); fields.put("title", "長生生物涉案狂犬病疫苗被指未做動物實驗檢測"); try { updateDoc("telegraph", "msg", "2", fields); } catch (Exception e) { e.printStackTrace(); } }
/** * 若是更新內容不存在,建立文檔 * * @param indexName * @param typeName * @param id * @param indexDoc 添加內容 * @param updateField 更新內容 * @throws Exception */ public static void upset(String indexName, String typeName, String id, Map<String, Object> indexDoc, Map<String, Object> updateField) throws Exception { // 建立新增request IndexRequest indexRequest = new IndexRequest(indexName, typeName, id); XContentBuilder indexBuilder = XContentFactory.jsonBuilder().startObject(); for (String key : indexDoc.keySet()) { indexBuilder.field(key, indexDoc.get(key)); } indexBuilder.endObject(); System.out.println(indexBuilder.string()); indexRequest.source(indexBuilder); // 建立更新request UpdateRequest updateRequest = new UpdateRequest(indexName, typeName, id); XContentBuilder updateBuilder = XContentFactory.jsonBuilder().startObject(); for (String key : updateField.keySet()) { updateBuilder.field(key, updateField.get(key)); } updateBuilder.endObject(); System.out.println(updateBuilder.string()); updateRequest.doc(updateBuilder).upsert(indexRequest); UpdateResponse response = getClient().update(updateRequest).get(); System.out.println(response.toString()); }
測試spa
public static void main(String[] args) throws Exception{ //若是存在就更新 Map<String, Object> fields = new HashMap<String, Object>(); fields.put("title", "被更新以後title"); //若是不存在就新增 Map<String, Object> indexDoc = new HashMap<String, Object>(); indexDoc.put("title","第一次測試添加title"); indexDoc.put("content", "測試添加內容"); upset("telegraph","msg","2",indexDoc,fields); }