1. Thông số index
1 2 3 4 5 6 7 8 9 10 | curl -X PUT "localhost:9200/quach-ca" -H 'Content-Type: application/json' -d' { "settings" : { "index" : { "number_of_shards" : 4, "number_of_replicas" : 1 } } } |
2. Thêm dữ liệu vào index
Từ Elastic search 6.x, 7x thì mỗi index chỉ có thể có một type và mỗi document cần có một giá trị _id và _type
được lấy từ định dạng URL mà chúng ta gửi hoặc các thông tin sẽ được khởi tạo ngẫu nhiên, id có thể là số hoặc chữ.
Cấu trúc format dữ liệu:
1 2 | http://host:port/[index]/[type]/[document] |
Cùng truyền vào dữ liệu
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | curl -X POST "localhost:9200/quach-ca/employee/1" -H 'Content-Type: application/json' -d' { "name": "quach dai ca" } { "_index": "quach-ca", "_type": "employee", "_id": "1", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 2 } |
3. Kiểm tra index
1 2 3 4 5 6 7 8 9 10 11 12 | curl -X GET http://localhost:9200/quach-ca/employee/1?pretty { "_index" : "quach-ca", "_type" : "employee", "_id" : "1", "_version" : 3, "found" : true, "_source" : { "name" : "quach dai ca" } } |
4. Mở/Đóng index
Nếu có yêu cầu thay đổi cấu hình bảo trì dữ liệu của một index hoặc thay đổi cấu hình của một index chúng ta có thể đóng index (close) để các hoạt động read/write không thể thực hiện trên index đó.
5. Đọc dữ liệu từ index
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | curl -X GET "localhost:9200/quach-ca/_search?pretty" { "took" : 1, "timed_out" : false, "_shards" : { "total" : 4, "successful" : 4, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 3, "max_score" : 1.0, "hits" : [ { "_index" : "quach-ca", "_type" : "employee", "_id" : "3", "_score" : 1.0, "_source" : { "name" : "quach dai ca" } }, { "_index" : "quach-ca", "_type" : "employee", "_id" : "1", "_score" : 1.0, "_source" : { "name" : "quach ca ca" } }, { "_index" : "quach-ca", "_type" : "employee", "_id" : "2", "_score" : 1.0, "_source" : { "name" : "quach ca" } } ] } } |
6. Cập nhập thông số
Nếu muốn cập nhập thông số của một index như số lượng primary shard/replica,… thì có thể làm như sau:
Đầu tiên là phải close index sau đó tiến hành cập nhập index và open trở lại
1 2 3 4 5 6 7 8 9 | curl -X POST 'localhost:9200/quach-ca/_close' {"acknowledged":true} # curl -X GET "localhost:9200/_cat/indices?v" health status index uuid pri rep docs.count docs.deleted store.size pri.store.size green open .kibana I1PNVZWrQ-qDutbGN2jAhA 1 0 2 0 10.4kb 10.4kb close quach-ca C4UD3u4bS6qKq8uWARFIgg |
1 2 3 4 5 6 7 | curl -X PUT "localhost:9200/quach-ca/_settings" -H 'Content-Type: application/json' -d' { "index" : { "number_of_replicas" : 3 } } |
Kiểm tra:
1 2 3 4 | curl -X GET "localhost:9200/_cat/indices?v" health status index uuid pri rep docs.count docs.deleted store.size pri.store.size green open .kibana I1PNVZWrQ-qDutbGN2jAhA 1 0 2 0 10.4kb 10.4kb yellow open quach-ca C4UD3u4bS6qKq8uWARFIgg 4 3 0 0 1kb 1kb |