添加
类型为employee,该类型位于索引megacorg,每个雇员索引一个文档,该文档包含该雇员的全部信息(面向文档),该雇员的id为1
需要index、type、id
curl -X PUT -H 'Content-Type: application/json' -i http://focuson1:9200/megacorp/employee/1 --data '{
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}'
添加更多的雇员
curl -X PUT -H 'Content-Type: application/json' -i http://focuson1:9200/megacorp/employee/2 --data '{
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests": [ "music" ]
}'
增加索引的时候,默认会有5个主分片,主分片是在创建索引的时候就要固定的,而副本分片个数随时可修改,比如,创建一个主分片为3,副本分片为1的索引。当往es中put数据时,会按照id进行hash,然后put到对应的分片上。
[root@focuson1 ~]# curl -X PUT -H 'Content-Type: application/json' -i http://focuson1:9200/megacorp2 --data '{
> "settings" : {
> "number_of_shards" : 3,
> "number_of_replicas" : 1
> }
> }'
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 68
{"acknowledged":true,"shards_acknowledged":true,"index":"megacorp2"}
往es添加数据时,也可以不指定id,会自动创建id,需要使用post请求,方式如下:
[root@focuson1 ~]# curl -X POST -H 'Content-Type: application/json' -i http://focuson1:9200/megacorp/employee/ --data '{
> "first_name": "John2",
> "last_name": "Smith2",
> "age": 256,
> "about": "I love to go rock climbing",
> "interests": [
> "sports",
> "music"
> ]
> }'
HTTP/1.1 201 Created
Location: /megacorp/employee/TfQ8ymMBtknNDl0i3mwi
content-type: application/json; charset=UTF-8
content-length: 179
{"_index":"megacorp","_type":"employee","_id":"TfQ8ymMBtknNDl0i3mwi","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":3,"_primary_term":1}
更新文档时,
和添加时是一样的,返回一个version,是一个不同于之前的version。更新时,elasticsearch将旧的文档标记为已删除,并增加一个全新的文档,旧的文档会在后台自动清除,但是不会立即清除。
创建文档
返回409,代表已存在,不能创建,如果不加op_type=create,会更新。也可以在URL最后加上/_create
[root@focuson1 ~]# curl -X PUT -H 'Content-Type: application/json' -i http://focuson1:9200/megacorp/employee/1?op_type=create --data '{
> "first_name": "John",
> "last_name": "Smith",
> "age": 25,
> "about": "I love to go rock climbing",
> "interests": [
> "sports",
> "music"
> ]
> }'
HTTP/1.1 409 Conflict
content-type: application/json; charset=UTF-8
content-length: 445
{"error":{"root_cause":[{"type":"version_conflict_engine_exception","reason":"[employee][1]: version conflict, document already exists (current version [4])","index_uuid":"hKhKh3YRT6yRiWQiBPSYuw","shard":"3","index":"megacorp"}],"type":"version_conflict_engine_exception","reason":"[employee][1]: version conflict, document already exists (current version [4])","index_uuid":"hKhKh3YRT6yRiWQiBPSYuw","shard":"3","index":"megacorp"},"status":409}
检索文档:
- 根据需要index、type、id,返回某个文档
[root@focuson1 ~]# curl -X GET -i http://focuson1:9200/megacorp/employee/1
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 249
{"_index":"megacorp","_type":"employee","_id":"1","_version":1,"found":true,"_source":{
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}}
- pretty
在请求参数中加上pretty,会使返回更加可读,但是source不会,会按照我们添加时候的格式返回
[root@focuson1 ~]# curl -X GET -i http://focuson1:9200/megacorp/employee/1?pretty
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 294
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "1",
"_version" : 3,
"found" : true,
"_source" : {
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests" : [
"sports",
"music"
]
}
}
- 返回部分字段
只返回部分字段
[root@focuson1 ~]# curl -X GET -i http://focuson1:9200/megacorp/employee/1?_source=first_name,last_name
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 128
{"_index":"megacorp","_type":"employee","_id":"1","_version":3,"found":true,"_source":{"last_name":"Smith","first_name":"John"}}
- 只返回source部分
只返回source里面的值
[root@focuson1 ~]# curl -X