Elasticquent icon indicating copy to clipboard operation
Elasticquent copied to clipboard

I can't create index

Open yannicknya opened this issue 4 years ago • 4 comments

Laravel Framework 6.7.0 elasticquent/elasticquent: dev-master

Hi, every time i try to create an index i get this error : Elasticsearch/Common/Exceptions/BadRequest400Exception with message '{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"Root mapping definition has unsupported parameters: [articles : {_source={enabled=true}, properties={title={analyzer=standard, type=string}}}]"}],"type":"mapper_parsing_exception","reason":"Failed to parse mapping [_doc]: Root mapping definition has unsupported parameters: [articles : {_source={enabled=true}, properties={title={analyzer=standard, type=string}}}]","caused_by":{"type":"mapper_parsing_exception","reason":"Root mapping definition has unsupported parameters: [articles : {_source={enabled=true}, properties={title={analyzer=standard, type=string}}}]"}},"status":400}'

i know this is because of my $mappingProperties, because when I remove it, the index is created well but with all the columns of my model => what I don't want. I would like to specify the fields and type them

My Aticles.php

`<?php namespace App; use Elasticquent\ElasticquentTrait; use Illuminate\Database\Eloquent\Model;

class Article extends Model { use ElasticquentTrait; protected $fillable = ['title', 'body', 'tags']; protected $mappingProperties = array( 'title' => array( 'type' => 'string', 'analyzer' => 'standard' ) ); }`
Can you help me ? thanks you so much

yannicknya avatar Mar 23 '20 23:03 yannicknya

Did you manage to get around this please ? I m facing the same problem.

seifEddineSalah avatar Apr 01 '20 13:04 seifEddineSalah

Same problem here. @timgws

my app\Book.php:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Elasticquent\ElasticquentTrait;

class Book extends Model
{
    use ElasticquentTrait;

    protected $guarded = [];

    /**
     * The elasticsearch settings.
     *
     * @var array
     */
    protected $indexSettings = [
        'analysis' => [
            'char_filter' => [
                'replace' => [
                    'type' => 'mapping',
                    'mappings' => [
                        '&=> and '
                    ],
                ],
            ],
            'filter' => [
                'word_delimiter' => [
                    'type' => 'word_delimiter',
                    'split_on_numerics' => false,
                    'split_on_case_change' => true,
                    'generate_word_parts' => true,
                    'generate_number_parts' => true,
                    'catenate_all' => true,
                    'preserve_original' => true,
                    'catenate_numbers' => true,
                ]
            ],
            'analyzer' => [
                'default' => [
                    'type' => 'custom',
                    'char_filter' => [
                        'html_strip',
                        'replace',
                    ],
                    'tokenizer' => 'whitespace',
                    'filter' => [
                        'lowercase',
                        'word_delimiter',
                    ],
                ],
            ],
        ],
    ];

    protected $mappingProperties = array(
        'title' => array(
            'type' => 'string',
            'analyzer' => 'standard'
        ),
    );

    function getIndexName()
    {
        return 'books';
    }

    function getTypeName()
    {
        return 'books';
    }
}

using tinker:

>>> \App\Book::createIndex($shards = null, $replicas = null);
Elasticsearch/Common/Exceptions/BadRequest400Exception with message '{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"Root mapping definition has unsupported parameters:  [books : {_source={enabled=true}, properties={title={analyzer=standard, type=string}}}]"}],"type":"mapper_parsing_exception","reason":"Failed to parse mapping [_doc]: Root mapping definition has unsupported parameters:  [books : {_source={enabled=true}, properties={title={analyzer=standard, type=string}}}]","caused_by":{"type":"mapper_parsing_exception","reason":"Root mapping definition has unsupported parameters:  [books : {_source={enabled=true}, properties={title={analyzer=standard, type=string}}}]"}},"status":400}'

dendihandian avatar Apr 05 '20 10:04 dendihandian

I created my index manually on elasticsearch

My model : dont forget getTypeName()

STEP 1


<?php

namespace App;
use Elasticquent\ElasticquentTrait;

use Illuminate\Database\Eloquent\Model;


class TransactionModel extends Model
{
	use ElasticquentTrait;
    protected $fillable = ["account_id", "amount" , "type_id" , "date" , "category_id" ,"state"];
    protected $table = 'transaction';
	public $timestamps = false; // car pas de champs update_at
   
	function getIndexName()
	{
		return 'transaction';
	}
		
		
	function getTypeName()
	{
		return '_doc';
	}
}

STEP 2

http://localhost:9200/transaction

PUT
{"settings":{"number_of_shards":1,"number_of_replicas":1},"mappings":{"properties":{"create_date":{"type":"date","format":"yyyy-MM-dd HH:mm:ss"},"write_date":{"type":"date","format":"yyyy-MM-dd HH:mm:ss"},"account_id":{"type":"integer"},"amount":{"type":"integer"},"date":{"type":"date","format":"yyyy-MM-dd"},"category_id":{"type":"integer"},"state":{"type":"integer"},"commentaire":{"type":"text"},"bank_id":{"type":"integer"}}}}

STEP 3 With TINKER

php artisan tinker 

TransactionModel::addAllToIndex();

this is how I managed to get around the issue

yannicknya avatar Apr 08 '20 10:04 yannicknya

Here is my solution: https://dev.to/dendihandian/elasticsearch-eloquent-integration-in-laravel-43kp

dendihandian avatar Apr 16 '20 06:04 dendihandian