Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find Index by Alias in Elasticsearch php client api

I am creating search application. When I reindex data into elasticsearch there should not be downtime while reindexing. I want to make reindexing process with zero downtime.I'm trying to do this:

Find the old index with alias. Create new index and fill with new data Remove alias and delete old index Give new index alias

How we can do this using php client library.

like image 584
Rahul Hedaoo Avatar asked Aug 23 '16 11:08

Rahul Hedaoo


1 Answers

I don't get why People are giving him down-votes, the question is straight forward, and the docs for elastic-search are not easy to follow!

Anyway here's the solution:

class SomeClass
{
    /** @var \Elasticsearch\Client */
    private $client;

    /**
     * @param \Elasticsearch\Client $client
     */
    public function __construct(\Elasticsearch\Client $client)
    {
        $this->client = $client;
    }

    /**
     * @param string $aliasName
     *
     * @return null|string
     */
    public function findIndexNameByAlias($aliasName)
    {
        $aliases = $this->client->indices()->getAliases();
        foreach ($aliases as $index => $aliasMapping) {
            if (array_key_exists($aliasName, $aliasMapping['aliases'])) {
                return $index;
            }
        }

        return null;
    }
}

$someClass = new SomeClass(new \Elasticsearch\Client());
echo "Index associated with 'MyAlias': " . $someClass->findIndexNameByAlias('MyAlias');
like image 183
Shehabic Avatar answered Oct 19 '22 07:10

Shehabic