ES6.6.2 uses summary-document operations

Keywords: ElasticSearch Java Junit

1. Document basic operations are divided into: document acquisition, deletion, update. Here are some simple tests.

2. Assume that the current ES has the following data (two).

3. Document operation source code is as follows:

package com.bas.demo;

import com.bas.util.ESUtil;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

/**
 * Document management
 */
public class DocumentDemo {

    public static void main(String[] args) {
        System.out.println(ESUtil.selectDocument("app_account", "blog" , "d759rmkBsxnypktQHp5P"));
    }

    @Test
    public void deleteDoc() {
        ESUtil.deleteDocument("app_account", "blog" , "eL59rmkBsxnypktQIJ4I");
    }

    @Test
    public void updateDoc() {
        try {
            ESUtil.updateDocument("app_account", "blog" , "er6VrmkBsxnypktQsZ6I" ,
                    jsonBuilder().startObject()
                            .field("title","qqqqqqqqqqqqqq")
                            .endObject());
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public  void upsertDoc() throws IOException, ExecutionException, InterruptedException {
        ESUtil.upsertDocument("app_account", "blog" , "6J3HrmkBHcVcCKaQcmRP",
                    jsonBuilder().startObject()
                            .field("id","5")
                            .field("title","Title asd")
                            .field("content","asdhjjje")
                            .field("postdate","2019-03-24 17:38:00")
                            .field("url","bas")
                            .endObject(),
                    jsonBuilder().startObject()
                        .field("title","To update title")
                        .endObject()
                );
    }

}

Among them, the ES tool class is located in: ES6.6.2 Use Summary - Use of ES Tool Class 

It is noteworthy that the ID of the method parameter is the field "_id" in the document data.

4. The first step is to get the result graph of the document and execute Document.main().

5. Document delete result graph and execute Document.deleteDoc().

6. There are two kinds of document updates. First, execute Document.updateDoc() as shown in Figure 1:

The data in ES is updated as it is now:

Then upsert updates the type, that is, if the document does not exist in the type, insert it, update the data when it exists, and execute Document.upsertDoc(), as shown in the figure:

Because a document with _id of "7Z_qrmkBHcVcCKaQfGQ3" already exists in the type, the update operation is performed, as shown in the figure:

Similarly, if the parameter in Document.upsertDoc() is changed to this, the data will be inserted, as shown in Figure 1:

ES has added a new document, as shown in Figure:

Posted by roopurt18 on Wed, 27 Mar 2019 18:24:29 -0700