[英]Yii2 Elasticsearch extension - how do I handle type mapping?
我希望能够在我的ES索引中存储一个json对象。 这是我要存储的示例(这是一个序列化模型,一个发送到ES的请求正文):
"{"id":218,"name":"Test2","category_id":1,"address":"Pushkin street","phone":null,"site":null,"location":{"lat":64,"lon":70},"city":"Heaven","description":"Super company","tags":["#test1","#test2"]}"
当我尝试存储它时(当然是通过扩展名),这是ES返回的错误:
"{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"failed to parse [location]"}],"type":"mapper_parsing_exception","reason":"failed to parse [location]","caused_by":{"type":"illegal_argument_exception","reason":"unknown property [lat]"}},"status":400}"
似乎没有特定的类型映射就无法做到这一点,例如在文档中: https : //www.elastic.co/guide/en/elasticsearch/reference/1.4/mapping-object-type.html
但是,我似乎没有找到在模型中提供该映射的方法。 该扩展的文档并没有真正说明任何内容。 所以,我的问题是:我是否完全需要它,如果需要,怎么办?
感谢所有反馈。
我假设您的模型是\\yii\\elasticsearch\\ActiveRecord
。 您需要描述其属性:
public function attributes()
{
return [
'name',
'category_id',
'address',
'phone',
'site',
'location',
'city',
'description',
'tags'
];
}
不要忘记配置index()
和type()
。 在以下示例中, type
为my_address
。
然后,您需要使用正确的字段映射 创建索引 。 映射应如下所示:
"mappings" : {
"my_address" : {
"properties" : {
"name" : { "type" : "string"},
"category_id" : { "type" : "integer"},
"address" : { "type" : "string"},
"phone" : { "type" : "string"},
"site" : { "type" : "string"},
"location" : { "type" : "geo_point"},
"city" : { "type" : "string"},
"description" : { "type" : "string"},
"tags" : { "type" : "string"}
}
}
}
注意三件事:
geo_point
。 string
。 这也将使它们成为字符串数组。 $model->primaryKey = '123'
)。 否则,您的ES模型会将其内部ID设置为AVDXmfJ3Ou7LzqD1DDMj
类的AVDXmfJ3Ou7LzqD1DDMj
并且还具有一个ID字段,该字段不是很方便。 我鼓励您仔细查看这些映射-在配置如何精确分析字符串时它们非常重要。
更新:您并没有真正描述模型中任何地方的映射。 在迁移中进行操作-类似于在SQL中创建表。
如果您使用ElasticSearch ActiveRecord,则可以定义setupMapping的方法
Class BookIndex extends yii\elasticsearch\ActiveRecord
{
/**
* sets up the index for this record
*
*/
public static function setUpMapping()
{
$db = static::getDb();
//in case you are not using elasticsearch ActiveRecord so current class extends database ActiveRecord yii/db/activeRecord
// $db = yii\elasticsearch\ActiveRecord::getDb();
$command = $db->createCommand();
/*
* you can delete the current mapping for fresh mapping but this not recommended and can be dangrous.
*/
// $command->deleteMapping(static::index(), static::type());
$command->setMapping(static::index(), static::type(), [
static::type() => [
// "_id" => ["path" => "id", "store" => "yes"],
"properties" => [
'name' => ["type" => "string"],
'author_name' => ["type" => "string"],
'publisher_name' => ["type" => "string"],
'created_at' => ["type" => "long"],
'updated_at' => ["type" => "long"],
'status' => ["type" => "long"],
],
],
]);
}
}
以后,只要您想应用新的映射,就只需要调用此方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.