Magento 2 - Useful Elasticsearch Commands
In this document, we will cover the elastic search commands that we generally used in the context of Magento debugging.
Related Tutorials
To get the Indices Details
List all indices
curl --location --request GET 'http://localhost:9200/_cat/indices?v'
Sample Output
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open magento2_product_14_v18 WfscY4ghRRCJXCwQnXfqiA 1 1 7 0 17.3kb 17.3kb
yellow open magento2_product_38_v18 zk6DJO9YRHaeGxk5XLKQOA 1 1 20 0 46.9kb 46.9kb
List all aliases
curl --location --request GET 'http://localhost:9200/_cat/aliases/?v'
Delete All Indices
curl -X DELETE "localhost:9200/*?pretty"
Sample Output
alias index filter routing.index routing.search is_write_index
magento2_product_27 magento2_product_27_v18 - - - -
commerce_product_7 commerce_product_7_v9 - - - -
To Fetch the Data directly from Elasticsearch
Get Records Count
To fetch the total no of records in the indices, Replace * with index name to get the total no of records for a specific index
curl --location --request GET 'http://localhost:9200/*/_count'
Sample Output
{
"count": 33,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
}
}
Get all the Records
To fetch all the records from all the indices, replace * with the index name to fetch all the records of a specific index
curl --location --request GET 'http://localhost:9200/*/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"track_total_hits" : true
}'
Sample Output
{
"took": 13,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 33,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "magento2_product_1_v26",
"_type": "document",
"_id": "340",
"_score": 1.0,
"_source": {
"store_id": "1",
"sku": "0986361001",
"updated_at": "2022-09-30T07:13:01+00:00",
"status": 1,
}
},
{
"_index": "magento2_product_1_v26",
"_type": "document",
"_id": "1714",
"_score": 1.0,
"_source": {
"store_id": "1",
"sku": "KKALSHAYA-001",
"updated_at": "2022-06-01T06:29:42+00:00",
"status": 1,
}
}
]
}
}
Get all the Records with limit
To fetch from a specific index, replace * with the index name
curl --location --request GET 'http://localhost:9200/*/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"match_all": {}
},
"_source": ["sku"],
"from" : 1,
"size" : 3
}'
Sample Output
{
"took": 20,
"timed_out": false,
"_shards": {
"total": 20,
"successful": 20,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 207,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "commerce_product_1_v9",
"_type": "document",
"_id": "2",
"_score": 1.0,
"_source": {
"sku": "24-MB04"
}
},
{
"_index": "commerce_product_1_v9",
"_type": "document",
"_id": "3",
"_score": 1.0,
"_source": {
"sku": "24-MB03"
}
},
{
"_index": "commerce_product_1_v9",
"_type": "document",
"_id": "430",
"_score": 1.0,
"_source": {
"sku": "MJ12"
}
}
]
}
}
To get a specific document
You should know the document ID. Here 1726 is the document id and magento2_product_1_v26 is the index name
curl --location --request GET 'http://localhost:9200/magento2_product_1_v26/_doc/1726'
Sample Output
{
"_index": "magento2_product_1_v26",
"_type": "_doc",
"_id": "1726",
"_version": 1,
"_seq_no": 5,
"_primary_term": 1,
"found": true,
"_source": {
"store_id": "1",
"sku": "giftcard_topup",
"updated_at": "2022-05-06T11:23:10+00:00",
"status": 1,
"status_value": "Enabled",
"visibility": 4,
"ship_to_store": 1,
"ship_to_store_value": "Enabled",
"reserve_and_collect": 1,
"reserve_and_collect_value": "Enabled",
"is_buyable": 1,
"tax_class_id": 0,
"tax_class_id_value": "None",
"is_sale": 0,
"is_new": 1,
"same_day_delivery": 0,
"same_day_delivery_value": "No",
"express_delivery": 0,
"express_delivery_value": "No",
"name": "topup",
"url_key": "buy-topup",
"product_activation_date": "2022-03-04T00:00:00+00:00",
"category_ids": [
2,
3736
],
"position_category_2": "10003",
"name_category_2": "category1",
"position_category_3736": "3",
"name_category_3736": "HPS Gift Card",
"price_0_1": "10.000000",
"price_1_1": "10.000000"
}
}
Fetch Data based on SKU
To fetch from a specific index, replace * with the index name
curl --location --request GET 'http://localhost:9200/*/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
"query": {
"match": {"sku": "24-MB04"}
}, "_source": ["name","sku","status","url_key","visibility","store_id","updated_at"]
}'
Sample Output
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.2039728,
"hits": [
{
"_index": "commerce_product_1_v9",
"_type": "document",
"_id": "2",
"_score": 1.2039728,
"_source": {
"store_id": "1",
"visibility": 4,
"name": "strive shoulder pack",
"sku": "24-MB04",
"url_key": "strive-shoulder-pack",
"status": 1
}
}
]
}
}
Get General Info on Elasticsearch Instance
To Get all the settings for all the indices.
Replace * with the index name to get the setting for a specific index.
curl --location --request GET 'http://localhost:9200/*/_settings'
Sample Output
{
"magento2_product_41_v18": {
"settings": {
"index": {
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_content"
}
}
},
"mapping": {
"total_fields": {
"limit": "101938"
}
},
"number_of_shards": "1",
"provided_name": "magento2_product_41_v18",
"max_result_window": "100000",
"creation_date": "1665213507317",
"analysis": {
"filter": {
"default_stemmer": {
"type": "stemmer",
"language": "english"
},
"unique_stem": {
"type": "unique",
"only_on_same_position": "true"
}
},
"char_filter": {
"default_char_filter": {
"type": "html_strip"
}
},
"analyzer": {
"prefix_search": {
"filter": [
"lowercase",
"keyword_repeat"
],
"char_filter": [
"default_char_filter"
],
"type": "custom",
"tokenizer": "default_tokenizer"
},
"default": {
"filter": [
"lowercase",
"keyword_repeat",
"default_stemmer",
"unique_stem"
],
"char_filter": [
"default_char_filter"
],
"type": "custom",
"tokenizer": "default_tokenizer"
},
"sku_prefix_search": {
"filter": [
"lowercase",
"keyword_repeat"
],
"type": "custom",
"tokenizer": "keyword"
},
"sku": {
"filter": [
"lowercase",
"keyword_repeat",
"default_stemmer",
"unique_stem"
],
"type": "custom",
"tokenizer": "keyword"
}
},
"tokenizer": {
"default_tokenizer": {
"type": "standard"
}
}
},
"number_of_replicas": "1",
"uuid": "3hoUumFITJi9cSnhwNDXog",
"version": {
"created": "7170499"
}
}
}
}
}
Get All Index Mapping
Here commerce_product_1 is the index name, replace it with * to get the mapping for all the indices
curl --location --request GET 'http://localhost:9200/commerce_product_1/_mapping'
Get Mapping for a Specific Field
To get the mapping details of a specific field. In the below query "barcode" is the field name. Replace * with index name to get for specific index
curl --location --request GET 'http://localhost:9200/*/_mapping/field/barcode'
Sample Response
{
"magento2_product_38_v28": {
"mappings": {
"barcode": {
"full_name": "barcode",
"mapping": {
"aims_barcode": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
},
"copy_to": [
"_search"
]
}
}
}
}
},
Get Index Mapping Fields Count
To get the mapping fields count of all the indices. Replace * with index name to get for specific index
curl --location --request GET 'http://localhost:9200/*/_settings/index.mapping.total_fields*'
Sample Response
{
"commerce_product_5_v9": {
"settings": {
"index": {
"mapping": {
"total_fields": {
"limit": "1267"
}
}
}
}
},
"commerce_product_1_v9": {
"settings": {
"index": {
"mapping": {
"total_fields": {
"limit": "1267"
}
}
}
}
}
}
To get the Heap Size
curl --location --request GET 'http://localhost:9200/_cat/nodes?h=heap*&v'
Sample Response
heap.current heap.percent heap.max
1.2gb 44 2.8gb
To get the cluster Health
curl --location --request GET 'http://localhost:9200/_cluster/health'
Sample Output
{
"cluster_name": "elasticsearch",
"status": "yellow",
"timed_out": false,
"number_of_nodes": 1,
"number_of_data_nodes": 1,
"active_primary_shards": 27,
"active_shards": 27,
"relocating_shards": 0,
"initializing_shards": 0,
"unassigned_shards": 20,
"delayed_unassigned_shards": 0,
"number_of_pending_tasks": 0,
"number_of_in_flight_fetch": 0,
"task_max_waiting_in_queue_millis": 0,
"active_shards_percent_as_number": 57.446808510638306
}
To list the pending task of elastic search
curl --location --request GET 'http://localhost:9200/_cluster/pending_tasks'
To get the node stats
This output will contain details of documents, shards, indexing, search etc
curl --location --request GET 'http://localhost:9200/_nodes/stats'
Update Elasticsearch Settings
To update a setting to a specific index replace * with index name
curl --location --request PUT 'http://localhost:9200/*/_settings' \
--header 'Content-Type: application/json' \
--data-raw '{ "index" : { "max_result_window": 3 } }'