Monthly Archive 6 November 2020

ElasticSearch / Magento 2 – various commands

Magento 2 uses ElasticSearch as a search engine.

It can be helpfull to directly request ElasticSearch to verify the data that have been indexed.

First you can get all indices.

In the command below ElastichSearch is accessible on host name 'elasticsearch' and on port 9200.


curl -X GET 'http://elasticsearch:9200/_cat/indices?v'

This will return the following data :

health status index                  uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   magento2_product_3_v10 h3qCDZVzT16OTOZAx3pMuA   1   1          0            0       208b           208b
yellow open   magento2_product_2_v10 uppg4DzLRSeNdf5HrgHcQg   1   1        189            0     79.4kb         79.4kb
yellow open   magento2_product_1_v10 nh8ckn7kS8-3l-F9dX8Tkw   1   1          0            0       208b           208b

You can then request documents in a specific indice.

Here for example, we are getting the 10 first documents in the indice magento2_product_2_v10.

curl -X GET --header 'Content-Type: application/json' \
http://elasticsearch:9200/magento2_product_2_v10/_search -d '{"size" : 10}'

The response look like that :

{
	"took": 3,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 189,
			"relation": "eq"
		},
		"max_score": 1.0,
		"hits": [
		  ...
		]
	}
}

You can se that the total number of documents that is returned in the "hits" > "total" field.

A hit structure will look like that :


{
    "_index": "magento2_product_2_v10",
    "_type": "document",
    "_id": "1692",
    "_score": 1.0,
    "_source": {
        "store_id": "2",
        "options": [
            "Prod 1"
        ],
        "sku": "prod_sku_1",
        "category_ids": [
            2
        ],
        "position_category_2": "0",
        "name_category_2": "Default Category"
        }
},

You can get documents using a query. Below we are requesting the documents in category 3.


curl -XGET --header 'Content-Type: application/json' http://elasticsearch:9200/magento2_product_2_v10/_search \
-d '{"query" : {"bool": {"must": [{"term": {"category_ids": "3"}}]}}}'