Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating an index with alias fails unless options are specified #24

Closed
Radiergummi opened this issue Mar 29, 2021 · 0 comments
Closed
Assignees
Labels
bug Something isn't working

Comments

@Radiergummi
Copy link
Member

This happens due to the way PHP serializes empty arrays. Elasticsearch expects a request like the following (taken from the docs):

PUT /test
{
  "aliases": {
    "alias_1": {},
    "alias_2": {
      "filter": {
        "term": { "user.id": "kimchy" }
      },
      "routing": "shard-1"
    }
  }
}

The library allows expressing the above fluently:

$connection->createIndex('test', fn(Index $index) => $index
    ->alias('alias_1')
    ->alias('alias_2', [
        'filter' => [
            'term' => [ 'user.id' => 'kimchy' ]
        ]
    ])
    ->alias('routing', 'shard-1')
);

But for the first case, we set an empty array as the default value (as the options are omitted from the call). Now, PHP doesn't know whether an empty array should be serialized to an object or an array, and (somewhat unfortunately) defaults to a JSON array, which triggers the following error:

Elasticsearch\Common\Exceptions\BadRequest400Exception 

  {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"No alias is specified"}],"type":"illegal_argument_exception","reason":"No alias is specified"},"status":400}

  at vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:675
    671▕             $exception = new ScriptLangNotSupportedException($responseBody. $statusCode);
    672▕         } elseif ($statusCode === 408) {
    673▕             $exception = new RequestTimeout408Exception($responseBody, $statusCode);
    674▕         } else {
  ➜ 675▕             $exception = new BadRequest400Exception($responseBody, $statusCode);
    676▕         }
    677▕ 
    678▕         $this->logRequestFail($request, $response, $exception);
    679▕ 

This can be alleviated by defaulting to an instance of \ArrayObject, instead.

@Radiergummi Radiergummi added the bug Something isn't working label Mar 29, 2021
@Radiergummi Radiergummi self-assigned this Mar 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant