實踐001-elasticsearch的index、create、update

niewj發表於2022-04-28

[toc]

一. 檢視叢集健康、節點、分片

1.檢視叢集健康度

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.檢視節點資訊

GET _cat/nodes

127.0.0.1 21 95 3 0.00 0.03 0.05 dilm * niewj

3. 檢視分片資訊

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                         

二. index、create、update文件

4. index一個文件

4.1 第一次index

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

結果:

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

再執行一次呢?

4.2 第一次index

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

輸出如下:(為了看出區別,加了個"author",減了個"price")

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

區別:

執行次數/區別第一次第二次
_version12
resultcreatedupdated
_seq_no01

執行完後,index查詢有沒有"price"呢?有就說明是更新了, 沒有就說明是覆蓋了:

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

看看:

{
  "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"
        }
      }
    ]
  }
}

沒price了!說明什麼?

  • index時,如果id已存在,則先刪除->再增加->_version+ _seq_no都會增加;
  • result 不同:第一次:created ; 第二次: updated

這裡雖然是updated,但並不是只修改了原來的欄位, 而是先刪,再建,昔有今無的欄位是沒的了!

就想一個商品房:新買的主人住進去一年,賣給第二個人,updated的是房間的狀態,原來的主人,基本不會在那兒了!

5. create一個文件

5.1 第1種create方式:PUT idx名/_create/id串

第1次create

PUT books/_create/3
{
"bookId": "3",
"bookName": "中國通史",
"author": "呂思勉"
}

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

第2次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
}

直接:"status": 409了;報錯了!

4xx的報錯一般都是告訴你輸入有問題

5.2 第2種create方式:POST idx名/_doc(自動生成ID)

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

連續執行了多次:

{
  "_index" : "books",
  "_type" : "_doc",
  "_id" : "OivOY4ABRxSL2QPxYNGs",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 10,
  "_primary_term" : 1
}
  • 每次_id都不一樣
  • _seq_no一直在增
因為每次都是存一個新文件了!查詢看看:
{
  "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" : "呂思勉"
        }
      }
    ]
  }
}

這裡就可以做一個小結:

6. index和create的區別

1.語法

index語法:PUT books/_doc/1

create語法:PUT books/_create/1 或者 POST books/_doc

2. 語義

index多次同一個id: 會刪除重建,但是版本一直++;

create的PUT books/_create/1 方式第二次執行會報錯,而不是++版本號!

create支援自動生成id:此時的多次執行就是索引多份document了;

7. update文件

update一個已有文件:

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

查詢看看:bookId並沒有丟失, 只是更新了賦值的欄位

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" : "魯迅"
        }
      }
    ]
  }
}

使用 GET books/_doc/NCvOY4ABRxSL2QPxNNHf可以看到版本:版本也發生了變化:update也會增加版本!

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

三.建立索引

建立索引的3點總結

  1. 當index或者create文件時,如果索引不存在,就會自動建立索引;
  2. 同時mapping和setting會使用預設的(也可以自定義模板,如果模板匹配,模板就會起作用,此時建立的索引的mapping和setting就會被index template定義的內容約束)
  3. 預設mapping是dynamic=true的,就是有新增的欄位,自動識別猜測型別;dynamic=false/strict時,就不會索引新的欄位,區別是:strict直接報錯不讓增加新欄位;false可以新增,_source裡也會存入,但是欄位不會被分詞和生成索引,作為搜尋條件去查詢是也會報錯!

四.刪除索引

DELETE books

刪除後再去查: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
}

相關文章