[toc]

1. View cluster health, nodes, and shards

1. Check the cluster health

GET _cluster/health

 {
  "cluster_name" : "elasticsearch",
  "status" : "yellow",
  "timed_out" : false,
  "number_of_nodes" : 1,
  "number_of_data_nodes" : 1,
  "active_primary_shards" : 8,
  "active_shards" : 8,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 3,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0,
  "task_max_waiting_in_queue_millis" : 0,
  "active_shards_percent_as_number" : 72.72727272727273
}

2. View node information

GET _cat/nodes

 127.0.0.1 21 95 3 0.00 0.03 0.05 dilm * niewj

3. View shard information

GET _cat/shards

 .security-7                0 p STARTED       42  80.1kb 127.0.0.1 niewj
.apm-agent-configuration   0 p STARTED        0    283b 127.0.0.1 niewj
users                      0 p STARTED        4   5.3kb 127.0.0.1 niewj
users                      0 r UNASSIGNED                         
movies                     0 p STARTED     9743   1.3mb 127.0.0.1 niewj
movies                     0 r UNASSIGNED                         
.kibana_1                  0 p STARTED       88 101.9kb 127.0.0.1 niewj
kibana_sample_data_flights 0 p STARTED    13059   6.3mb 127.0.0.1 niewj
.kibana_task_manager_1     0 p STARTED        2  12.5kb 127.0.0.1 niewj
my_index                   0 p STARTED        2    10kb 127.0.0.1 niewj
my_index                   0 r UNASSIGNED

2. index, create, update documents

4. index a document

4.1 The first index

 PUT books/_doc/1
{
  "bookId":"1",
  "bookName":"Thinking in Java",
  "price": 99.99
}

result:

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

Do it again?

4.2 The first index

 PUT books/_doc/1
{
  "bookId":"1",
  "bookName":"Thinking in Java",
  "author": "Bruce Eckel"
}

The output is as follows: (In order to see the difference, "author" is added and "price" is subtracted)

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

the difference:

Number of executions/difference first the second time
_version 1 2
result created updated
_seq_no 0 1

After the execution, does the index query have "price"? If there is, it means that it is updated, if not, it means that it is overwritten:

 GET books/_search
{
  "query": {"match_all": {}}
}

have a look:

 {
  "took" : 1002,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "books",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "bookId" : "1",
          "bookName" : "Thinking in Java",
          "author" : "Bruce Eckel"
        }
      }
    ]
  }
}

No more price! Explain what?

  • When indexing, if the id already exists, delete it first -> then add it -> _version + _seq_no will increase;
  • result different: first time: created ; second time: updated

Although it is updated here, it does not only modify the original field, but deletes it first, and then builds it again. The fields that existed in the past are gone!

Just think of a commercial house: the newly bought owner lives in for a year and sells it to a second person. The updated status is the state of the room. The original owner is basically not there anymore!

5. Create a document

5.1 The first create method: PUT idx name/_create/id string

1st create

PUT books/_create/3
{
"bookId": "3",
"bookName": "General History of China",
"author": "Lu Simian"
}

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

2nd create

 {
  "error": {
    "root_cause": [
      {
        "type": "version_conflict_engine_exception",
        "reason": "[3]: version conflict, document already exists (current version [1])",
        "index_uuid": "64ki-Bg-SkeC6mTRY-7AnA",
        "shard": "0",
        "index": "books"
      }
    ],
    "type": "version_conflict_engine_exception",
    "reason": "[3]: version conflict, document already exists (current version [1])",
    "index_uuid": "64ki-Bg-SkeC6mTRY-7AnA",
    "shard": "0",
    "index": "books"
  },
  "status": 409
}

Direct: "status": 409; an error is reported!

4xx errors usually tell you that there is a problem with the input

5.2 The second create method: POST idx name/_doc (automatically generated ID)

 POST books/_doc
{
  "bookId": "4",
  "bookName": "中国通史4",
  "author": "吕思勉"
}

Executed multiple times in a row:

 {
  "_index" : "books",
  "_type" : "_doc",
  "_id" : "OivOY4ABRxSL2QPxYNGs",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 10,
  "_primary_term" : 1
}
  • _id is different every time
  • _seq_no keeps increasing
Because every time a new document is saved! Check out:
 {
  "took" : 627,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "books",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "bookId" : "1",
          "bookName" : "Thinking in Java",
          "author" : "Bruce Eckel"
        }
      },
      {
        "_index" : "books",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "bookId" : "2",
          "bookName" : "中国通史",
          "author" : "吕思勉"
        }
      },
      {
        "_index" : "books",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "bookId" : "3",
          "bookName" : "中国通史",
          "author" : "吕思勉"
        }
      },
      {
        "_index" : "books",
        "_type" : "_doc",
        "_id" : "NCvOY4ABRxSL2QPxNNHf",
        "_score" : 1.0,
        "_source" : {
          "bookId" : "4",
          "bookName" : "中国通史4",
          "author" : "吕思勉"
        }
      },
      {
        "_index" : "books",
        "_type" : "_doc",
        "_id" : "NSvOY4ABRxSL2QPxPdEy",
        "_score" : 1.0,
        "_source" : {
          "bookId" : "4",
          "bookName" : "中国通史4",
          "author" : "吕思勉"
        }
      },
      {
        "_index" : "books",
        "_type" : "_doc",
        "_id" : "NivOY4ABRxSL2QPxQdEo",
        "_score" : 1.0,
        "_source" : {
          "bookId" : "4",
          "bookName" : "中国通史4",
          "author" : "吕思勉"
        }
      },
      {
        "_index" : "books",
        "_type" : "_doc",
        "_id" : "NyvOY4ABRxSL2QPxRNHC",
        "_score" : 1.0,
        "_source" : {
          "bookId" : "4",
          "bookName" : "中国通史4",
          "author" : "吕思勉"
        }
      },
      {
        "_index" : "books",
        "_type" : "_doc",
        "_id" : "OCvOY4ABRxSL2QPxSdHA",
        "_score" : 1.0,
        "_source" : {
          "bookId" : "4",
          "bookName" : "中国通史4",
          "author" : "吕思勉"
        }
      },
      {
        "_index" : "books",
        "_type" : "_doc",
        "_id" : "OSvOY4ABRxSL2QPxTNHb",
        "_score" : 1.0,
        "_source" : {
          "bookId" : "4",
          "bookName" : "中国通史4",
          "author" : "吕思勉"
        }
      },
      {
        "_index" : "books",
        "_type" : "_doc",
        "_id" : "OivOY4ABRxSL2QPxYNGs",
        "_score" : 1.0,
        "_source" : {
          "bookId" : "4",
          "bookName" : "中国通史4",
          "author" : "吕思勉"
        }
      }
    ]
  }
}

Here is a summary:

6. The difference between index and create

1. Grammar

Index syntax: PUT books/_doc/1

create syntax: PUT books/_create/1 or POST books/_doc

2. Semantics

Index with the same id multiple times: It will be deleted and rebuilt, but the version has always been ++;

The PUT books/_create/1 method of create will report an error for the second execution, not the ++ version number!

create supports automatic id generation: multiple executions at this time are indexing multiple documents;

7. Update documentation

update an existing document:

 POST books/_update/NCvOY4ABRxSL2QPxNNHf
{
  "doc": {
    "bookName": "鲁迅散文",
    "author": "鲁迅"
  }
}

Look at the query: bookId is not lost, just updated the assigned field

GET books/_search?q=_id:NCvOY4ABRxSL2QPxNNHf

 {
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "books",
        "_type" : "_doc",
        "_id" : "NCvOY4ABRxSL2QPxNNHf",
        "_score" : 1.0,
        "_source" : {
          "bookId" : "4",
          "bookName" : "鲁迅散文",
          "author" : "鲁迅"
        }
      }
    ]
  }
}

Use GET books/_doc/NCvOY4ABRxSL2QPxNNHf to see the version: the version has also changed: update will also increase the version!

 {
  "_index" : "books",
  "_type" : "_doc",
  "_id" : "NCvOY4ABRxSL2QPxNNHf",
  "_version" : 2,
  "_seq_no" : 11,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "bookId" : "4",
    "bookName" : "鲁迅散文",
    "author" : "鲁迅"
  }
}

3. Create an index

3-point summary of index creation

  1. When indexing or creating a document, if the index does not exist, the index will be automatically created;
  2. At the same time, the default mapping and setting will be used (you can also customize the template. If the template matches, the template will work, and the mapping and setting of the created index will be constrained by the content defined by the index template)
  3. The default mapping is dynamic=true, that is, there are new fields, and the guess type is automatically recognized; when dynamic=false/strict, new fields will not be indexed. The difference is: strict reports an error directly and does not allow new fields to be added; false can Increase, it will also be stored in _source, but the field will not be segmented and indexed, and an error will be reported when querying as a search condition!

4. Delete the index

 DELETE books

Delete and then check: GET books/_doc/1

 {
  "error" : {
    "root_cause" : [
      {
        "type" : "index_not_found_exception",
        "reason" : "no such index [books1]",
        "resource.type" : "index_expression",
        "resource.id" : "books1",
        "index_uuid" : "_na_",
        "index" : "books1"
      }
    ],
    "type" : "index_not_found_exception",
    "reason" : "no such index [books1]",
    "resource.type" : "index_expression",
    "resource.id" : "books1",
    "index_uuid" : "_na_",
    "index" : "books1"
  },
  "status" : 404
}

丰木
322 声望19 粉丝

遇见超乎想象的自己!