首页  

elasticsearch5.0入门之索引操作     所属分类 elasticsearch 浏览量 1432
根据原文翻译整理
https://www.elastic.co/guide/en/elasticsearch/reference/5.0/_cluster_health.html

cluster health check
HTTP/REST api 
 _cat API

 GET /_cat/health?v
 
 http://127.0.0.1:9200/_cat/health?v
 
 v verbose  显示列名
 
  green yellow  red
  
  Green means everything is good (cluster is fully functional), 
  yellow means all data is available but some replicas are not yet allocated (cluster is fully functional), 
  and red means some data is not available for whatever reason. 
  
  Note that even if a cluster is red, it still is partially functional 
  (it will continue to serve search requests from the available shards) 
  but you will likely need to fix it ASAP since you have missing data.
  
  yellow  一些副本没有分配
  red 一些数据不可用
  
  即使集群是红色的,它仍然具有部分功能(可用分片的搜索服务仍然可用),但是需要尽快修复它,因为丢失了数据。
  
  
  http://127.0.0.1:9200/_cat/nodes?v

List All Indices

http://127.0.0.1:9200/_cat/indices?v

Create an Index 
curl -X PUT http://127.0.0.1:9200/customer?pretty
重复创建索引返回错误 index_already_exists_exception

http://127.0.0.1:9200/_cat/indices?v

health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   customer GaZScgxaQyevhG00RsRzvw   5   1          0            0       650b           650b

have 1 index named customer and it has 5 primary shards and 1 replica (the defaults) and it contains 0 documents in it.
5个主分片一个副本 0个文档

索引状态是yellow, 因为只有一个节点,1个副本没有分配,
Once that replica gets allocated onto a second node, the health status for this index will turn to green.
一旦将该副本分配到第二个节点上,该索引的健康状态将变为绿色。

创建一个 0个副本的索引 
curl -X PUT http://127.0.0.1:9200/myindex01?pretty -d '{"settings":{"number_of_shards":1,"number_of_replicas":0}}'

health status index     uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   customer  GaZScgxaQyevhG00RsRzvw   5   1          0            0       650b           650b
green  open   myindex01 TAaTzqT2SYKkx1hR2FXWBg   1   0          0            0       130b           130b

myindex01 创建时指定了0个副本,所以状态为绿色

添加文档
curl -X PUT http://127.0.0.1:9200/customer/type1/1?pretty -d '{"name":"tiger"}'
type=type1  id=1    name=tiger
查看文档
http://127.0.0.1:9200/customer/type1/1?pretty

{
    "_index": "customer",
    "_type": "type1",
    "_id": "1",
    "_version": 1,
    "found": true,
    "_source": {
        "name": "tiger"
    }
}

更新文档 

Indexing/Replacing Documents
reindex

curl -X PUT http://127.0.0.1:9200/customer/type1/1?pretty -d '{"name":"tiger-new"}'
_version 变成 2

curl -X PUT http://127.0.0.1:9200/customer/type1/2?pretty -d '{"name":"dog"}'

不指定ID
curl -X POST http://127.0.0.1:9200/customer/type1?pretty -d '{"name":"cat"}'
注意使用 POST !!!

{
  "_index" : "customer",
  "_type" : "type1",
  "_id" : "AWhV0u0pt8zQvWGuVKCs",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "created" : true
}

Updating Documents

In addition to being able to index and replace documents, we can also update documents. 
Note though that Elasticsearch does not actually do in-place updates under the hood. 
Whenever we do an update, Elasticsearch deletes the old document 
and then indexes a new document with the update applied to it in one shot.
除了能够索引和替换文档外,还可以更新文档。请注意,实际上并没有在底层进行就地更新。
每当进行更新时,都会删除旧文档,重新索引新文档。


curl -X POST http://127.0.0.1:9200/customer/type1/1/_update?pretty -d '{"doc":{"name":"tiger-new2"}}'


curl -X POST http://127.0.0.1:9200/customer/type1/1/_update?pretty -d '{"doc":{"name":"tiger-new3","age":9}}'


age自动映射为数字 ,之后更新为 非数字 会报错 
curl -X POST http://127.0.0.1:9200/customer/type1/1/_update?pretty -d '{"doc":{"name":"tiger-new3","age":"abc"}}'

{
  "error" : {
    "root_cause" : [
      {
        "type" : "mapper_parsing_exception",
        "reason" : "failed to parse [age]"
      }
    ],
    "type" : "mapper_parsing_exception",
    "reason" : "failed to parse [age]",
    "caused_by" : {
      "type" : "number_format_exception",
      "reason" : "For input string: \"abc\""
    }
  },
  "status" : 400
}

Updates can also be performed by using simple scripts. 

使用脚本更新

curl -X POST http://127.0.0.1:9200/customer/type1/1/_update?pretty -d '{"script":"ctx._source.age += 7"}'

updates can only be performed on a single document at a time. 
只支持单个文档更新,暂不支持根据条件更新

Deleting Documents

DELETE /customer/type1/2?pretty

curl -X DELETE http://127.0.0.1:9200/customer/type1/2?pretty

Batch Processing
批量处理

POST /customer/type1/_bulk?pretty
{"index":{"_id":"5"}}
{"name": "name5" }
{"index":{"_id":"6"}}
{"name": "name6" }

POST /customer/type1/_bulk?pretty
{"update":{"_id":"5"}}
{"doc": { "name": "name5-new" } }
{"delete":{"_id":"6"}}


curl -X POST http://127.0.0.1:9200/customer/type1/_bulk?pretty -d '
{"index":{"_id":"5"}}
{"name": "name5" }
{"index":{"_id":"6"}}
{"name": "name6" }
'


curl -X POST http://127.0.0.1:9200/customer/type1/_bulk?pretty -d '
{"update":{"_id":"5"}}
{"doc": { "name": "name5-new" }}
{"delete":{"_id":"6"}}
'
       
Delete an Index 删除索引
DELETE /customer?pretty

curl -X DELETE http://127.0.0.1:9200/customer?pretty

上一篇     下一篇
KPI与KOR

elasticsearch5.0基本概念

elasticsearch5.0术语

elasticsearch中refresh和flush区别

elasticsearch5.0数据索引与查询实战

网站运营推广之七言绝句