Common Endpoints

Cluster

Cluster Health

GET /_cluster/health
GET /_cluster/health?pretty

Using curl:

curl -XGET "<es_host>/_cluster/health" -u <user>:<pass>
curl -XGET "<es_host>/.kibana/_search" -u <user>:<pass> -H 'Content-Type: application/json" -d '{ "query": { "match_all": {} } }'

Cluster Nodes

GET /_cat/nodes?v
GET /_node

Cluster Indices

GET /_cat/indices?v

Cluster Shards

GET /_cat/shards?v

Index

Create Index

PUT /<index>
PUT /<index>
{
    "settings": {
        "number_of_shards": 1,
        "number_of_replicas": 1
    }
}

Delete Index

DELETE /<index>

Add Documents

POST /<index>/_doc
{
    "f1": "v1"
    ...
}

You can specify a document id:

PUT /<index>/_doc/<id>
{ <json data>
}

Get Document by _id

GET /<index>/_doc/<id>

Delete Document by _id

DELETE /<index>/_doc/<id>

Update a document

Actually retrieve old -> Add -> Replace

POST /<index>/_update/<id>
{
    "doc": {
        <f1>: <v1>
    }
}

Update documents using script

Script can also be used. For example, subtract a numeric field by 1

POST /<index>/_update/<id>
{
    "script": {
        "source": "ctx._source.<field>--"
    }
}

We can also declare params

POST /<index>/_update/<id>
{
    "script": {
        "source": "ctx._source.<field> -= params.<field_2>",
        "params": {
            "<field_2>": <value>
        }
    }
}

Do operation upon condition:

  • ctx.op = 'noop'

  • ctx.op = 'delete'

POST /<index>/_update/<id>
{
    "script": {
        "source": """
            if (ctx._source.<field> == 0) {
                ctx.op == 'noop';
            }
        """
    }
}

Update multiple documents

For example, decrement <field> by 1 in bulk:

POST /<index>/_update_by_query
{
    "script": {
        "source": "ctx._source.<field>--"
    },
    "query": {
        "match_all": {}
    }
}
POST /<index>/_update_by_query
{
    "conflicts": "proceed",
    "script": {
        "source": "ctx._source.<field>--"
    },
    "query": {
        "match_all": {}
    }
}

Bulk

Create index + Create document

POST /_bulk
{ "index": { "_index": "<index_name>" } }
{ <json_document> }
{ "create": { "_index": "<index_name>", "_id": <id> } }
{ <json_document> }

Update / Delete document

POST /_bulk
{ "update": { "_index": "<index>", "_id": <id> } }
{ "doc": { <json_document> } }
{ "delete": { "_index": "index", "_id": <id> } }

We can also use: POST /<index>/_bulk

Note:

  1. Content-Type should be application/x-ndjson if using script / curl etc.

  2. Each line must end with a newline character \n or \r\n (In text editor, this means the last line should be empty) (Don't type \n or \r\n in a text editor!)

  3. A failed action will not affect other actions

  4. Can use optimistic concurrency control within the action metadata!

Last updated