ES[7.6.x] addition, deletion and modification of learning note data

In the previous sections, we learned index, field mapping, analyzer, etc., which are the basis of using ES, just like creating tables in database. After the basic work is done, we will really use it. In this section, we will see how to write data, modify data, and delete data into index. As for search, the main function of ES is to Search, so we will talk about the related functions of search later.

Document creation and update

The data in the index is called document, which is the same as a record in the data. The index is like a table in the database. We add data to the index, just like adding a record in the database table. Let's see how to add data to the index,

PUT /<index>/_doc/<_id>

POST /<index>/_doc/

PUT /<index>/_create/<_id>

POST /<index>/_create/<_id>

In this POST request, < index > is the name of the index, which is the same as inserting records into the database to know which table to insert. < index > can be followed by "Doc" or "create". What do these two mean? Let's see slowly that in addition to these two differences, there is also a request method, which is divided into POST and PUT. In general, is POST used for data insertion and user data modification? Let's try all four ways. First, let's look at the request of POST / < index > / _doc /,

POST /ik_index/_doc
{
  "id": 1,
  "title": "Apple",
  "desc": "The apples are delicious"
}

In this case, we use the ik_index created in the previous section to execute. Then we look up the index,

GET /ik_index/_search

The results are as follows:

{
    "took": 1000,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 1,
        "hits": [
            {
                "_index": "ik_index",
                "_type": "_doc",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "id": 1,
                    "title": "Watermelon in Panggezhuang, Daxing",
                    "desc": "The watermelons in Panggezhuang, Daxing are delicious. They are crispy, Sandy and sweet"
                }
            },
            {
                "_index": "ik_index",
                "_type": "_doc",
                "_id": "fEsN-HEBZl0Dh1ayKWZb",
                "_score": 1,
                "_source": {
                    "id": 1,
                    "title": "Apple",
                    "desc": "The apples are delicious"
                }
            }
        ]
    }
}

Let's focus on hits. This is the result we found. The first one is the data we saved in the previous section, regardless of it. Let's take a look at the second record. Notice the field "id". This "id" is the unique identification of this record in the index. In the request to insert data, we did not specify this id. ES automatically generated the fsn-hebzl0dh1aykwzb for us. So can we specify? want a go,

POST /ik_index/_doc/2
{
  "id": 1,
  "title": "Banana",
  "desc": "Bananas are delicious"
}

Pay attention to the request we sent. Add 2 after the "doc" to specify the id and execute it. Then query again. In the returned results, we only intercept the parts of hits, as follows:

"hits": [
            {
                "_index": "ik_index",
                "_type": "_doc",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "id": 1,
                    "title": "Watermelon in Panggezhuang, Daxing",
                    "desc": "The watermelons in Panggezhuang, Daxing are delicious. They are crispy, Sandy and sweet"
                }
            },
            {
                "_index": "ik_index",
                "_type": "_doc",
                "_id": "fEsN-HEBZl0Dh1ayKWZb",
                "_score": 1,
                "_source": {
                    "id": 1,
                    "title": "Apple",
                    "desc": "The apples are delicious"
                }
            },
            {
                "_index": "ik_index",
                "_type": "_doc",
                "_id": "2",
                "_score": 1,
                "_source": {
                    "id": 1,
                    "title": "Banana",
                    "desc": "Bananas are delicious"
                }
            }
        ]

We see the inserted banana record. Its ﹣ id is 2. So what happens when the id specified in the POST request exists in the index? Let's take another look,

POST /ik_index/_doc/1
{
  "id": 1,
  "title": "Banana",
  "desc": "Bananas are delicious"
}

For banana data, we specify that the data id=1, id=1 exists in the index. Let's perform the following query, and the returned result is as follows:

"hits": [
            {
                "_index": "ik_index",
                "_type": "_doc",
                "_id": "fEsN-HEBZl0Dh1ayKWZb",
                "_score": 1,
                "_source": {
                    "id": 1,
                    "title": "Apple",
                    "desc": "The apples are delicious"
                }
            },
            {
                "_index": "ik_index",
                "_type": "_doc",
                "_id": "2",
                "_score": 1,
                "_source": {
                    "id": 1,
                    "title": "Banana",
                    "desc": "Bananas are delicious"
                }
            },
            {
                "_index": "ik_index",
                "_type": "_doc",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "id": 1,
                    "title": "Banana",
                    "desc": "Bananas are delicious"
                }
            }
        ]

We can see that the previous data has been modified. Therefore, with regard to post / < index > / [Doc / < ID >, we can draw the conclusion as follows:

  • If it is not specified, ES will automatically generate the id for us;
  • When < u id > is specified, and the ID does not exist in the index, ES will add a piece of data with the specified ID;
  • When < u id > is specified, but the ID exists in the index, ES will update the data;

Next, let's take a look at the PUT request method of the doc method. First, we don't specify the id to see what the situation is. The request is as follows:

PUT /ik_index/_doc
{
  "id": 1,
  "title": "Grape",
  "desc": "Grapes are delicious"
}

Execute the following to return the following results:

{
    "error": "Incorrect HTTP method for uri [/ik_index/_doc] and method [PUT], allowed: [POST]",
    "status": 405
}

The error message says that our request is wrong. Let's use POST request. It seems that it's not possible to use PUT request without specifying id. Let's take a look at specifying a nonexistent id, as follows:

PUT /ik_index/_doc/3
{
  "id": 1,
  "title": "Grape",
  "desc": "Grapes are delicious"
}

Executed successfully. Check again,

"hits": [
            ......
            {
                "_index": "ik_index",
                "_type": "_doc",
                "_id": "3",
                "_score": 1,
                "_source": {
                    "id": 1,
                    "title": "Grape",
                    "desc": "Grapes are delicious"
                }
            }
        ]

Data added successfully. Let's see what it is like to specify an existing id. of course, it's modified. Let's try again,

PUT /ik_index/_doc/3
{
  "id": 1,
  "title": "A mandarin orange",
  "desc": "Oranges are delicious"
}

Executed successfully. Check again,

"hits": [
            ......
            {
                "_index": "ik_index",
                "_type": "_doc",
                "_id": "3",
                "_score": 1,
                "_source": {
                    "id": 1,
                    "title": "A mandarin orange",
                    "desc": "Oranges are delicious"
                }
            }
        ]

No problem, modification succeeded. Post / < index > / [Doc / < ID > is summarized as follows:

  • < u id > must be specified. If it is not specified, an error will be reported;
  • < u id > does not exist in the index, to add new data;
  • < u id > exists in the index. It is modified data;

_The POST and PUT of the doc request have been tried, and then look at the "create" request. First, look at the situation where the id is not specified, as follows:

POST /ik_index/_create
{
  "id": 1,
  "title": "Peach",
  "desc": "Peaches are delicious"
}

The error message returned is as follows:

{
    "error": {
        "root_cause": [
            {
                "type": "invalid_type_name_exception",
                "reason": "mapping type name [_create] can't start with '_' unless it is called [_doc]"
            }
        ],
        "type": "invalid_type_name_exception",
        "reason": "mapping type name [_create] can't start with '_' unless it is called [_doc]"
    },
    "status": 400
}

We won't explain the specific content. In a word, it's not allowed. Then add an index without id to try,

POST /ik_index/_create/4
{
  "id": 1,
  "title": "Peach",
  "desc": "Peaches are delicious"
}

The returned result is created successfully. The query is as follows:

"hits": [
            ......
            {
                "_index": "ik_index",
                "_type": "_doc",
                "_id": "4",
                "_score": 1,
                "_source": {
                    "id": 1,
                    "title": "Peach",
                    "desc": "Peaches are delicious"
                }
            }
        ]

What if the id exists in the index? Try again,

POST /ik_index/_create/3
{
  "id": 1,
  "title": "Peach",
  "desc": "Peaches are delicious"
}

Error returned:

{
    "error": {
        "root_cause": [
            {
                "type": "version_conflict_engine_exception",
                "reason": "[3]: version conflict, document already exists (current version [2])",
                "index_uuid": "W2X_riHIT4u678p8HZwnEg",
                "shard": "0",
                "index": "ik_index"
            }
        ],
        "type": "version_conflict_engine_exception",
        "reason": "[3]: version conflict, document already exists (current version [2])",
        "index_uuid": "W2X_riHIT4u678p8HZwnEg",
        "shard": "0",
        "index": "ik_index"
    },
    "status": 409
}

Roughly speaking, the data already exists, and new records can't be added. It seems that the "create" method is more strict. The summary is as follows:

  • id must be specified;
  • If the specified id exists in the index, an error is reported and the addition is unsuccessful;
  • The specified id does not exist in the index. It is added successfully and meets the expectation;

Take a look at the PUT of "create". It should be the opposite of POST, right? Let's try. First, we don't specify the id. try,

PUT /ik_index/_create
{
  "id": 1,
  "title": "pitaya",
  "desc": "The dragon is really delicious"
}

If you don't specify an id, you will not be able to return an error. If you don't specify an id, you won't be able to post the error information. Then you can specify an id that doesn't exist,

PUT /ik_index/_create/5
{
  "id": 1,
  "title": "pitaya",
  "desc": "The dragon is really delicious"
}

If the creation is successful, the query result will not be shown to you, and then an existing id will be changed, as follows:

PUT /ik_index/_create/4
{
  "id": 1,
  "title": "pitaya",
  "desc": "The dragon is really delicious"
}

The error message is returned as follows, which is the same as the POST request,

{
    "error": {
        "root_cause": [
            {
                "type": "version_conflict_engine_exception",
                "reason": "[4]: version conflict, document already exists (current version [1])",
                "index_uuid": "W2X_riHIT4u678p8HZwnEg",
                "shard": "0",
                "index": "ik_index"
            }
        ],
        "type": "version_conflict_engine_exception",
        "reason": "[4]: version conflict, document already exists (current version [1])",
        "index_uuid": "W2X_riHIT4u678p8HZwnEg",
        "shard": "0",
        "index": "ik_index"
    },
    "status": 409
}

We come to the following conclusion:

  • _create this form of POST and PUT are the same, there is no difference;
  • id must be specified;
  • The id must not exist in the index;

Deletion of Document

With addition, there must be deletion. The deletion method is very simple. The request format is as follows:

DELETE /<index>/_doc/<_id>

Send the delete request and specify the id of the data. Let's try to delete the data of huolongguo just added. Its id is 5. We send the request as follows:

DELETE /ik_index/_doc/5

The execution was successful and the data was deleted successfully.

Query Document by id

It is also very simple to query data according to id. you can complete the query by sending the following request,

GET <index>/_doc/<_id>

We need to specify the name of the index and the id of the data to be queried, as follows:

GET ik_index/_doc/3

The results are as follows:

{
    "_index": "ik_index",
    "_type": "_doc",
    "_id": "3",
    "_version": 2,
    "_seq_no": 5,
    "_primary_term": 3,
    "found": true,
    "_source": {
        "id": 1,
        "title": "A mandarin orange",
        "desc": "Oranges are delicious"
    }
}

According to the id, the result is found successfully.

OK ~ so far, the addition, deletion and modification of ES data are introduced. At the beginning of the next section, let's take a look at the core function of ES - Search.

Tags: Java Database

Posted on Sat, 09 May 2020 02:18:55 -0700 by sargus