meilisearch / meilisearch-laravel-scout

MeiliSearch integration for Laravel Scout
https://github.com/meilisearch/MeiliSearch
MIT License
465 stars 43 forks source link

One Model not importing #137

Closed tfmm closed 3 years ago

tfmm commented 3 years ago

I have 3 models that need to be searchable. 2 of the 3 imported just fine using scout:import. The third model appears to work, and shows that my 500+ items were imported, but when checking the contents of the index, it is empty.

I'm new to using meilisearch, so I may be missing something, but I can't find any logs showing errors or anything like that.

My Melisearch is running in Docker on a remote machine, but as I said, 2 of 3 Models/Indices are working.

Any guidance would be appreciated.

curquiza commented 3 years ago

Hello @tfmm,

Can you check all the updates were correctly processed with this route: https://docs.meilisearch.com/reference/api/updates.html#get-all-update-status

tfmm commented 3 years ago

So, just to double check everything, I created a new meilisearch container and attempted to index everything fresh, and now this model is not creating the index at all, but the others are working.

It is possible I manually created the index previously when I was testing things, so the scout:import may never have created the index for this model.

The other 2 models are still working on the new instance.

Here's the Model for reference, I don't see anything here that should be causing an issue.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use Laravel\Scout\Searchable;
use Spatie\Searchable\Searchable as SpatieSearchable;
use Spatie\Searchable\SearchResult;

/**
 * App\Models\ResourceItem
 *
 * @property int $id
 * @property string $name
 * @property string|null $file_path
 * @property string|null $external_url
 * @property bool $active
 * @property int|null $order
 * @property int $resource_category_id
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read string $link
 * @property-read string $link_ext
 * @property-read string $link_type
 * @property-read \App\Models\ResourceCategory $resource_category
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ResourceItem newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ResourceItem newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ResourceItem query()
 * @mixin \Eloquent
 */
class ResourceItem extends Model implements SpatieSearchable
{
    use Searchable;

    /**
     * The attributes that are mass assignable.
     *
     * @var string[]
     */
    protected $fillable = [
        'name',
        'file_path',
        'external_url',
        'active',
        'order',
        'resource_category_id',
    ];

    public $searchableType = 'Resources';

    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'active' => 'boolean',
        'order' => 'integer',
    ];

    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = ['link_type', 'link', 'link_ext'];

    /**
     * Get the indexable data array for the model.
     *
     * @return array
     */
    public function toSearchableArray()
    {

        $array = [];
        $array['name'] = $this->name;
        return $array;
    }

    public function shouldBeSearchable()
    {
        return $this->active;
    }

    /**
     * @return SearchResult
     */
    public function getSearchResult(): SearchResult
    {
        $url = route('resource.index', $this->resource_category);

        return new SearchResult(
            $this,
            $this->name,
            $url . '#item-' . $this->id
        );
    }

    public function getLinkTypeAttribute(): string
    {
        if (isset($this->external_url)) {
            return 'external';
        }
        if (isset($this->file_path)) {
            return 'local';
        }
        return '';
    }

    public function getLinkAttribute(): string
    {
        return $this->external_url ?? Storage::disk('resources')->url($this->file_path);
    }

    public function getLinkExtAttribute(): string
    {
        if ($this->file_path) {
            return strtolower(cached_fileinfo($this->file_path)['extension']);
        }
        return '';
    }

    /**
     * Get the category that owns the resource.
     */
    // phpcs:disable
    public function resource_category()
    {
        return $this->belongsTo(ResourceCategory::class);
    }
    // phpcs:enable
}

Command I'm using for the import:

php artisan scout:import 'App\Models\ResourceItem'

The Searchable settings in this model match what is being used in the other models which work.

tfmm commented 3 years ago

I discovered and resolved the issue. This model was not feeding the id into the searchableArray. After adding that in, this is now working. Closing this.