teamtnt / laravel-scout-tntsearch-driver

Driver for Laravel Scout search package based on https://github.com/teamtnt/tntsearch
MIT License
1.1k stars 144 forks source link

Laravel Scout with TntSearch doesn't return certain search terms #348

Closed darioparrinello closed 2 years ago

darioparrinello commented 2 years ago

Hello, please go easy on me as I just began my journey into coding.

I have an issue with TntSearch in Laravel and I really can't wrap my head around it. To summarize, I want to search a model by three attributes: city, region and type. While searching for "city" and "region" works fine, "type" gives me some weird problems. It has four options "Bowl", "Pipe", "Park" and "Street", but only "Park" and "Pipe" are giving back results, while "Bowl" and "Street" return zero hits even tho there are some records.

If I search for "park" it works

unknown1

What makes it even more strange to me is the fact that if I search for a term that actually doesn't exist I get the usual page "No posts for this search terms"

unknown

while if I search for one of those two terms, I have the same empty page but with pagination controls.

unknowwn

Any help would be hugely appreciated.

I have this in my model

<?php

namespace App\Models;

use App\Models\Image;
use App\Models\Region;
use Laravel\Scout\Searchable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Spot extends Model
{
    use HasFactory, Searchable;

    protected $fillable = [
        'name',
        'city',
        'address',
        'type',
        'description',
        'image',
        'user_id', 
    ];

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

        $array = [
            'id' => $this->id,
            'name' => $this->name,
            'city' => $this->city,
            'type' => $this->type,
            'region' => $region,
        ];
        return $array;
    }

    public function user(){
        return $this->belongsTo(User::class);
    }

    public function region(){
        return $this->belongsTo(Region::class);
    }

}

The route in web.php

Route::get('/ricerca/spot', [FrontController::class, 'searchSpots'])->name('spots.search');

And the function in my controller is

public function searchSpots(Request $request)
{
    $spots = Spot::search($request->searched)->where('is_accepted', true)->paginate(8);
    return view('spot.index', compact('spots'));
}

scout.php is configured

'tntsearch' => [
    'storage'  => storage_path(), //place where the index files will be stored
    'fuzziness' => env('TNTSEARCH_FUZZINESS', true),
    'fuzzy' => [
        'prefix_length' => 2,
        'max_expansions' => 50,
        'distance' => 2
    ],
    'asYouType' => false,
    'searchBoolean' => env('TNTSEARCH_BOOLEAN', false),
    'maxDocs' => env('TNTSEARCH_MAX_DOCS', 500),
],
darioparrinello commented 2 years ago

Found the solution by myself. I had to execute

php artisan scout:flush "App\Models\Spot"

and import it again.