2017-04-22 428 views
0

我的elasticsearch使用的是版本2.4。 我的Java编码如下:使用JSON如何在elasticsearch中检查现有数据并插入或更新新数据

import com.google.gson.Gson; 
import com.google.gson.GsonBuilder; 
public void generateJsonObject(NewsContentObj newsContentObj, String sNewsID) { 

    try { 
     Gson gson = new GsonBuilder().disableHtmlEscaping().create(); 

     if (sExDate.equalsIgnoreCase("1900-01-01")) { 
      sExDate = ""; 
     } 

     if (sPaymnetDate.equalsIgnoreCase("1900-01-01")) { 
      sPaymnetDate = ""; 
     } 
     newsContentObj.setExDate(sExDate); 
     newsContentObj.setPaymentDate(sPaymnetDate); 

     String Json = gson.toJson(newsContentObj); 

     out.println("ElasticSearch sNewsID :" + sNewsID); 
     out.println("JSON :" + Json); 

     sendIndexer(sNewsID, Json); 
    } catch (Exception ex) { 
     out.println("News Id :" + sNewsID + " -> Exception :" + ex); 
     ex.printStackTrace(); 
    } 
} 

// Sharon 20161011 elastic search 
private void sendIndexer(String nid, String json) { 
    try { 
     String url = indexserver + nid; 
     StringEntity reqEntity = new StringEntity(json, "application/json", "UTF8"); 

     HttpPost post = new HttpPost(url); 

     post.setEntity(reqEntity); 

     CloseableHttpClient httpclient = HttpClients.createDefault(); 

     CloseableHttpResponse res = httpclient.execute(post); 

     // Issue to solve: if sleep is not applied, 
     // JQC will be too quick to respond and call back ES causing blank data as ES had not finish index new data 
     // below is just temp fix, most likely need migrate to use ES API to get actual push index success 
     // Thread.sleep(5000); 

     // Debug purpose 
     // out.println("Send Indexer status: " + res.getStatusLine()); 

    } catch (UnsupportedEncodingException uee) { 
     out.println("Send Indexer encoding exception: This should not happen unless hardcoded item being changed!"); 
    } catch (ClientProtocolException cpe) { 
     out.println("Send Indexer CPE exception: " + cpe); 
    } catch (IOException ioe) { 
     out.println("Send Indexer IO exception: " + ioe); 
    } 
} 

我目前唯一的编码中插入数据elasticsearch。 插入之后将是如下: [enter image description here] [1

如何用Java编码来检查从elasticsearch存在数据,如果存在仅更新新的数据,如果没有存在,插入elasticsearch。

例如: 当新数据进入时,并且在elasticsearch的TAG处检查id,一旦它从field标记中获得id,它只会更新字段stkno。

敬请建议,如果正常,得到了样品Java代码来指代。

感谢

回答

0

您可以使用Elasticsearch的Update API(例如here),并通过该文件ID与有效载荷,以更新的内容一起。

假设文件都唯一的ID,你可以做到以下几点:

UpdateRequest updateRequest = new UpdateRequest(); 
updateRequest.docAsUpsert(true); 
updateRequest.index(index); 
updateRequest.type(type); 
updateRequest.id(id); 
updateRequest.doc(json); 

UpdateResponse response = client.update(updateRequest).actionGet(); 
if (response.isCreated()) { 
//Created a new document 
}else{ 
//Updated an existing document 
} 
+0

意味着我需要使用5.3 elasticsearch正好可以利用这个编码?如何检查现有数据? –

+0

您不需要为此使用elasticsearch 5.3,更新API适用于任何版本(尝试从RHS drodown中选择不同的版本)。就检查现有数据而言,上述代码通过'id'进行检查。如果你想检查其他字段,请查看[this](https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs-update)。 html#java-docs-update-api-upsert)的例子。 –

+0

我需要使用什么jar文件? –

相关问题