【问题标题】:How to also get custom fields of documents from Elasticsearch using Laravel Scout Elasticsearch Driver?如何使用 Laravel Scout Elasticsearch Driver 从 Elasticsearch 获取自定义文档字段?
【发布时间】:2020-01-15 11:31:40
【问题描述】:

下面是我在 Laravel 项目中使用的 Scout Elasticsearch Drive 的链接。

https://github.com/babenkoivan/scout-elasticsearch-driver

我的数据库表 users 包含以下列:

id            Integer
name          String
created_at    DateTime

按照 GitHub 上的文档,我在 User 模型中的映射如下所示:

use Searchable; // using Searchable trait.

protected $mapping = [
     'properties' => [
         'name' => [
            'type' => 'text',
            'fields' => [
                'raw' => [
                    'type' => 'keyword',
                ],
            ],
         ],
         'created_at' => [
            'type' => 'date',
            'format' => 'dd-MM-yyyy HH:mm',
            'fields' => [
                'raw' => [
                    'type' => 'keyword',
                ],
            ],
        ],
        // Custom field...
        'created_date' => [
            'type' => 'date',
            'format' => 'dd-MM-yyyy',
            'fields' => [
                'raw' => [
                    'type' => 'keyword',
                ],
            ],
        ],
        // Custom field...
        'created_time' => [
            'type' => 'date',
            'format' => 'HH:mm',
            'fields' => [
                'raw' => [
                    'type' => 'keyword',
                ],
            ],
        ],

     ]
]

下面是toSearchableArray()函数的实现。

public function toSearchableArray()
{
    $array = $this->toArray();

    // Pre-format the custom fields before inserting them into Elasticsearch.
    $array['created_date'] = $this->created_at->format('d-m-Y');
    $array['created_time'] = $this->created_at->format('h:i');

    return $array;
}

使用curl -X GET 命令时,我得到以下结果,当然还有自定义字段。

"hits" : [
  {
      "_source" : {
          "id" : 3,
          "name": "John Doe",
          "created_at": "31-12-2018 23:59",
          "created_date": "31-12-2018", // My custom field.
          "created_time": "23:59" // My custom field.
  }
]

在我的控制器中的index() 操作中,我使用以下代码查询数据。

public function index() 
{
     return User::search("*")->get();
}

我得到的记录只有原始属性,与数据库表中的那些列匹配如下。

[
   {
       "id" : 3,
       "name": "John Doe",
       "created_at": "31-12-2018 23:59",
   }
]

请注意,这是 Laravel 默认提供的 JSON 响应,用于响应 API 调用。我的自定义属性 created_datecreated_time 也确实存在于 Elasticsearch 中。我怎样才能得到它们呢?我创建这些自定义字段的原因是事先在服务器端格式化日期和时间,这样我的客户端就不必担心在 for 循环中格式化日期和时间。

当我使用User::search("*")->explain(); 时,我确实在hits.hits._source{} 对象中获得了我的自定义字段。

【问题讨论】:

    标签: json elasticsearch custom-fields custom-attributes laravel-scout


    【解决方案1】:

    这应该会直接为您提供来自 Elastic Search 的结果,而无需与您的相关模型匹配。我认为这就是您的自定义字段丢失的原因。

    public function index() 
    {
         return User::search("*")->raw();
    }
    

    Documentation

    【讨论】:

      猜你喜欢
      • 2019-04-28
      • 1970-01-01
      • 2017-12-18
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 2021-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多