Operation 003 documents

A. Add a document

In the section above, we removed the user indexes.

Now we execute the following command:

PUT /user/_doc/1
{
  "username" : "trek",
  "age" : 27
}

 The following results were obtained 

{
  "_index" : "user",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

  We can get from among the result, we have successfully added a document.

We continue to query the index:

GET /user

  You can obtain the following results:

{
  "user" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "age" : {
          "type" : "long"
        },
        "username" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1569423964719",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "_7BZkTRISAm7zzCo5B17ow",
        "version" : {
          "created" : "7030299"
        },
        "provided_name" : "user"
      }
    }
  }
}

  We found that when we add a record, but also created the index.

This is the characteristic nosql database, it is always nothing specific requirements.

 

Two. Id document

In the above, we add a document, and to develop a document id is 1.

If we do not specify, es will help us to automatically generate a document id.

as follows:

POST /user/_doc
{
  "username":"tom",
  "age" : 33
}

  The results obtained were as follows:

{
  "_index" : "user",
  "_type" : "_doc",
  "_id" : "6pr4aG0BsjQtAa68ty9w",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}

  We found that we created es to a random id value.

In addition, we can get such a conclusion.

In fact, the nature of the request is put updated, as described above.

It is the new post on the nature of the request. When we do not specify the id when, es will perform the new operation.

If id is specified, es processing logic is updated.

Es but is not allowed to update the contents of the document, its logic is to first remove and then add a new document.

As we document id = 1 does not exist before, so we know es intention is actually new.

Here we are talking about the difference between the post and put in the back is the same.

If you do not analyze these, it is easy to think of inconsistent put a new operation, and general restful style.

 

Four. The total amount of update

When updating the content, we need to specify the id.

as follows:

PUT /user/_doc/1
{
  "username":"tim"
}

  The results obtained were as follows:

{
  "_index" : "user",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 2,
  "_primary_term" : 1
}

  We need to note that version of the content now it becomes 2, which is the mechanism by optimism, where we do not say in detail.

We go to query the contents of the document id = 1.

GET /user/_doc/1

  Obtain the following content, we are concerned about the contents of _source.

{
  "_index" : "user",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 2,
  "_seq_no" : 2,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "username" : "tim"
  }
}

  We are surprised that we had wanted to update username, but es not only help us update the field, and removed the age field.

Causes it?

And our content described above is actually consistent, es the document does not update the operation, only after deleting the new features first.

Our command above, is essentially is the case, therefore, the new document did not age field that is missing.

In many cases, we do not want such an effect occurs.

 

IV. --Partial update partial update operations.

See the following command:

We first create the following documents:

PUT /user/_doc/3
{
  "username":"john",
  "age":33
}

  Use the following command to update:

POST /user/_update/3
{
  "doc":{
      "username":"john",
      "age":34
  }

}

  Local update operation we use, this operation will update local content.

 

V. delete documents

DELETE /user/_doc/1

  We can send a request to delete the right to delete documents.

 

Guess you like

Origin www.cnblogs.com/trekxu/p/11588367.html