为什么80%的码农都做不了架构师?>>>
一、创建文档
[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"}}]}
}