4.ElasticSearch的基本api操作
1. ElasticSearch的Index
1. 索引初始化
在创建索引之前 对索引进行初始化操作
指定shards数量和replicas数量
curl -XPUT 'http://192.168.10.1:9200/library' -d {
"settings":{
"index":{
"number_of_shards":5,
"number_of_replicas":1,
}
}
}
2. 查看索引信息
-
GET 地址/索引名称/_settings 即可查看索引信息
执行 curl -X GET http://192.168.10.1:9200/test/_settings 返回 { "test": { "settings": { "index": { "creation_date": "1507896234377", "number_of_shards": "5", "number_of_replicas": "1", "uuid": "fhI9dJWsQoCswafewXNqrA", "version": { "created": "5010299" }, "provided_name": "test" } } } }
3. 查看多个索引信息
-
GET 地址/索引名称,索引名称/_settings 即可查看2个索引信息
执行 curl -X GET http://192.168.10.1:9200/test,test2/_settings 返回 { "test": { "settings": { "index": { "creation_date": "1507896234377", "number_of_shards": "5", "number_of_replicas": "1", "uuid": "fhI9dJWsQoCswafewXNqrA", "version": { "created": "5010299" }, "provided_name": "test" } } } "test2": { "settings": { "index": { "creation_date": "1506320574153", "number_of_shards": "5", "number_of_replicas": "1", "uuid": "Ol-fhI9dJWsQoNqrA1jE1w", "version": { "created": "5010299" }, "provided_name": "test2" } } } }
4. 查看所有索引信息
-
GET 地址/all/_settings 查看所有索引信息
执行 curl -X GET http://192.168.10.1:9200/_all/_settings 返回 省略了 因为太多了
5. 查看所有索引列表
-
GET 地址/_cat/indices?v
执行 curl -X GET 'http://192.168.10.1:9200/_cat/indices?v=' 返回 省略了 因为太多了
2. ElasticSearch的CURD
1. 创建索引
-
创建索引
创建一个索引名称为test9的索引 curl -X PUT http://192.168.10.1:9200/test9/ 返回 { "acknowledged": true, "shards_acknowledged": true }
-
创建索引及类型和文档
PUT 地址/索引名称/type名称/文档id 执行 curl -X PUT http://192.168.10.1:9200/test10/people/1 -d '{ "title": "test10" }' 返回 { "_index": "test10", "_type": "people", "_id": "AV8fmZO1Jq0BrQx-qzvq", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 2, "failed": 0 }, "created": true }
-
创建索引及类型,不设置文档ID(因为会自动设置文档ID)
POST 地址/索引名称/type名称/ 执行 curl -X POST http://192.168.10.1:9200/test11/people/ -d '{ "title": "test11" }' 返回 { "_index": "test11", "_type": "people", "_id": "AV8fmZO1Jq0BrQx-qzvq", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 2, "failed": 0 }, "created": true }
2. 查看索引相关信息
-
根据id查看文档信息
get 地址/索引名称/type名称/文档id 执行 curl -X GET http://192.168.10.1:9200/test10/people/1 返回 { "_index": "test10", "_type": "people", "_id": "1", "_version": 1, "found": true, "_source": { "title": "test10" } }
-
通过source获取指定字段
get /索引名称/type名称/文档id?_source=字段 执行 curl -X GET http://192.168.10.1:9200/test10/people/1?_source=title 返回 { "_index": "test10", "_type": "people", "_id": "1", "_version": 1, "found": true, "_source": { "title": "test10" } }
3. 更新索引
-
更新同一id下的信息
PUT 地址/索引名称/type名称/文档id 执行 curl -X PUT http://192.168.10.1:9200/test10/people/1 -d '{ "title": "test10" }' 返回 { "_index": "test10", "_type": "people", "_id": "1", "_version": 2, "result": "updated", "_shards": { "total": 2, "successful": 2, "failed": 0 }, "created": false }
-
更新指定字段
POST 地址/索引名称/type名称/文档id/_update 执行 curl -X POST http://192.168.10.1:9200/test10/people/1 -d '{ "doc":{ "title": "testt12" } }' 返回 { "_index": "test10", "_type": "people", "_id": "1", "_version": 3, "found": true, "_source": { "doc": { "title": "testt12" } } }
4. 删除索引及文档
-
删除索引
delete 地址/索引名称 执行 curl -X DELETE http://192.168.10.1:9200/test10 返回 { "acknowledged": true }
-
删除文档
delete 地址/索引名称/type名称/文档id 执行 curl -X DELETE http://192.168.10.1:9200/test10/people/1 { "found": true, "_index": "test10", "_type": "people", "_id": "1", "_version": 4, "result": "deleted", "_shards": { "total": 2, "successful": 2, "failed": 0 } }