Types cannot be provided in put mapping requests,

2020-08-17 07:54发布

问题:

I am using https://github.com/babenkoivan/scout-elasticsearch-driver to implement Elasticsearch with Laravel Scout. Ivan mentions this at Github:

Indices created in Elasticsearch 6.0.0 or later may only contain a single mapping type. Indices created in 5.x with multiple mapping types will continue to function as before in Elasticsearch 6.x. Mapping types will be completely removed in Elasticsearch 7.0.0.

If I understood right here: https://www.elastic.co/guide/en/elasticsearch/reference/master/removal-of-types.html I either need to use:

1) PUT index?include_type_name=true

or, better

2) PUT index/_doc/1 { "foo": "baz" }

I am stucked since I have no idea how to use either 1) or 2)

How can I add the parameter include_type_name=true?

How can I create the right mapping without using the include_type_name parameter?

class TestIndexConfigurator extends IndexConfigurator
{
    use Migratable;
    /**
     * @var array
     */
    protected $settings = [
    ];
    protected $name = 'test';
}

回答1:

Earlier versions of Elasticsearch (<= 5) supported multiple types per index. That meant that you could have different data mappings for each type. With Elasticsearch 6, this was removed and you can only have single mapping type.

Therefore, for Elasticsearch 7 (latest release), you can add an index, setup mappings and add document like this:

  • Create an index

    PUT user
    
  • Add mapping

    PUT user/_mapping 
    {
      "properties": {
        "name": {
          "type": "keyword"
        },
        "loginCount": {
          "type": "long"
        }
      }
    }
    
  • Add document(s)

    PUT user/_doc/1
    {
      "name": "John",
      "loginCount": 4
    }
    
    
  • Check data in the index

    GET user/_search
    

Now, regarding the scout-elasticsearch-driver that you use, after reading the documentation you mentioned, it is simply saying that you need to create separate index configurator for each searchable model, as multiple models cannot be stored inside the same index.

So to create the index, run

php artisan make:index-configurator MyIndexConfigurator

and then

php artisan elastic:create-index App\\MyIndexConfigurator

which will create the index in the elasticsearch for you.

To learn more about elasticsearch, I suggest you install both elasticsearch and kibana to your development machine and then play around with it in kibana - the interface is quite nice and supports autocomplete to ease the learning curve.



回答2:

When I tried GET product/default/_mapping in Kibana console.

I kept getting this error.

"Types cannot be provided in get mapping requests, unless include_type_name is set to true"

This is happening in elastic search 7.3.0. Looks like the above command is no longer supported in latest versions of elastic search.

It worked for me when I remove the default from the above command.

GET product/_mapping