2017-04-22 20 views
0

私のelasticsearchはバージョン2.4を使用しています。 以下のように私のJavaのコーディング:のみコーディング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); 
    } 
} 

私の現在のJSONを使用してelasticsearchにデータを挿入します。挿入後 は以下の通りであろう: [enter image description here] [NOが存在は、elasticsearchに挿入しない場合にのみ、新しいデータを更新存在する場合、elasticsearchからデータを存在チェックするためのJavaコードを使用する方法1

例: 新しいデータが入っていて、elasticsearchのTAGでIDをチェックすると、フィールドタグから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ドロップダウンから別のバージョンを選択してみてください)。既存のデータをチェックする限り、上記のコードは 'id'でチェックします。他のフィールドもチェックしたい場合は、[this](https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs-update)を見てください。 html#java-docs-update-api-upsert)の例です。 –

+0

私はどんなjarファイルを使用する必要がありますか? –

関連する問題