关于elasticsearch:elasticsearch的indexcreateupdate

254次阅读

共计 6248 个字符,预计需要花费 16 分钟才能阅读完成。

[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
}

区别:

执行次数 / 区别 第一次 第二次
_version 1 2
result created updated
_seq_no 0 1

执行完后,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
}

正文完
 0