OpenSearch Important API Commands

Handy API commands for working with OpenSearch clusters.

6 min read Updated SS

API: Application Programming Interfaces.

The API commands help users to interact with OpenSearch clusters. This interaction helps to perform different tasks quickly and easily.

To execute all the API commands mentioned below, can be copied and pasted in OpenSearch_dashboard > stack management > Dev tools.

Note: The following API commands provide the status in OpenSearch.

ActionCommands
Get Indices for a date sorted on storage size
GET
/_cat/indices/*2020.02.18?v&h=health,status,index,id,p,r,dc,dd,ss,cr
eation.date.string&s=ss:desc
Get Indices for a date sorted on the document count
GET
/_cat/indices/*2020.02.18?v&h=health,status,index,id,p,r,dc,dd,ss,cr
eation.date.string&s=dc:desc
Get Node-wise Index allocation stats sorted on available disk on the node
GET _cat/allocation?v&s=disk.avail:desc
OR use the below to get custom formatted.
GET_cat/allocation?v&h=node,ip,host,shards,disk.indices,disk.used,d
isk.avail,disk.total,disk.percent&s=disk.percent:desc
Simple basic get indices with headers
GET _cat/indices?v
Get indices sorted on storage size
GET /_cat/indices?v&h=i,ss&s=ss:desc
Get all Indices sorted on doc count
GET
/_cat/indices/?v&h=health,status,index,id,p,r,dc,dd,ss,creation.date.
string&s=dc:desc
Get Shards sorted on store size
GET /_cat/shards/?v&s=store:desc
Get Shards sorted on max docs
GET /_cat/shards/?v&s=docs:desc
Get Shards with a custom field list
GET _cat/shards/?v&h=node,ip,index,shard,prirep,state,
docs,store&s=node:desc,index:desc
Get un-assigned shards and Reason
GET _cat/shards?v&h=index,shard,prirep,state,unassigned.reason
Explain the reason for unassigned shards
GET _cluster/allocation/explain?pretty
Retry allocation for unassigned shards/
POST _cluster/reroute?retry_failed=true
List all Nodes in cluster
GET _cat/nodes?v&s=name:desc
Get stats
GET _stats
Get Node-wise Thread Pool Count/Size
GET _nodes/thread_pool
Get Thread Pool Stats based on rejections
GET _cat/thread_pool?v&s=rejected:desc
GET Detailed Thread Pool Stats based on rejections
GET_cat/thread_pool?v&h=node_name,name,active,queue,rejected,
completed,core,largest,max,pool_size,queue_size,size&s=rejected:de
sc
Get Indexing Stats
GET _stats?filter_path=indices.logscan*.total.indexing.
index_total
Get Nodes in Cluster
GET _cat/nodes?v&s=name:desc
GET Nodes in Cluster with Custom Field Order
GET_cat/nodes?v&h=name,ip,master,node.role,heap.percent,ram.pe
rcent,cpu,load_1m,load_5m,load_15m&s=name:desc
Get Node attributes
GET _cat/nodeattrs?v
Get Shards sorted on Index
GET /_cat/shards/?v&s=index:desc
GET Cluster Health
GET _cluster/health?pretty
GET cluster state
GET _cluster/state
GET Cluster Node Hot Threads
GET _cluster/nodes/hotthreads
OR
GET _cluster/nodes/hot_threads
GET Cluster Stats
GET _cluster/stats
GET RECOVERY Status
GET _cat/recovery?v
GET Pending Tasks
GET _cluster/pending_tasks
GET List of running tasks
GET _tasks
Cancel all tasks with the action as search
POST _tasks/_cancel?actions=*search
Get details of all search tasks
GET _tasks?actions=*search&detailed
Cancel a task based on the task ID
POST _tasks/Lh0N-jeIT7G0n60-bAu-mg:762638/_cancel
GET Cluster health for Index level
GET _cluster/health?pretty&level=indices
GET Cluster health for the shard level
GET _cluster/health?pretty&level=shards
The command below is equivalent to tail -f on Unix
docker logs -f <Container-Name/Id>
The command below is equivalent to More on Unix
docker logs <Container-Name/Id>
The command below is equivalent to tail on Unix
docker logs --tail <Number of lines to tail> <Container-Name/Id>
For example: docker logs --tail 10 logscan-app
Execute the below to change the disk-based shard allocation setting
PUT _cluster/settings{"transient":
{"cluster.routing.allocation.disk.threshold_enabled": false
"cluster.routing.allocation.disk.watermark.low":
"100gb","cluster.routing.allocation.disk.watermark.high":
"50gb","cluster.routing.allocation.disk.watermark.flood_stage":
"30gb","cluster.info.update.interval": "1m"}}
Move a particular Shard from one node to another
POST /_cluster/reroute{"commands" : [{"move" : {"index" : "logscan-
raw-upi-scl-2019.12.02", "shard" : 1,"from_node" : "node1",
"to_node" : "node2"}}]}
Enable Cluster Routing allocation for all nodes
PUT _cluster/settings{ "transient":{
"cluster.routing.allocation.enable" : "all"}}
Increase the node_left delay timeout for an index
PUT logscan-raw-logs-upi-*/_settings{"settings":
{"index.unassigned.node_left.delayed_timeout": "30s"}}
Manually allocate stale primary shards or allocate_empty_primary
POST _cluster/reroute?pretty{"commands" : [
{"allocate_stale_primary" :{"index": "logscan-raw-obp-app_index-
2019.12.21", "shard" : 0,"node": "logscan-db-node1
data-node-2", "accept_data_loss": true}}]}

Note: The following API commands help in sending queries.

All the below API commands can be executed as curl commands:

1. All GET/DELETE APIs can be executed as shown:

curl -u <UserName>:<Password> -XGET "http://<OpenSearchHost>:<OpenSearchPort>/<APICommand>"
curl -u <UserName>:<Password> -XDELETE "http://<OpenSearchHost>:<OpenSearchPort>/<APICommand>"

2. All PUT/POST APIs can be executed as shown:

curl -u <UserName>:<Password> -XPUT "http://<OpenSearchHost>:<OpenSearchPort>/<APICommand>" -H 'Content-Type: application/json' -d'{<JSON REQUEST BODY>}'
curl -u <UserName>:<Password> -XPOST "http://<OpenSearchHost>:<OpenSearchPort>/<APICommand>" -H 'Content-Type: application/json' -d'{<JSON REQUEST BODY>}'
ActionCommand
Create Index
POST sample-index/_doc
{
"message":"Hello World"
}
Delete Index
DELETE sample-index
Search Index
GET sample-index/_search
  • Index Mapping

Index mapping refers to the configuration of the data structure and properties of an index.

It determines the following:

  • Data storage
  • Indexing data
  • How to perform a search of the data
  • How to perform a query on the data
ActionCommand
Create new ISM policy
PUT _plugins/_ism/policies/<policyName>
{
// request body
}
Get specific ISM policy
GET _plugins/_ism/policies/<policyName>
List all ISM policies
GET _plugins/_ism/policies
Delete ISM policy
DELETE _plugins/_ism/policies/<policyName>
Create ingest-pipeline
PUT _ingest/pipeline/<ingest-pipeline-name>
{
// request body
}
Get a specific ingest pipeline
GET _ingest/pipeline/<ingest-pipeline-name>
List all ingest pipelines
GET _ingest/pipeline/
Delete ingest-pipeline
DELETE_ingest/pipeline/<ingest-pipeline-name>
Simulate ingest-pipeline
POST _ingest/pipeline/jim-ingest-pipeline/_simulate
{
// request body
}
Monitor the Ingest pipeline performance
GET _nodes/stats/ingest?filter_path=nodes.*.ingest
Create rollup-job
PUT _plugins/_rollup/jobs/<RollupJobName>
{
// request body
}
Get a specific rollup job
GET _plugins/_rollup/jobs/<RollupJobName>
List all rollup-jobs
GET _plugins/_rollup/jobs/
Delete rollup-job
DELETE _plugins/_rollup/jobs/<RollupJobName>
Create index template
PUT _template/<template name>
{
// request body
}
Get index template
GET _index_template/<template name>
Delete index template
DELETE _template/<template name>
List all index templates
GET _cat/templates?v
List all Aliases
GET _cat/aliases?v
Add an Alias
POST _aliases
{
"actions": [
{
"add": {
"index": "heal-kpi-rollup-15min-000001",
"alias": "heal-kpi-rollup-15mins",
"is_write_index": true
}
}
]
}
Remove Alias
POST _aliases
{
"actions": [
{
"remove": {
"index": "logs-nginx.access-prod",
"alias": "logs"
}
}
]
}
Delete Alias
DELETE <target>/_alias/<alias>
OR
DELETE <target>/_aliases/<alias>
Roll over an Alias or data stream
POST <AliasName/DataStream>/_rollover
Index template for data stream
PUT _index_template/logs-template-nginx
{
"index_patterns": "logs-nginx",
"data_stream": {
"timestamp_field": {
"name": "request_time"
}
},
"priority": 200,
"template": {
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
}
}
}
Create a data stream
PUT _data_stream/logs-nginx
Get information about a data stream
GET _data_stream/logs-nginx
Monitor stats of a data stream
GET _data_stream/logs-nginx/_stats
Ingest data into the data stream
POST logs-nginx/_doc
{
"message": "login attempt",
"@timestamp": "2013-03-01T00:00:00"
}
Searching a data stream
GET logs-nginx/_search
{
"query": {
"match": {
"message": "login"
}
}
}
Delete a data stream
DELETE _data_stream/<name_of_data_stream>
Re-index an Index
POST _reindex
{
"source": {
"index": "<SourceIndex>"
},
"dest": {
"index": "<DestinationIndex>"
}
}
Re-index data based on a condition or time range
POST _reindex
{
"source": {
"index": "heal_collated_kpi_41c34267-2eaf-426b-85de-
169a3b2f088e_2022.w42",
"query": {
"range": {
"@timestamp": {
"gte": "2022-10-17 16:00:00",
"lt": "2022-10-17 16:59:59",
"time_zone": "+05:30",
"format": "yyyy-MM-dd HH:mm:ss"
}
}
}
},
"dest": {
"index": "heal_collated_kpi_41c34267-2eaf-426b-85de-
169a3b2f088
e_2022.w42_reindex_timerange"
}
}
Re-index and apply the Ingest pipeline.
curl -ku admin:admin -XPOST "https://
192.0.2.54:9200/_reindex" -H 'Content-Type: application/json' -
d'
{
"source": {
"index": "heal_collated_kpi_heal_health_2022.w40"
},
"dest": {
"index": "heal_collated_kpi_heal_health_2022.w40_reindex",
"pipeline": "trino-date-match-ingest-pipeline"
}
}'
Check if ISM is set on an INDEX
GET _plugins/_ism/explain/index_1?show_policy=true
Retry ISM Policy on an index
POST _plugins/_ism/retry/index_1
{
"state": "delete"
}
Increase the Shard limit for single-node deployments.
### Increase the max shards per node for single-node deployment.
2curl -k -u admin:admin -XPUT https://localhost:9200
/_cluster/settings -H 'Content-type: application/json' --data-binary
$'{"persistent":{"cluster.max
_shards_per_node":3000}}'
Create a new ISM policy
PUT _plugins/_ism/policies/<policyName>
{
// request body
}
Get specific ISM policy
GET _plugins/_ism/policies/<policyName>
List all ISM policies
GET _plugins/_ism/policies
Delete ISM policy
DELETE _plugins/_ism/policies/<policyName>
Create ingest-pipeline
PUT _ingest/pipeline/<ingest-pipeline-name>
{
// request body
}
Get a specific ingest pipeline
GET _ingest/pipeline/<ingest-pipeline-name>
List all ingest pipelines
GET _ingest/pipeline/
Delete ingest-pipeline
DELETE_ingest/pipeline/<ingest-pipeline-name>
Simulate ingest-pipeline
POST _ingest/pipeline/jim-ingest-pipeline/_simulate
{
// request body
}
Monitor the Ingest pipeline performance
GET _nodes/stats/ingest?filter_path=nodes.*.ingest
Create rollup-job
PUT _plugins/_rollup/jobs/<RollupJobName>
{
// request body
}
Get a specific rollup job
GET _plugins/_rollup/jobs/<RollupJobName>
List all rollup-jobs
GET _plugins/_rollup/jobs/
Delete rollup-job
DELETE _plugins/_rollup/jobs/<RollupJobName>
Create index template
PUT _template/<template name>
{
// request body
}
Get index template
GET _index_template/<template name>
Delete index template
DELETE _template/<template name>
List all index templates
GET _cat/templates?v
List all Aliases
GET _cat/aliases?v
Add an Alias
POST _aliases
{
"actions": [
{
"add": {
"index": "heal-kpi-rollup-15min-000001",
"alias": "heal-kpi-rollup-15mins",
"is_write_index": true
}
}
]
}
Remove Alias
POST _aliases
{
"actions": [
{
"remove": {
"index": "logs-nginx.access-prod",
"alias": "logs"
}
}
]
}
Delete Alias
DELETE <target>/_alias/<alias>
OR
DELETE <target>/_aliases/<alias>
Roll over an Alias or data stream
POST <AliasName/DataStream>/_rollover
Index template for data stream
PUT _index_template/logs-template-nginx
{
"index_patterns": "logs-nginx",
"data_stream": {
"timestamp_field": {
"name": "request_time"
}
},
"priority": 200,
"template": {
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
}
}
}
Create a data stream
PUT _data_stream/logs-nginx
Get information about a data stream
GET _data_stream/logs-nginx
Monitor stats of a data stream
GET _data_stream/logs-nginx/_stats
Ingest data into the data stream
POST logs-nginx/_doc
{
"message": "login attempt",
"@timestamp": "2013-03-01T00:00:00"
}
Searching for a data stream
GET logs-nginx/_search
{
"query": {
"match": {
"message": "login"
}
}
}
Delete a data stream
DELETE _data_stream/<name_of_data_stream>
Re-index an Index
POST _reindex
{
"source": {
"index": "<SourceIndex>"
},
"dest": {
"index": "<DestinationIndex>"
}
}
Re-index data based on a condition or time range.
POST _reindex
{
"source": {
"index": "heal_collated_kpi_41c34267-2eaf-426b-85de-
169a3b2f088e_2022.w42",
"query": {
"range": {
"@timestamp": {
"gte": "2022-10-17 16:00:00",
"lt": "2022-10-17 16:59:59",
"time_zone": "+05:30",
"format": "yyyy-MM-dd HH:mm:ss"
}
}
}
},
"dest": {
"index": "heal_collated_kpi_41c34267-2eaf-426b-85de-169a3
b2f088e_2022.w42_reindex_timerange"
}
}
Re-index and apply the Ingest pipeline.
curl -ku admin:admin -XPOST "https://192.0.2.54:9200
/_reindex" -H 'Content-Type: application/json' -d'
{
"source": {
"index": "heal_collated_kpi_heal_health_2022.w40"
},
"dest": {
"index": "heal_collated_kpi_heal_health_2022.w40_reindex",
"pipeline": "trino-date-match-ingest-pipeline"
}
}'
Check if ISM is set on an INDEX
GET _plugins/_ism/explain/index_1?show_policy=true
Retry ISM Policy on an index
POST _plugins/_ism/retry/index_1
{
"state": "delete"
}
Increase the Shard limit for single-node deployments.
### Increase the max shards per node for single-node deployment.
curl -k -u admin:admin -XPUT https://localhost:9200/
_cluster/settings -H 'Content-type: application/json' --data-binary
$'{"persistent":{"cluster.max_shards_per_node":3000}}'