gpt4 book ai didi

elasticsearch - 保存的方法运行迁移时出错

转载 作者:行者123 更新时间:2023-12-02 23:55:59 27 4
gpt4 key购买 nike

在我的 laravel 5.7 应用程序中,我使用 Elasticsearch,在模型中,我使用相关模型的保存和删除方法:

<?php

namespace App;

use DB;
use App\MyAppModel;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;

class Vote extends MyAppModel
{
protected $table = 'votes';
protected $elasticsearch_type = 'vote';
protected $primaryKey = 'id';
public $timestamps = false;

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



protected static function boot() {
parent::boot();

static::deleted(function ($vote ) {
// return; // TO UNCOMMENT
$elastic = app(\App\Elastic\Elastic::class);
$elasticsearch_root_index = config('app.elasticsearch_root_index');
$elasticsearch_type = with(new Vote)->getElasticsearchType();
$elastic->delete([
'index' => $elasticsearch_root_index,
'type' => $elasticsearch_type,
'id' => $vote->id,
]);
});

static::saved(function ($vote) {
// return; // TO UNCOMMENT
$elastic = app(\App\Elastic\Elastic::class);
$elasticsearch_root_index = config('app.elasticsearch_root_index');
$elasticsearch_type = with(new Vote)->getElasticsearchType();

$elastic->delete([
'index' => $elasticsearch_root_index,
'type' => $elasticsearch_type,
'id' => $vote->id,
]);

if ($vote->status == 'A') { // only active votes must be saved in elasticsearch
$elastic->index([
'index' => $elasticsearch_root_index,
'type' => $elasticsearch_type,
'id' => $vote->id,
'body' => [
'id' => $vote->id,
'slug' => $vote->slug,
'name' => $vote->name,
'description' => $vote->description,
]
]);
} // if ($vote->status == 'A') { // only active votes must be saved in elasticsearch

当我在编辑器中修改数据时,它可以正常工作,但运行播种器命令
php artisan migrate:refresh --seed -v

我得到错误:
...
Migrating: 2018_07_13_150151_create_votes_table

Elasticsearch\Common\Exceptions\Missing404Exception : {"_index":"select_vote","_type":"vote","_id":"2","_version":1,"result":"not_found","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":18,"_primary_term":23}

at /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:607
603| $exception = new AlreadyExpiredException($responseBody, $statusCode);
604| } elseif ($statusCode === 403) {
605| $exception = new Forbidden403Exception($responseBody, $statusCode);
606| } elseif ($statusCode === 404) {
> 607| $exception = new Missing404Exception($responseBody, $statusCode);
608| } elseif ($statusCode === 409) {
609| $exception = new Conflict409Exception($responseBody, $statusCode);
610| } elseif ($statusCode === 400 && strpos($responseBody, 'script_lang not supported') !== false) {
611| $exception = new ScriptLangNotSupportedException($responseBody. $statusCode);

Exception trace:

1 Elasticsearch\Connections\Connection::process4xxError([])
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:279

2 Elasticsearch\Connections\Connection::Elasticsearch\Connections\{closure}()
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/react/promise/src/FulfilledPromise.php:25

3 React\Promise\FulfilledPromise::then(Object(Closure))
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/guzzlehttp/ringphp/src/Future/CompletedFutureValue.php:55

4 GuzzleHttp\Ring\Future\CompletedFutureValue::then(Object(Closure))
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/guzzlehttp/ringphp/src/Core.php:341

5 GuzzleHttp\Ring\Core::proxy(Object(GuzzleHttp\Ring\Future\CompletedFutureArray), Object(Closure))
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:299

6 Elasticsearch\Connections\Connection::Elasticsearch\Connections\{closure}(Object(Elasticsearch\Connections\Connection), Object(Elasticsearch\Transport), [])
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:177

7 Elasticsearch\Connections\Connection::performRequest("DELETE", "/select_vote/vote/2", [], [], Object(Elasticsearch\Transport))
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Transport.php:110

8 Elasticsearch\Transport::performRequest("DELETE", "/select_vote/vote/2", [], [])
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Client.php:1553

9 Elasticsearch\Client::performRequest(Object(Elasticsearch\Endpoints\Delete))
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Client.php:265

10 Elasticsearch\Client::delete([])
/mnt/_work_sdb8/wwwroot/lar/Votes/app/Elastic.php:33

11 App\Elastic\Elastic::delete(["select_vote", "vote"])
/mnt/_work_sdb8/wwwroot/lar/Votes/app/Vote.php:142

12 App\Vote::App\{closure}(Object(App\Vote))
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:360

13 Illuminate\Events\Dispatcher::Illuminate\Events\{closure}("eloquent.saved: App\Vote")
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:209

14 Illuminate\Events\Dispatcher::dispatch("eloquent.saved: App\Vote")
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php:162

15 Illuminate\Database\Eloquent\Model::fireModelEvent("saved")
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:692

16 Illuminate\Database\Eloquent\Model::finishSave([])
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:663

17 Illuminate\Database\Eloquent\Model::save()
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:790

18 Illuminate\Database\Eloquent\Builder::Illuminate\Database\Eloquent\{closure}(Object(App\Vote))
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Support/helpers.php:1027

19 tap(Object(App\Vote), Object(Closure))
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:791

20 Illuminate\Database\Eloquent\Builder::create()
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php:23

21 Illuminate\Database\Eloquent\Model::forwardCallTo(Object(Illuminate\Database\Eloquent\Builder), "create")
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1608

22 Illuminate\Database\Eloquent\Model::__call("create")
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1620

23 Illuminate\Database\Eloquent\Model::__callStatic("create")
/mnt/_work_sdb8/wwwroot/lar/Votes/database/seeds/votesInitData.php:92

24 votesInitData::run()
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:29

25 call_user_func_array([])
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:29

...

如果在保存和删除的方法中取消注释返回它可以正常工作(不应对 Elasticsearch )。
为什么错误以及如何修复它?
有没有办法检查它是否从迁移命令中调用并退出?这能解决问题吗?

谢谢!

最佳答案

在类似的情况下,我在迁移脚本开头的设置表中设置了标志,例如

DB::table('settings')->insert([
'name' => 'elastic_automation',
'value' => 'N',
]);

在最后一个迁移脚本中,我更改了 key
Settings::where('name', 'elastic_automation')->update(['value' => 'Y']);

我在我的模型事件中检查了这个键。这对我行得通!

关于elasticsearch - 保存的方法运行迁移时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53359042/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com