App\Models\Product::toESArray() 方法中对boolean类型的值处理有问题
首先代码中已经加了cast:
protected $cast = [
'on_sale' => 'boolean',
];
但是在tinker中运行教材代码还是提示错误:
>>> app('es')->index(['id' => $arr['id'], 'index' => 'products', 'body' => $arr]);
Elasticsearch\Common\Exceptions\BadRequest400Exception with message '{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"failed to parse field [on_sale] of type [boolean] in document with id '29'. Preview of field's value: '1'"}],"type":"mapper_parsing_exception","reason":"failed to parse field [on_sale] of type [boolean] in document with id '29'. Preview of field's value: '1'","caused_by":{"type":"json_parse_exception","reason":"Current token (VALUE_NUMBER_INT) not of boolean type\n at [Source: (ByteArrayInputStream); line: 1, column: 882]"}},"status":400}'
>>> $arr = App\Models\Product::find(29)->toESArray();
=> [
"id" => 29,
"type" => "normal",
"category_id" => 4,
"title" => "手机存储卡",
"long_title" => "这是一个超级牛逼的手机存储卡",
"rating" => 5.0,
"sold_count" => 0,
"review_count" => 0,
"price" => "803.00",
"category" => [
"手机配件",
"存储卡",
],
"category_path" => "-1-",
"description" => "Praesentium repudiandae a est tenetur molestias dolorem.",
"skus" => Illuminate\Support\Collection {#4723
all: [
[
"title" => "voluptatem",
"description" => "Ratione et nulla labore adipisci quia sequi nam.",
"price" => "8991.00",
],
[
"title" => "tenetur",
"description" => "Optio repellat non consequatur iste.",
"price" => "5831.00",
],
[
"title" => "dicta",
"description" => "Harum occaecati nostrum delectus repudiandae animi quasi doloremque.",
"price" => "803.00",
],
],
},
"properties" => Illuminate\Support\Collection {#4708
all: [
[
"name" => "容量",
"value" => "32G",
],
[
"name" => "品牌",
"value" => "金士顿",
],
[
"name" => "容量",
"value" => "64G",
],
],
},
"on_sale" => 1,
]
发现”on_sale”的值是1而不是true,有人遇到类似的问题么?我用的ES是7.13.4版本的。
推荐文章: