Skip to content

Elasticsearch

Data Types

Data Type Description
keyword Not analyzed, used for exact matches
text Analyzed for full-text search
binary Binary data, encoded as base64
Data Type Description Range
byte 8-bit signed integer -128 to 127
short 16-bit signed integer -32,768 to 32,767
integer 32-bit signed integer -2,147,483,648 to 2,147,483,647
long 64-bit signed integer -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float Single-precision floating point Approx. ยฑ3.4 ร— 10^38
double Double-precision floating point Approx. ยฑ1.7 ร— 10^308
Data Type Description
date Date and time values, format defined by the user
Data Type Description
boolean True or false
Data Type Description
dense_vector Vector with fixed dimensions
sparse_vector Vector with variable dimensions

Document

curl -X GET "http://localhost:9200/_search?pretty" -u <username_and_password: elastic:pass123>
curl -X GET "http://localhost:9200/<index_name>/<_search or _count>?pretty" -H 'Content-Type: application/json' -d '{
  "from": <offset: 5>,
  "size": <limit: 20>,
  "_source": ["<field_1>", "<field_2>"],
  "query": {
    "bool": {
      "must": {
        "match": {"<field>": "<value>"}
      }
    }
  },
  "runtime_mappings": {
    "<custom_field>": {
      "type": "<field_type_in_java_format: double>",
      "script": {
        "source": "emit(doc[\"<another_field>\"].value * 2)"
      }
    }
  },
  "sort": [
    {"<field1>": "asc"},
    {"<field2>": "asc"}
  ]
}'
curl -X GET "http://localhost:9200/<index_name>/_search?pretty" -H 'Content-Type: application/json' -d '{
  "size": 0,  # will only return aggregate results
  "aggs": {
    "<agg_name_1>": {"avg": {"field": "<field>"}},
    "<agg_name_2>": {
      "filter": {"match": {"<field>": "<value>"}},
      "aggs": {
        "<nested_agg_name_1>": {"avg": {"field": "<field>"}}
      }
    }
  }
}'
curl -X POST "http://localhost:9200/<index_name>/_doc?pretty" -H 'Content-Type: application/json' -d '{
  "<field>": "<value>"
}'
# fully replace
curl -X PUT "http://localhost:9200/<index_name>/_doc/<_id>?pretty" -H 'Content-Type: application/json' -d '{
  "<field>": "<value>"
}'
# return document and its related data
curl -X GET "http://localhost:9200/<index_name>/_doc/<_id>?pretty"

# return document only
curl -X GET "http://localhost:9200/<index_name>/_source/<_id>?pretty"
curl -X DELETE "http://localhost:9200/<index_name>/_doc/<_id>?pretty"

Provides a way to perform multiple index, create, delete, and update actions in a single request.

  • The index and create actions expect a source on the next line.
  • update expects that the partial doc, upsert, and script and its options are specified on the next line.
  • delete does not expect a source on the next line
# Tip: Because this format uses literal `\n`'s as delimiters, make sure that the JSON actions and sources are not pretty printed

curl -X POST "http://localhost:9200/<index_name>/_bulk?pretty" -H 'Content-Type: application/json' -d '
{"index": {"_id": "<document_id>"}}
{"<field>": "<value>"}
{"delete": {"_id": "<document_id>"}}
{"create": {"_id": "<document_id>"}}
{"<field>": "<value>"}
{"update": {"_id": "<document_id>"}}
{"doc": {"<field>": "<value>"}}
'

Index

curl -X GET "http://localhost:9200/_aliases?pretty"
curl -X GET "http://localhost:9200/_cat/indices?v&h=index,store.size"
curl -X PUT "http://localhost:9200/<index_name>?pretty"
curl -X PUT "http://localhost:9200/<index_name>?pretty" -H 'Content-Type: application/json' -d '{
  "mappings": {
    "dynamic": "<dynamic_type: strict>",
    "properties": {
      "<field>": {
        "type": "<data_type>"
      }
    }
  }
}'
# Tip: The `remove` action will remove this alias from other indices

curl -X POST "http://localhost:9200/_aliases?pretty" -H 'Content-Type: application/json' -d'{
  "actions": [
    {
      "remove": {
        "index": "*",
        "alias": "<alias_name>"
      }
    },
    {
      "add": {
        "index": "<index_name>",
        "alias": "<alias_name>"
      }
    }
  ]
}'
# One index
curl -X DELETE "http://localhost:9200/<index_name>?pretty"

# Multiple indices
curl -X DELETE "http://localhost:9200/<index1>,<index2>,<index3>?pretty"

# All (it needs: `action.destructive_requires_name=false`)
curl -X DELETE "http://localhost:9200/*?pretty"
# Make the source index readonly
curl -X PUT "http://localhost:9200/<source_index_name>/_settings?pretty" -H 'Content-Type: application/json' -d'
{
  "settings": {"index.blocks.write": true}
}'

# Clone
curl -X POST "http://localhost:9200/<source_index_name>/_clone/<destination_index_name>?pretty"

# Make the source index write able
curl -X PUT "http://localhost:9200/<source_index_name>/_settings?pretty" -H 'Content-Type: application/json' -d'
{
  "settings": {"index.blocks.write": false}
}'
curl -X POST "http://localhost:9200/_reindex?pretty" -H 'Content-Type: application/json' -d'{
  "source": {"index": "<source_index_name>"},
  "dest": {"index": "<destination_index_name>"}
}'

Mapping

curl -X GET "http://localhost:9200/<index_name>/_mapping?pretty"
# Tip: We can't change the mapping or field type of an existing field. Changing an existing field could invalidate data that's already indexed

curl -X PUT "http://localhost:9200/<index_name>/_mapping?pretty" -H 'Content-Type: application/json' -d '{
  "dynamic": "<dynamic_type: strict>",
  "properties": {
    "<field>": {
      "type": "<data_type>"
    }
  }
}'

There is no way to delete a field from mapping. Even if you delete all documents that contain this field

Node

curl -X GET "http://localhost:9200/_cat/nodes?v&h=name,disk.total,disk.used,disk.avail"

Other

  • Clustering:

  • Elasticsearch DSL & Django ORM: