1. Index parameters
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. Add data to index
From Elastic search 6.x, 7x , each index can only have one type and each document needs to have an _id and _type value taken from the URL format that we send or the information will be randomly initialized , id can be numbers or letters.
Data format structure:
1 2 |
http://host:port/[index]/[type]/[document] |
Let’s pass the data together
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. Check the 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. Open/Close index
If there is a requirement to change the data maintenance configuration of an index or change the configuration of an index we can close the index (close) so that read/write operations cannot be performed on that index.
5. Read data from 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. Update parameters
If you want to update the parameters of an index such as the number of primary shards/replica,… you can do the following:
First, close the index , then update the index and open it again
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 } } |
Test:
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 |