Elasticsearch REST API
Elasticsearch supports HTTP request-response service, HTTP requests are broken by default with 9200, so the Curl command allows you to send an HTTP request and get the JSON return content. Common rest APIs include a few:
Check ES cluster status
curl http://localhost:9200/_cat/health?v
Check the status of the ES node
curl http://localhost:9200/_cat/nodes?v
Querying all the Indexes
curl http://localhost:9200/_cat/indices?v
Create an index
curl -XPUT http://localhost:9200/myindex/mytype/id -H ‘Content-Type: application/json‘ -d ‘{"name":"ysl"}‘
Delete Index
curl -XDELETE http://localhost:9200/myindex
Query index
curl -XGET http://localhost:9200/myindex/mytype/id
Add data
curl -XPUT ‘http://localhost:9200/kimchy/doc/1?pretty‘ -H ‘Content-Type: application/json‘ -d ‘{ "user": "kimchy", "post_date": "2009-11-15T13:12:00", "message": "Trying out Elasticsearch, so far so good?"}‘
Querying data
curl -XGET ‘http://localhost:9200/kimchy,another_user/_search?pretty=true‘ -H ‘Content-Type: application/json‘ -d ‘{ "query" : { "match_all" : {} }}‘
Bulk Add data
Dynamic mapping can't add data, don't worry! You can use the bulk command to add data within a JSON file
Defining JSON data
{"index":{"_index":"test123","_id":1}}{"name":"ysl","age":25}{"index":{"_index":"test123","_id":2}}{"name":"wdd","age":25}{"index":{"_index":"test123","_id":3}}{"name":"yjb","age":25}
Note that the JSON file name is arbitrarily specified, and the first line defines the index and some characters commonly used segments:
- _index defines the name of the index, if you do not specify that you want to add the index name field in the Curl command
- _type defines the type of the index, if you do not specify that you want to add the index type field to the Curl command
- _ID defines the ID of the row's data and, if not specified, randomly generates a string of numbers.
Execute command
Go to the directory where the JSON file is located and execute the command:
curl localhost:9200/索引名称/索引类型/_bulk?pretty --data-binary @data.json
Note that if _index and _type are defined in the JSON file, they are not written here (even if they are written, they are generated as JSON files).
curl localhost:9200/_bulk?pretty --data-binary @data.json
Similarly, if you follow the above definition of _index but do not define _type, then the index is test123 and the type is the type specified in our Curl command.
Execute the above command
curl http://localhost:9200/test123/test123/_bulk?pretty --data-binary @data.json
Get the following results
{ "took" : 1233, "errors" : false, "items" : [ { "index" : { "_index" : "test123", "_type" : "test123", "_id" : "1", "_version" : 1, "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "status" : 201 } }, { "index" : { "_index" : "test123", "_type" : "test123", "_id" : "2", "_version" : 1, "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "status" : 201 } } ]}
Elasticsearch using the rest API for full-text indexing