Magento 2 ElasticSearch: No alive nodes found in your cluster / Types cannot be provided in put mapping requests

Problem:

There was the following error related to ElasticSearch / OpenSearch while reindexing the catalogsearch in Magento.


bin/magento indexer:reindex catalogsearch_fulltext

could not validate a connection to elasticsearch. 
no alive nodes found in your cluster

Another similar error during catalogsearch reindex:


bin/magento indexer:reindex catalogsearch_fulltext

Catalog Search index process error during indexation process:
{"error":{
"root_cause":[{
"type":"illegal_argument_exception",
"reason":"Types cannot be provided in put mapping requests, 
    unless the include_type_name parameter is set to true."
}],
"type":"illegal_argument_exception",
"reason":"Types cannot be provided in put mapping requests, 
    unless the include_type_name parameter is set to true."
},
"status":400}

Solution:

There can be different causes for this issue. Below are some of the solutions:

Check if ElasticSearch is working


curl http://localhost:9200

If you get proper JSON output then your ElasticSearch / OpenSearch is working fine.


{
  "name" : "85f15ec78014",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "GOcHpOsvQH-t4gbC1jiRtQ",
  "version" : {
    "distribution" : "opensearch",
    "number" : "1.2.4",
    "build_type" : "tar",
    "build_hash" : "e505b10357c03ae8d26d675172402f2f2144ef0f",
    "build_date" : "2022-01-14T03:38:06.881862Z",
    "build_snapshot" : false,
    "lucene_version" : "8.10.1",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "The OpenSearch Project: https://opensearch.org/"
}

If it sends a response like could not connect, then ElasticSearch is not running on your machine. You might need to start it or install it if not installed.


sudo service elasticsearch start

Check Magento Configuration Settings

Check core_config_data table


SELECT * FROM core_config_data WHERE path like '%elastic%’;

In older Magento versions, the ElasticSearch settings were saved with the following path:

catalog/search/elasticsearch*


MariaDB [magento]> SELECT * FROM core_config_data WHERE path like '%elastic%';
+-----------+---------+----------+----------------------------------------------------+-----------+---------------------+
| config_id | scope   | scope_id | path                                               | value     | updated_at          |
+-----------+---------+----------+----------------------------------------------------+-----------+---------------------+
|      3456 | default |        0 | catalog/search/elasticsearch_server_hostname       | 127.0.0.1 | 2020-04-07 19:53:33 |
|      3459 | default |        0 | catalog/search/elasticsearch_server_port           | 9200      | 2020-04-07 19:53:33 |
|      3462 | default |        0 | catalog/search/elasticsearch_index_prefix          | magento2  | 2020-04-07 19:53:33 |
|      3465 | default |        0 | catalog/search/elasticsearch_enable_auth           | 0         | 2020-04-07 19:53:33 |
|      3468 | default |        0 | catalog/search/elasticsearch_server_timeout        | 15        | 2020-04-07 19:53:33 |
|      5222 | default |        0 | catalog/search/elasticsearch5_enable_auth          | 0         | 2020-04-07 19:53:33 |
|      5225 | default |        0 | catalog/search/elasticsearch5_server_timeout       | 15        | 2020-04-07 19:53:33 |
|      6404 | default |        0 | catalog/search/elasticsearch7_enable_auth          | 0         | 2020-06-09 00:45:34 |
|      6407 | default |        0 | catalog/search/elasticsearch7_server_timeout       | 15        | 2020-06-09 00:45:34 |
|      7232 | default |        0 | catalog/search/elasticsearch6_enable_auth          | 0         | 2021-08-17 20:28:46 |
|      7233 | default |        0 | catalog/search/elasticsearch6_server_timeout       | 15        | 2021-08-17 20:28:46 |
|      7234 | default |        0 | catalog/search/elasticsearch6_minimum_should_match | NULL      | 2021-08-17 20:28:46 |
+-----------+---------+----------+----------------------------------------------------+-----------+---------------------+
14 rows in set (0.005 sec)

The ElasticServer hostname and port number settings for ElasticSearch 7 were missing in the core_config_data table.

Add them back:


bin/magento config:set catalog/search/elasticsearch7_server_hostname 127.0.0.1

bin/magento config:set catalog/search/elasticsearch7_server_port 9200

Check ElasticSearch settings in app/etc/env.php

app/etc/env.php


'system' => [
        'default' => [
            'catalog' => [
                'search' => [
                    'engine' => 'elasticsearch6',
                    'elasticsearch6_server_hostname' => 'opensearch',
                    'elasticsearch6_server_port' => '9200',
                    'elasticsearch6_index_prefix' => 'magento'
                ]
            ]
        ]
    ],

The Elasticsearch settings had elasticsearch6 in the value and key names.

Those need to be changed to elasticsearch7.


'system' => [
        'default' => [
            'catalog' => [
                'search' => [
                    'engine' => 'elasticsearch7',
                    'elasticsearch7_server_hostname' => 'opensearch',
                    'elasticsearch7_server_port' => '9200',
                    'elasticsearch7_index_prefix' => 'magento'
                ]
            ]
        ]
    ],

Now, import the settings:


bin/magento app:config:import

Refresh Cache


bin/magento cache:flush

Now, reindex catalogsearch should work fine:


bin/magento indexer:reindex catalogsearch_fulltext

Hope this helps. Thanks.