0

Below is the link to the Scout Elasticsearch Drive which I am using in my Laravel project.

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

I have database table users with the following columns:

id            Integer
name          String
created_at    DateTime

Following the documentation on GitHub, my mapping in the User model looks like followings:

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',
                ],
            ],
        ],

     ]
]

Below is the implementation of the toSearchableArray() function.

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;
}

When using curl -X GET command I get the following results and of course with the custom fields.

"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.
  }
]

In my index() action in my controller, I query the data using the following code.

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

I get the records with only the original attributes, matching those columns in the database table as following.

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

Note that this is a JSON response provided by default by Laravel, responding to API calls. My custom attributes created_date and created_time do exist in Elasticsearch as well. How can I get them too as result? The reason I created these custom fields is to format the date and time on the server side beforehands so that my client-side does not need to worry about formating the date and time in the for-loop.

When I use User::search("*")->explain(); I do get my custom fields as well in the hits.hits._source{} object.

O Connor
  • 4,236
  • 15
  • 50
  • 91

1 Answers1

0

This should give you results directly from Elastic Search without matching up with your related models. I think thats the reason why your custom fields get lost.

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

Documentation

patriziotomato
  • 591
  • 1
  • 11
  • 25