Elasticsearch Cheat Sheet

< Blog

A quick reference to Elasticsearch.

Created on: 2019-01-22

Tag: cheat_sheet

Words wrapped with curly braces, like {host} are variable, replace them with appropriate value.

List indexes

To list all the indexes of an Elasticsearch instance run the following command 1:

curl -XGET "http://{host}:{port}/_cat/indices?v"

Create an Index

To create a new index run the following command 2:

curl -XPUT 'http://{host}:{port}/{index}?pretty&pretty'

Delete an Index

To delete an index run the following command 3:

curl -XDELETE 'http://{host}:{port}/{index}?pretty&pretty'

Upload data

To upload a single data to an index run the following command 4:

curl -XPUT 'http://{host}:{port}/{index}/{type}/{doc_id}?pretty&pretty' -H 'Content-Type: application/json' -d'
{
"field_name": "data"
}'

Update a field value

To update a single data field run the following command 5:

curl -XPOST 'http://{host}:{port}/{index}/{type}/{doc_id}/_update?pretty&pretty' -H 'Content-Type: application/json' -d'
{
  "doc": { "field_name": "data" }
}'

OR

curl -XPOST 'http://{host}:{port}/{index}/{type}/{doc_id}/_update?pretty&pretty' -H 'Content-Type: application/json' -d'
{
  "script" : "ctx._source.field_name += 1"
}'

Search index

To search and index run the following command 6:

curl -XGET 'http://{host}:{port}/{index}/_search?q={field_name}:{keyword}'

Delete index data only

To delete only the data of an index run the following command 7:

curl -X POST "http://{host}:{port}/{index}/{type}/_delete_by_query?conflicts=proceed" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match_all": {}
  }
}
'

Source

1

List All Indices

2

Create an Index

3

Delete an Index

4

Indexing/Replacing Documents

5

Updating Documents

6

Search

7

Delete By Query API