ElasticSearch document operations

Keywords: MongoDB

In this section, we will explain the operation of documents in ES

I. index / update documents

Index and update documents in ES are PUT operations.

PUT /{IndexName}/{TypeName}/{ID}

For example, another test document is as follows:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "user",
        "_type" : "introduce",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "jack",
          "age" : 20,
          "gender" : "male"
        }
      }
    ]
  }
}

Index a new document

PUT /user/introduce/2
{
   "name":"lucy",
   "age":18,
   "gender":"female"
}

Perform get / user / introduction / "search? Pretty to view the document

......
{
  "_index" : "user",
  "_type" : "introduce",
  "_id" : "2",
  "_score" : 1.0,
  "_source" : {
    "name" : "lucy",
    "age" : 18,
    "gender" : "female"
  }
},
{
  "_index" : "user",
  "_type" : "introduce",
  "_id" : "1",
  "_score" : 1.0,
  "_source" : {
    "name" : "jack",
    "age" : 20,
    "gender" : "male"
  }
}
......

At this time, a new document is inserted and indexed. Next, let's update a document to try

PUT /user/introduce/1
{
   "name":"jone"
}
//Return
{
  "_index" : "user",
  "_type" : "introduce",
  "_id" : "1",
  "_version" : 4,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 3,
  "_primary_term" : 1
}

Notice that the result changes to updated

Perform get / user / introduction / "search? Pretty again to view the document

........
"hits" : [
      {
        "_index" : "user",
        "_type" : "introduce",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "lucy",
          "age" : 18,
          "gender" : "female"
        }
      },
      {
        "_index" : "user",
        "_type" : "introduce",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "jone"
        }
      }
    ]
.......

Notice that there is no such field as "name" in the document with "id 1", neither age nor gender. This means that PUT is an update to the whole document rather than one or several fields in the document

Document local field update POST

I restore the data first, and then update some fields

POST /{IndexName}/{TypeName}/{ID}/_update
{
    "doc":{}
}
POST /user/introduce/1/_update
{
   "doc":{
      "name":"jone",
      "age":22
   }
}

POST can also create a document

POST /user/introduce?pretty
{
  "age":20
}

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

The system will automatically assign ﹣ id, which is the same as MongoDB

2. Delete documents

DELETE /{IndexName}/{TypeName}/{ID}?pretty

Posted by rosenrosen on Sun, 08 Dec 2019 19:10:12 -0800