一、創建文檔
[root@ AOS2 @AutoTest01:/root]#curl -X PUT '9.1.6.140:9200/students/class1/1?pretty' -d '
> {
> "first_name": "changwei",
> "last_name": "kang",
> "gender": "male",
> "age": 28,
> "courses": "IT services"
> }'
{
"error" : "Content-Type header [application/x-www-form-urlencoded] is not supported",
"status" : 406
}
創建文檔報錯,es6版本要求更加嚴格需要加上-H 'Content-Type: application/json'
[root@ AOS2 @AutoTest01:/root]#curl -X PUT '9.1.6.140:9200/students/class1/1?pretty' -H 'Content-Type: application/json' -d '
{
"first_name": "changwei",
"last_name": "kang",
"gender": "male",
"age": 28,
"courses": "IT services"
}'
{
"_index" : "students",
"_type" : "class1",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
二、獲取文檔
[root@ AOS2 @AutoTest01:/root]#curl -XGET 'localhost:9200/students/class1/1?pretty'
{
"_index" : "students",
"_type" : "class1",
"_id" : "1",
"_version" : 3,
"found" : true,
"_source" : {
"first_name" : "changweiA",
"last_name" : "kangA",
"gender" : "male",
"age" : 28,
"courses" : "IT services"
}
}
創建
[root@ AOS2 @AutoTest01:/root]#curl -X PUT '9.1.6.140:9200/students/class1/2?pretty' -H 'Content-Type: application/json' -d '
{
"first_name": "changweiAaa",
"last_name": "kangAaa",
"gender": "maleaa",
"age": 28,
"courses": "IT services aa"
}'
{
"_index" : "students",
"_type" : "class1",
"_id" : "2",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
獲取
[root@ AOS2 @AutoTest01:/root]#curl -XGET 'localhost:9200/students/class1/2?pretty'
{
"_index" : "students",
"_type" : "class1",
"_id" : "2",
"_version" : 2,
"found" : true,
"_source" : {
"first_name" : "changweiAaa",
"last_name" : "kangAaa",
"gender" : "maleaa",
"age" : 28,
"courses" : "IT services aa"
}
}
三、更新文檔
[root@ AOS2 @AutoTest01:/root]#curl -XPOST '9.1.6.140:9200/students/class1/2/_update?' -d '
> {
> "doc": {"courses": "chushichushichushi"}
> }'
{"error":"Content-Type header [application/x-www-form-urlencoded] is not supported","status":406}[
報錯忘了加個參數
修改
[root@ AOS2 @AutoTest01:/root]#curl -XPOST '9.1.6.140:9200/students/class1/2/_update?pretty' -H 'Content-Type: application/json' -d '
{
"doc": {"courses": "chushichushichushiaaaaaaaaa"}
}'
{
"_index" : "students",
"_type" : "class1",
"_id" : "2",
"_version" : 4,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 3,
"_primary_term" : 1
}
查看
[root@ AOS2 @AutoTest01:/root]#curl -XGET '9.1.6.140:9200/students/class1/2?pretty'
{
"_index" : "students",
"_type" : "class1",
"_id" : "2",
"_version" : 4,
"found" : true,
"_source" : {
"first_name" : "changweiAaa",
"last_name" : "kangAaa",
"gender" : "maleaa",
"age" : 28,
"courses" : "chushichushichushiaaaaaaaaa"
}
}
修改成功
四、刪除文檔
刪除數據
[root@ AOS2 @AutoTest01:/root]# curl -XDELETE '9.1.6.140:9200/students/class1/1?pretty'
{
"_index" : "students",
"_type" : "class1",
"_id" : "1",
"_version" : 4,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 3,
"_primary_term" : 1
}
[root@ AOS2 @AutoTest01:/root]# curl -XDELETE '9.1.6.140:9200/students/class1/2?pretty'
{
"_index" : "students",
"_type" : "class1",
"_id" : "2",
"_version" : 5,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 4,
"_primary_term" : 1
}
再次查看
[root@ AOS2 @AutoTest01:/root]# curl -XGET '9.1.6.140:9200/students/class1/2?pretty'
{
"_index" : "students",
"_type" : "class1",
"_id" : "2",
"found" : false
}
查看索引
[root@ AOS2 @AutoTest01:/root]#curl -XGET 'localhost:9200/_cat/indices?v'
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open students H0z_SQQ2Q_-cXWKmAECBjg 5 1 2 0 23.6kb 11.8kb
刪除索引
[root@ AOS2 @AutoTest01:/root]# curl -XDELETE '9.1.6.140:9200/students?pretty'
{
"acknowledged" : true
}
查看索引
[root@ AOS2 @AutoTest01:/root]#curl -XGET 'localhost:9200/_cat/indices?v'
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
五、簡單查詢
1、request API 查詢
先創建數據
[root@ AOS2 @AutoTest01:/root]#curl -X PUT '9.1.6.140:9200/students/class1/1?pretty' -H 'Content-Type: application/json' -d '
{
"first_name": "kang wang",
"last_name": "changweikang",
"gender": "male",
"age": 25,
"courses": "IT Services Linux"
}'
{
"_index" : "students",
"_type" : "class1",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
[root@ AOS2 @AutoTest01:/root]#curl -X PUT '9.1.6.140:9200/students/class1/2?pretty' -H 'Content-Type: application/json' -d '
{
"first_name": "kang",
"last_name": "changwei",
"gender": "male",
"age": 25,
"courses": "IT Services"
}'
{
"_index" : "students",
"_type" : "class1",
"_id" : "2",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
查詢所有文檔方法_serach
[root@ AOS2 @AutoTest01:/root]#curl -XGET '9.1.6.140:9200/students/_search?pretty'
{
"took" : 22,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
}, 上面爲查詢執行的相關信息
下面爲命中文檔的相關信息
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [
{
"_index" : "students",
"_type" : "class1",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"first_name" : "kang",
"last_name" : "changwei",
"gender" : "male",
"age" : 25,
"courses" : "IT Services"
}
},
{
"_index" : "students",
"_type" : "class1",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"first_name" : "kang wang",
"last_name" : "changweikang",
"gender" : "male",
"age" : 25,
"courses" : "IT Services Linux"
}
}
]
}
}
查詢kang字符串的數據
[root@ AOS2 @AutoTest01:/root]#curl -XGET '9.1.6.140:9200/_search?q='kang''
{"took":132,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":2,"max_score":0.2876821,"hits":[{"_index":"students","_type":"class1","_id":"2","_score":0.2876821,"_source":
{
"first_name": "kang",
"last_name": "changwei",
"gender": "male",
"age": 25,
"courses": "IT Services"
}},{"_index":"students","_type":"class1","_id":"1","_score":0.2876821,"_source":
{
"first_name": "kang wang",
"last_name": "changweikang",
"gender": "male",
"age": 25,
"courses": "IT Services Linux"
}}]}}[root@ AOS2 @AutoTest01:/root]#
更直觀的json格式
[root@ AOS2 @AutoTest01:/root]#curl -XGET '9.1.6.140:9200/_search?q='kang'&pretty'
{
"took" : 8,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "students",
"_type" : "class1",
"_id" : "2",
"_score" : 0.2876821,
"_source" : {
"first_name" : "kang",
"last_name" : "changwei",
"gender" : "male",
"age" : 25,
"courses" : "IT Services"
}
},
{
"_index" : "students",
"_type" : "class1",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"first_name" : "kang wang",
"last_name" : "changweikang",
"gender" : "male",
"age" : 25,
"courses" : "IT Services Linux"
}
}
]
}
}
多索引、多類型查詢:
/_search:所有索引;
/INDEX_NAME/_search:單索引;
/INDEX1,INDEX2/_search:多索引;
/s*,t*/_search:
/students/class1/_search:單類型搜索
/students/class1,class2/_search:多類型搜索
對每一個文檔,會取得其所有域的所有值,生成一個名爲“_all”的域;執行查詢時,如果在query_string未指定查詢的域,則在_all域上執行查詢操作;
GET /_search?q='Kang'
GET /_search?q='IT20%Linux'
GET /_search?q=courses:'IT%20Linux'
GET /_search?q=courses:'Linux'
搜索制定域的字段
[root@ AOS2 @AutoTest01:/root]#curl -XGET '9.1.6.140:9200/_search?q=courses:Linux&pretty'
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "students",
"_type" : "class1",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"first_name" : "kang wang",
"last_name" : "changweikang",
"gender" : "male",
"age" : 25,
"courses" : "IT Services Linux"
}
}
]
}
}
2、request body查詢
此種查詢可以編寫更爲複雜的查詢
[root@ AOS2 @AutoTest01:/root]#curl -XGET '9.1.6.140:9200/students/_search?pretty' -H 'Content-Type: application/json' -d '
{
"query":{ "match_all": {} }
}'
{
"took" : 9,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [
{
"_index" : "students",
"_type" : "class1",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"first_name" : "kang",
"last_name" : "changwei",
"gender" : "male",
"age" : 25,
"courses" : "IT Services"
}
},
{
"_index" : "students",
"_type" : "class1",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"first_name" : "kang wang",
"last_name" : "changweikang",
"gender" : "male",
"age" : 25,
"courses" : "IT Services Linux"
}
}
]
}
}