首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Yii2弹性搜索扩展-我如何处理类型映射?

Yii2弹性搜索扩展-我如何处理类型映射?
EN

Stack Overflow用户
提问于 2015-10-29 11:23:49
回答 2查看 2K关注 0票数 0

我希望能够在ES索引中存储json对象。下面是我试图存储的一个示例(这是一个序列化模型,一个发送给ES的请求体):

代码语言:javascript
复制
"{"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返回的错误:

代码语言:javascript
复制
"{"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}"

如果没有特定类型的映射(如docs:https://www.elastic.co/guide/en/elasticsearch/reference/1.4/mapping-object-type.html ),我似乎无法做到这一点。

但是,我似乎没有找到在模型中提供映射的方法。扩展的文档并没有真正说明任何关于它的信息。所以,我的问题是:我需要它吗?如果我需要,怎么做?

感谢所有反馈。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-11-05 12:28:39

我假设您的模型是\yii\elasticsearch\ActiveRecord。您需要描述它的属性:

代码语言:javascript
复制
public function attributes()
{
  return [
    'name',
    'category_id',
    'address',
    'phone',
    'site',
    'location',
    'city',
    'description',
    'tags'
  ];
}

不要忘记配置index()type()。在下面的示例中,typemy_address

然后,您将需要使用创建索引适当场映射。下面是您的映射应该是什么样子:

代码语言:javascript
复制
"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"}
    }
  }
}

注意三件事:

  1. 位置为geo_point类型。
  2. 标记被声明为string。这也将允许它们成为字符串数组。
  3. 我不包括id字段。如果它是唯一的,我建议您将yii模型的id设置为必需的值($model->primaryKey = '123')。否则,您的ES模型将其内部id设置为类似于AVDXmfJ3Ou7LzqD1DDMj的内容,并且还有一个不太方便的id字段。

我鼓励您更仔细地查看映射--在配置字符串的准确分析方式时,它们非常重要。

更新:您没有真正描述模型中任何位置的映射的。在迁移中这样做--类似于在SQL中创建表。

票数 1
EN

Stack Overflow用户

发布于 2016-04-13 18:06:24

如果您使用ElasticSearch ActiveRecord,您可以为setupMapping定义一个方法

代码语言:javascript
复制
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"],

                ],
            ],
        ]);
    }
}

稍后,您只需要在任何时候调用此方法,就可以应用新的映射。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33412897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档