Elasticsearch-php: Error when creating an alias in an index template

Created on 10 Jun 2014  ·  2Comments  ·  Source: elastic/elasticsearch-php

Hi,

I want to create an index template roughly like so:

        $this->getElasticsearchClient()
            ->indices()
            ->putTemplate([
                'name' => 'my_template',
                'body' => [
                    'template' => 'index-matcher-*',
                    'aliases' => [
                        // register an alias for each new index so that we can
                        // perform index migrations without downtime
                        // only use the read- prefixed indices to query for data
                        'read-{index}' => [],
                    ],
                ],
            ])
        ;

However, I get a 400 BadRequest with the error ElasticsearchIllegalArgumentException[No alias specified]

If I use 'read-{index}' => ['a' => 'b'], instead, it works.
Are options to the index alias mandatory? Specifying dummy options just to have an associative array seems to circumvent that?

Most helpful comment

Ah, this is an unfortunate quirk of PHP and json_encode. You can specify an empty JSON object ({}) by using an explicit empty PHP object. Try this:

 $this->getElasticsearchClient()
            ->indices()
            ->putTemplate([
                'name' => 'my_template',
                'body' => [
                    'template' => 'index-matcher-*',
                    'aliases' => [
                        // register an alias for each new index so that we can
                        // perform index migrations without downtime
                        // only use the read- prefixed indices to query for data
                        'read-{index}' => new \stdClass(),
                    ],
                ],
            ])
        ;

If that doesn't work, I'll break out the debugger and see what's going on. :)

All 2 comments

Ah, this is an unfortunate quirk of PHP and json_encode. You can specify an empty JSON object ({}) by using an explicit empty PHP object. Try this:

 $this->getElasticsearchClient()
            ->indices()
            ->putTemplate([
                'name' => 'my_template',
                'body' => [
                    'template' => 'index-matcher-*',
                    'aliases' => [
                        // register an alias for each new index so that we can
                        // perform index migrations without downtime
                        // only use the read- prefixed indices to query for data
                        'read-{index}' => new \stdClass(),
                    ],
                ],
            ])
        ;

If that doesn't work, I'll break out the debugger and see what's going on. :)

Hi, thanks! I figured something like this might be going on. stdClass works.

Was this page helpful?
0 / 5 - 0 ratings