How to check elasticsearch is enabled programmatically Magento 2?

Magento 2.4 Out of the box supports Primary Search Engines is Elasticsearch.

If your site having some third party search engine installed or want to know about programmatically for the elastic search is enable or not in your code, you can know it using the given code snippet.

Just instantiate the Magento\Elasticsearch\Model\Config class to your __construct() method.

<?php
namespace Jesadiya\Elasticsearch\Model;

use Magento\Elasticsearch\Model\Config;

class ConfigData
{
    /**
     * @var Config
     */
    private $config;

    public function __construct(
        Config $config
    ) {
        $this->config = $config;
    }

    /**
     * @return bool
     */
    public function isElasticsearchEnabled()
    {
        return $this->config->isElasticsearchEnabled();
    }
}

Check with the given syntax,

$result = $this->isElasticsearchEnabled();

The result will be true if your site has enabled elastic search as default search engine.

Output:
Boolean