Elasticsearch supports RESTful APIs, which allows you to use HTTP APIs to perform the add, delete, modify, and search operations and configure aliases. This topic describes how to use RESTful APIs for business operations.

Note For more information, see Elasticsearch RESTful API.

Elasticsearch reference [5.5]

Single document APIs

Multi-document APIs

Use Java REST clients to access clusters

Only access over HTTP and TCP is supported. We recommend that you use the Java REST client.

Use the Java API to access clusters

Elasticsearch provides a client for Java users to access clusters. For more information about the Java API, see Java API.

Transport client

The Transport client forwards requests to nodes in a cluster. The client is not a part of a cluster.

The Transport client uses the Elasticsearch Transport Protocol to communicate with clusters over port 9300.

Nodes in a cluster also use port 9300 to communicate with each other. You must open port 9300 for your nodes before grouping them into a cluster.

Note Your Java clients and nodes must use the same Elasticsearch version for them to recognize each other.

RESTful API over HTTP

All other languages can use a RESTful API to communicate with Elasticsearch over port 9200. You can use your preferred web client or the curl command to communicate with Elasticsearch.

Note

Elasticsearch provides official clients for several languages, including Groovy, Javascript, .NET, PHP, Perl, Python, and Ruby.

For more information about clients and plug-ins that are contributed by the community, see Community contributed clients.

Structure of a curl request over HTTP

curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>? <QUERY_STRING>' -d '<BODY>'

  • VERB: an HTTP method, including GET, POST, PUT, HEAD, and DELETE.
  • PROTOCOL: http or https. Use https only if HTTPS is enabled for Elasticsearch.
  • HOST: the hostname of a node in your Elasticsearch cluster. If you use the local node, set this field to localhost.
  • PORT: the port over which the Elasticsearch HTTP service runs. The default port number is 9200.
  • PATH: the API endpoint. For example, _count returns the number of documents in a cluster. PATH can contain multiple segments, such as _cluster/stats and _nodes/stats/jvm.
  • QUERY_STRING: optional request parameters. For example, the ?pretty parameter ensures that the JSON-format data returned by the request is more readable.
  • BODY: the JSON-format request body. This field is necessary only if the request requires a body.

Examples

Count the number of documents in an Elasticsearch cluster:
curl -XGET 'http://localhost:9200/_count?pretty' -d '
{ 
  "query": { 
    "match_all": {} 
  }
}'
Response:
{ 
    "count" : 0, 
    "_shards" : { 
        "total" : 5, 
        "successful" : 5, 
        "failed" : 0 
     }
}

You can use the curl -i command to retrieve the HTTP header, for example, curl -i -XGET 'localhost:9200/'.

Complete sample request:
curl -XGET 'localhost:9200/_count? pretty' -d '
{ 
    "query": { 
        "match_all": {} 
    }
}'
Shortened sample request:
GET /_count
{ 
    "query": { 
        "match_all": {} 
    }
}