Elasticsearch索引操作

一、索引初始化操作

插件推荐使用head、marvel (收费) 

1.1 创建新索引

curl -XPUT 'http://localhost:9200/test' -d '
{
    "settings":{
"index":{ "number_of_shards": 5, "number_of_replicas": 1 } } }
'

返回如下内容即为成功:

{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "test"
}

number_of_replicas可以替换为:

blocks.read_only : 设为true, 则当前索引只允许读,不允许写或更新;

blocks.read : 设为true , 则禁止读操作;

blocks.write : 设为true , 则禁止写操作;

blocks.metadata : 设为true , 则禁止对metadata操作。

 

1.2 查询索引的设置信息

curl -XGET 'http://localhost:9200/test/_settings?pretty'
curl -XGET 'http://localhost:9200/test,test2/_settings?pretty'
curl -XGET 'http://localhost:9200/_all/_settings?pretty'

1.3  创建文档

curl -XPUT 'http://localhost:9200/test/book/1' -d '
{
    "title": "ZQW Book",
    "name": {
        "first": "Z",
        "last": "QW"
     },
    "publish_date": "2017-11-20",
    "price": "39.99"
}    
'

不设置文档ID

curl -XPOST 'http://localhost:9200/test/book/' -d '
{
    "title": "No ID",
    "name": {
        "first": "Z",
        "last": "QW"
     },
    "publish_date": "2017-11-20",
    "price": "39.99"
}    
'

 

1.4 获取文档

curl -XGET 'http://localhost:9200/test/book/1'

 

1.5 通过 _source 获取指定字段

curl -XGET localhost:9200/test/book/1?_source=title
curl -XGET localhost:9200/test/book/1?_source=title,price
curl -XGET localhost:9200/test/book/1?_source

 

1.6 更新文档

curl -XPOST 'http://localhost:9200/test/book/1/_update' -d '
{
  "doc" : {
        "price" : "free"
    }  
}
'

 

1.7 删除文档

curl -XDELETE 'http://localhost:9200/test/book/1'
curl -XDELETE 'http://localhost:9200/test/book'
curl -XDELETE 'http://localhost:9200/test'

 

1.8 _mget 批量获取

curl -XPOST 'http://localhost:9200/_mget' -d '
#### docs 属组 ####
{ "docs": [ { "_index": "library", "_type": "book", "_id": 2 },
{
"_index": "library", "_type": "book", "_id": 3,
#####只获取指定数据#####
    "_source" : "price" },
{
"_index": "shop", "_type": "apple", "_id": 1 }
] }
'

 相同index,type下通过不同ID批量寻找文档

curl -XPOST 'http://localhost:9200/library/book/_mget' -d '
{
    "docs" : [
           {  "_id" : 1  },
           {  "_type" : "book", "_id" : 2  }
    ]
}
#####或者如下#####
{
  "ids": [    0,    1,    2,    3,    4  ]
}
'

 

1.9 bulk 批量操作

实现多个文档的 create、index、update 或 delete 操作

请求体格式(不能如同常规JSON格式一般美化展示):

{action:{metadata}}\n

{request body}\n

ex: { "delete" : { "_index" : "test","_type" : "type","_id" : "1" } }

curl -XPOST 'http://localhost:9200/test/type/_bulk' -d '
{ "index": {"_id": 0}}
{ "title" : "test00","exist": "true"}
{ "index": {"_id": 1}}
{ "title" : "test01","exist": "true"}
{ "index": {"_id": 2}}
{ "title" : "test02","exist": "true"}
{ "index": {"_id": 3}}
{ "title" : "test03","exist": "true"}
{ "index": {"_id": 4}}
{ "title" : "test04","exist": "true"}
'

批量操作:

curl -XPOST 'http://localhost:9200/test/type/_bulk?pretty' -d '
{"delete": {"_index": "test","_type": "type","_id" : "1"}}
{"create": {"_index": "meta","_type":"mtype","_id":"1"}}
{"title":"meta_type01","exist":"true"}
{"index":{"_index": "meta","_type":"mtype"}}
{"title":"meta_type02","exist":"true"}
{"update":{"_index":"test","_type":"type","_id":"2"}}
{"doc":{"exist":"false"}}
'

bulk处理文档大小的最佳值:

  • 数据加载在每个节点的RAM里
  • 请求的数据超过一定的大小,那bulk的 处理性能就回降低
  • 文档数据大小跟数据配置,文档复杂度,以及当前集群的负载有关

 

2.0 版本控制

  • 悲观锁:假定会发生并发冲突,屏蔽一切可能违反数据完整性的操作。(安全)
  • 乐观锁:假定不会发生并发冲突,只在提交操作时检查是否违反数据的完整性。

Elasticsearch 内置使用的是乐观锁:

  • 内部版本控制:每次修改后,_version自增长
  • 外部版本控制:手动修改_version
curl -XPUT 'http://localhost:9200/test/type/3?version=5&version_type=external' -d '
{
"title":"123"
}
'

 

posted @ 2017-11-20 17:54  瑶瑶仔  阅读(413)  评论(0)    收藏  举报