Elasticsearch-php: Error: type is required for Index - when creating index.

Created on 27 Mar 2018  路  6Comments  路  Source: elastic/elasticsearch-php

Summary of problem or feature request

An error is thrown when I try to create an index with no type
Elasticsearch\Common\Exceptions\RuntimeException: type is required for Index in /var/www/predelanet/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Endpoints/Index.php:61

Code snippet of problem

$data = [
                'body' => [
                    'brand' => $product->brand,
                    'model' => $product->model,
                    'full_name' => $product->brand .' '. $product->model,
                    'text' => strip_tags($product->text),
                    'stext' => strip_tags($product->stext)
                ],
                'index' => 'products',
                //'type' => 'my_type',
                'id' => 'my_id',
            ];

System details

  • Ubuntu 16.04.3
  • PHP Version - 7.1
  • ES-PHP client version - 6.0.1
  • Elasticsearch version - 6.2.3

Question

What's the sense to demand the _type_ when it is declared _deprecated_ according to the official docs?
Thank you!

Most helpful comment

What's the sense to demand the type when it is declared deprecated according to the official docs?

It's a good question. Unfortunately despite being deprecated, it's still a required component of the URL. Elasticsearch 6.x allows _any_ type name so long as there is only one type per index, but doesn't actually provide a default. E.g. if you try create a document with PUT /foo/1 it will fail because there is no type.

The 6.x docs tend to use the type "_doc" as the convention because the 7.x syntax will be PUT /{index_name}/_doc/{id} so it will match the new version nicely. But technically you can set it to anything you want (foo, my_type, etc). See: https://www.elastic.co/guide/en/elasticsearch/reference/6.x/removal-of-types.html#_schedule_for_removal_of_mapping_types

Because of those two points we still require the type to be specified since it's technically still required.

I suppose we could set a default in the client (_doc like the documentation suggests)... but we try to keep the client opinion-less and that injects at least a temporary opinion on how your data is structured in 6.x.

Let me poke a few other client authors and see what they're thoughts on defaulting to _doc are. Setting a default seems reasonable to me, thanks for the ticket!

All 6 comments

What's the sense to demand the type when it is declared deprecated according to the official docs?

It's a good question. Unfortunately despite being deprecated, it's still a required component of the URL. Elasticsearch 6.x allows _any_ type name so long as there is only one type per index, but doesn't actually provide a default. E.g. if you try create a document with PUT /foo/1 it will fail because there is no type.

The 6.x docs tend to use the type "_doc" as the convention because the 7.x syntax will be PUT /{index_name}/_doc/{id} so it will match the new version nicely. But technically you can set it to anything you want (foo, my_type, etc). See: https://www.elastic.co/guide/en/elasticsearch/reference/6.x/removal-of-types.html#_schedule_for_removal_of_mapping_types

Because of those two points we still require the type to be specified since it's technically still required.

I suppose we could set a default in the client (_doc like the documentation suggests)... but we try to keep the client opinion-less and that injects at least a temporary opinion on how your data is structured in 6.x.

Let me poke a few other client authors and see what they're thoughts on defaulting to _doc are. Setting a default seems reasonable to me, thanks for the ticket!

I support the proposal to add a default type = '_doc'. However, this fails in my copy of ES version 5.6

'{"error":{"root_cause":[{"type":"invalid_type_name_exception","reason":"Document mapping type name can't start with '_', found: [_doc]"}],"type":"invalid_type_name_exception","reason":"Document mapping type name can't start with '_', found: [_doc]"},"status":400}'

Solved by @polyfractal in this comment.

More than 1 year later, this issue still up to date..

@BenGrandin this is not an issue from elasticsearch-php 7.0, we added _doc type as default: https://github.com/elastic/elasticsearch-php/blob/v7.0.0/src/Elasticsearch/Endpoints/Index.php#L42
Are you still using elasticsearch-php 6.x?

@ezimuel You were right. Even if my composer.json had elastic dependency to 7.*, I hadn't the correct version.

I remove my vendor folder and uninstall elastic then re-install. Now that's works !

Thank you for your time,

Ps : I am using docker, and new into Symfony world.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kabudu picture kabudu  路  4Comments

paulhuisman picture paulhuisman  路  6Comments

angelxmoreno picture angelxmoreno  路  3Comments

thinkspill picture thinkspill  路  3Comments

approached picture approached  路  4Comments