Elasticsearch API with Curl
Curl Command
a command-line tool to transfer data to or from a server
can transfer multiple files at once
curl [options] [URL...]
curl --help
# -o, saves the downloaded content on the local
curl -o [file_name] [URL...]
# -u, download from user authenticated servers
curl -u {username}:{password} [FTP_URL]
# -T, upload a file to the FTP server
curl -u {username}:{password} -T {filename} {FTP_Location}
# -x, set a proxy to access a URL
curl -x "http://user:pwd@127.0.0.1:1234" "http://httpbin.org/ip"
curl "http://httpbin.org/ip"
# -X or --request, HTTP method to be used, GET, POST, PUT, DELETE
curl -XGET "localhost:9200/_cluster/health"
# -i or --include, include the response headers
# -d or --data, the data to be sent to the API
# -H or --header, any additional headers to be sent
curl -XGET "localhost:9200/produce_v2/_search" -H 'Content-Type: application/json' -d'{"query":{"match_all":{}}}'
Basics
# Cluster health
# GET _cluster/health
curl -XGET "localhost:9200/_cluster/health"
# Node info
# GET _nodes/stats
curl -XGET "localhost:9200/_nodes/stats"
# Get index info
# GET index_name
curl -XGET "localhost:9200/ecommerce_data"
# List indices
# GET /_cat/indices
curl -XGET "localhost:9200/_cat/indices"
Create, Read, Update, and Delete (CRUD)
Create
# PUT index_name
curl -XPUT "localhost:9200/tutorial_curl"
# create index with a mapping
curl -XPUT "localhost:9200/tutorial_curl" -H 'Content-Type: application/json' -d'{"mappings":{"properties":{"Firstname":{"type":"keyword"},"Lastname":{"type":"long"}}}}'
Insert
curl -XPOST "localhost:9200/tutorial_curl/_doc" -H 'Content-Type: application/json' -d'{"Firstname":"Lin","Lastname":"Chen"}'
# insert a document with the id
curl -XPOST "localhost:9200/tutorial_curl/_doc/1" -H 'Content-Type: application/json' -d'{"Firstname":"Lin","Lastname":"Chen"}'
Read
# GET index_name/_doc/id
curl -XGET "localhost:9200/tutorial_curl/_doc/1"
Update
# POST index_name/_update/id
curl -XPOST "localhost:9200/tutorial_curl/_update/1" -H 'Content-Type: application/json' -d'{"doc":{"Firstname":"Unknown","Lastname":"Unknown"}}'
Delete
# DELETE index_name
curl -XDELETE "localhost:9200/tutorial_curl"
# DELETE index_name/_doc/id
curl -XDELETE "localhost:9200/tutorial_curl/_doc/1"
# Delete by query
curl -XPOST "localhost:9200/tutorial_curl/_delete_by_query" -H 'Content-Type: application/json' -d'{"query":{"match":{"Firstname":{"query":"Lin"}}}}'
Query
# query all
curl -XGET "localhost:9200/ecommerce_data/_search"
# Search between two date
curl -XGET "localhost:9200/news_headlines/_search" -H 'Content-Type: application/json' -d'{"query":{"range":{"date":{"gte":"2017-05-28T00:00:00.000-04:00","lt":"2017-12-26T00:00:00.000-05:00"}}}}'
# term match
curl -XGET "localhost:9200/news_headlines/_search" -H 'Content-Type: application/json' -d'{"query":{"match":{"headline":{"query":"Khloe Kardashian Kendall Jenner"}}}}'
# query multiple fields
curl -XGET "localhost:9200/news_headlines/_search" -H 'Content-Type: application/json' -d'{"query":{"multi_match":{"query":"Michelle Obama","fields":["headline","short_description","authors"]}}}'
Aggregation
curl -XGET "localhost:9200/news_headlines/_search" -H 'Content-Type: application/json' -d'{"aggs":{"by_category":{"terms":{"field":"category","size":100}}}}'