Alexander Horner's avatar

Laravel Scout returns empty array on get() but works with raw()

As described in the title, my Model using Laravel Scout returns an empty array when retrieving a search with ->get(), but gives a seemingly normal result using ->raw().

Here's my Model:

<?php

namespace App\Models;

use Illuminate\Support\Str;

// Respurces:
// https://laravel.com/docs/8.x/eloquent

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use \Soundasleep\Html2Text;
use \Orbit\Concerns\Orbital;
use Laravel\Scout\Searchable;
use \Mtownsend\ReadTime\ReadTime;


class Article extends Model
{
    use Orbital, Searchable;

    protected $fillable = [
        'title',
        'link',
        'category',
        'author',
        'published_on',
        'thumbnail',
        'thumbnail_slide'
    ];

    public $timestamps = false;

    public static function schema(Blueprint $table)
    {
        $table->string('title')->default("Titel des Artikels");
        $table->string('link')->default("link-zum-Artikel");
        $table->string('category')->default("Allgemein");
        $table->string('author')->nullable()->default(null);
        $table->timestamp('published_on')->nullable()->default('1.4.10');
        $table->string('thumbnail')->default('name_of_thumbnail_image');
        $table->string('thumbnail_slide')->default('name_of_slide_image');
    }

    public function toSearchableArray()
    {
        return [
            'title' => $this->title,
            'link' => $this->link,
            'category' => $this->category,
            'author' => $this->author,
            'published_on' => $this->published_on,
            'plaintext' => $this->plaintext()
        ];
    }

    public function getKeyName()
    {
        return 'link';
    }
    
    public function getIncrementing()
    {
        return false;
    }

    public function plaintext()
    {
        $htmlToTextOptions = array(
            'ignore_errors' => true,
            'drop_links' => true
        );

        $textOnly = Html2Text::convert($this->content, $htmlToTextOptions);
        $oneLine = str_replace(array("\r","\n")," ", $textOnly);
        $onlyOneSpaceAllowed = preg_replace('!\s+!', ' ', $oneLine);

        return $onlyOneSpaceAllowed;
    }

    public function read_time()
    {
        return (new ReadTime($this->plaintext()))->get();
    }

}

The result when I run Article::search("link")->get():

>>> Article::search('anfahrt')->get()
=> Illuminate\Database\Eloquent\Collection {#4643
     all: [],
   }

The result when I run Article::search('anfahrt')->raw():

>>> Article::search('anfahrt')->raw()
[!] Aliasing 'Article' to 'App\Models\Article' for this Tinker session.
=> [
     "hits" => [
       [
         "title" => "Adresse und Anfahrt an das Schulgelände",
         "link" => "anfahrt",
         "category" => "Allgemein",
         "author" => "Jan Bayer",
         "published_on" => "29.01.2022 15:00",
         "plaintext" => "Die Adresse des redacted",
         "objectID" => "anfahrt",
         "_highlightResult" => [
           "title" => [
             "value" => "Adresse und <em>Anfahrt</em> an das Schulgelände",
             "matchLevel" => "full",
             "fullyHighlighted" => false,
             "matchedWords" => [
               "anfahrt",
             ],
           ],
           "link" => [
             "value" => "<em>anfahrt</em>",
             "matchLevel" => "full",
             "fullyHighlighted" => true,
             "matchedWords" => [
               "anfahrt",
             ],
           ],
           "category" => [
             "value" => "Allgemein",
             "matchLevel" => "none",
             "matchedWords" => [],
           ],
           "author" => [
             "value" => "Jan Bayer",
             "matchLevel" => "none",
             "matchedWords" => [],
           ],
           "published_on" => [
             "value" => "29.01.2022 15:00",
             "matchLevel" => "none",
             "matchedWords" => [],
           ],
           "plaintext" => [
             "value" => "Die Adresse des Willibald-Gluck-Gymnasium Neumarkt i.d.Opf. lautet: Woffenbacher Straße 33 92381 Neumarkt Es befindet sich hier:",
             "matchLevel" => "none",
             "matchedWords" => [],
           ],
         ],
       ],
     ],
     "nbHits" => 1,
     "page" => 0,
     "nbPages" => 1,
     "hitsPerPage" => 20,
     "exhaustiveNbHits" => true,
     "exhaustiveTypo" => true,
     "query" => "anfahrt",
     "params" => "query=anfahrt",
     "renderingContent" => [],
     "processingTimeMS" => 2,
   ]
0 likes
3 replies
JerrelZ's avatar

Old thread but I had the same issue.

The issue for me was wrongly imported id's. The solution was to scout:flush the model and then scout:import it again.

1 like

Please or to participate in this conversation.