简体   繁体   中英

how to check exist data at elasticsearch and insert or update the new data

my elasticsearch is using version 2.4. my java coding as below:

 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);
    }
}

my current coding only insert the data into elasticsearch using json. after insert will be as below: [ 在此处输入图片说明 ][1

how to use java coding to check exist data from elasticsearch, if exist only update the new data, if no exist, insert into elasticsearch.

for example: when new data in, and check id at TAG from elasticsearch, once it get the id from field tag, it only update of field stkno.

Please kindly advice, if ok, got sample java coding to refer.

Thanks

You can use Elasticsearch's Update API (example here ) and pass the document id along with payload to update the content.

Assuming the documents have unique id, you can do the following:

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM