使用elasticquent lib创建Elasticsearch映射时出错

时间:2018-11-29 15:51:21

标签: elasticsearch laravel-5

在我的Laravel 5.7应用程序中,我试图通过composer.json使用elasticsearch:

{
    "require": {
        ...
        "elasticquent/elasticquent": "dev-master",
        ...
    },

我正在阅读这篇文章:https://appdividend.com/2018/06/30/laravel-elasticsearch-tutorial-example/

我想从创建映射和批量数据开始。为此,在我的模型app/Vote.php中:

<?php

namespace App;

use DB; ... use Elasticquent\ElasticquentTrait; ... class Vote extends MyAppModel {
    use ElasticquentTrait; ...


    protected $table = 'votes';
    protected $primaryKey = 'id';
    public $timestamps = false;

    protected $fillable = ['name', 'slug', 'description', 'creator_id', 'vote_category_id', 'is_quiz', 'status', 'image'];
    protected $mappingProperties = array(

        'id'               => [
            'type' => 'integer',
            "analyzer" => "standard",
            'include_in_all' => FALSE
        ], //            'user'    => array(  // THERE would be nested data later //                'type' => 'object', //                'properties' => array( //                    'name'      => array('type' => 'string', 'include_in_all' => TRUE), //                'fullName'  => array('type' => 'string', 'include_in_all' => TRUE, 'boost' => 2) //                ), //            ),
        'name'             => [
            'type' => 'string',
            "analyzer" => "standard",
            'include_in_all' => TRUE
        ],
        'slug'             => [
            'type' => 'string',
            "analyzer" => "standard",
            'include_in_all' => TRUE
        ],
        'description'      => [
            'type' => 'string',
            "analyzer" => "standard",
            'include_in_all' => FALSE
        ],
        'created_at'       => [
            'type' => 'date',
            "analyzer" => "standard",
            'include_in_all' => FALSE
        ],


    public function getTableName(): string
    {
        return $this->table;
    }

    public function getPrimaryKey(): string
    {
        return $this->primaryKey;
    }



    public static function setVotesElasticMapping()
    {

        // Vote::deleteMapping();

        Vote::createIndex(/*$shards = null, $replicas = null*/);

        Vote::putMapping(/*$ignoreConflicts = true*/);

        Vote::addAllToIndex ();
    }
}

但是调用方法setVotesElasticMapping时出现此错误:

{"error":{"root_cause":[{"type":"resource_already_exists_exception","reason":"index [select_vote/IxHrJ300SIWSZ_s12-jAJw] already exists","index_uuid":"IxHrJ300SIWSZ_s12-jAJw","index":"select_vote"}],"type":"resource_already_exists_exception","reason":"index [select_vote/IxHrJ300SIWSZ_s12-jAJw] already exists","index_uuid":"IxHrJ300SIWSZ_s12-jAJw","index":"select_vote"},
"status":400}

很明显,因为我之前已经创建了索引,并且config/elasticquent.php文件中的“ select_vote”是“ default_index”。

取消注释时:

Vote::deleteMapping();

我收到此错误:

Elasticsearch \ Common \ Exceptions \ BadRequest400Exception (405)
{"error":"Incorrect HTTP method for uri [/select_vote/votes/_mapping] and method [DELETE], allowed: [PUT, GET, POST]","status":405}

您能否说出我为什么得到此错误以及如何解决它?无论如何,我认为无论如何我都需要调用此方法,比如说我是否重新构建了映射结构。

1 个答案:

答案 0 :(得分:0)

您的Elasticsearch版本与Eloquent不兼容,您必须安装旧版本的Elasticsearch。