4. 添加文档:
(1). 指定文档ID:
curl -XPUT "localhost:9200/lib/user/1?pretty" -H 'Content-Type: application/json' -d'{
"name": "Jane",
"age": 32,
"interests": ["music"]
}'
结果:
{
"_index" : "lib",
"_type" : "user",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 1,
"successful" : 1, // 成功了
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
(2). 不指定文档ID:
curl -XPOST "localhost:9200/lib/user?pretty" -H 'Content-Type: application/json' -d'{
"name": "Lily",
"age": 19,
"interests": ["driver"]
}'
结果:
{
"_index" : "lib",
"_type" : "user",
"_id" : "s_ED7m4BKPXy_Hki0Ti6", // es自动生成的文档ID
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 1,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
(3). 查看文档:
curl -XGET "localhost:9200/lib/user/1?pretty"
{
"_index" : "lib",
"_type" : "user",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "Jane",
"age" : 32,
"interests" : [
"music"
]
}
}
curl -XGET "localhost:9200/lib/user/s_ED7m4BKPXy_Hki0Ti6?pretty"
# 只返回name和age的字段的值
curl -XGET "localhost:9200/lib/user/1?_source=name,age&pretty"