Why I do not see any algolia global-search request with search method of laravel/scout 10?

26 Views Asked by At

I added laravel/scout 10 to my laravel 10 app and added Scout support to my app/Models/Task.php model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
...
use Laravel\Scout\Searchable;

class Task extends Model
{
    use HasFactory;
    use Searchable;

    protected $table = 'tasks';
    protected $primaryKey = 'id';
    public $timestamps = false;

    /**
     * Get the name of the index associated with the model.
     */
    public function searchableAs(): string
    {
        return 'tasks_index';
    }

    public function toSearchableArray(): array
    {
        ...
    }

    /**
     * Determine if the model should be searchable.
     */
    public function shouldBeSearchable(): bool
    {
        return true;
    }

When I fill tasks table with migration seeder or by API commands I create data on algolia, which are syncronized with I see valid data on algolia server :

enter image description here

But search method works differently then I read at https://laravel.com/docs/10.x/scout as running code in controller :

<?php

namespace App\Http\Controllers;

use App\Models\Task;
use Illuminate\Routing\Controller;

class HomeController extends Controller
{
    public function readAlgoliaData()
    {
        $request= request();

        $tasks = Task::search($request->search)->get();
        echo '<pre>'.count($tasks).'::$tasks::'.print_r($tasks,true).'</pre>';

    }
}

I see that I data are read from local database asI see sql-request in telescope :

select
*
from
  `tasks`
where
  `tasks`.`id` in (
    99,
    98,
    97,
    96,
    95,
    94,
    93,
    92,
    91,
    90,
    9,
    89,
    88,
    87,
    86,
    85,
    84,
    83,
    82,
    81
)

Just last rows in my db. I do not see any algolia global-search request : a) in my browser console

b) in telescope(if it can show algolia global-search request ?)

c) In Thunder Client of my Visual Studio Code if it can show algolia global-search request ?)

What can be wrong and how debug my requests ?

ADDITIVE : I removed laravel/scout package and installed algolia/scout-extended:^3.0", but again with code :

$tasks = Task::search($request->search)->get();

I se that rows are read from db with sql-request on my side and no "global-search" request in my browser.

I removed "->get()" from code4 line above and check that "ScoutExtended" is returned :

Algolia\ScoutExtended\Builder Object
(
    [model] => App\Models\Task Object
        (
            [connection:protected] =>
            [table:protected] => tasks
            [primaryKey:protected] => id
            [keyType:protected] => int
            [incrementing] => 1
            [with:protected] => Array
                (
                )

I run on my local kubuntu 22.04 with apache 2 and php 8.2 with local server I run with

php artisan  serve

command.

I tried to follow some step by step manuals, like video https://www.youtube.com/watch?v=m4ejQ2xKrEA but anyway I see that data are read from db, when I expect data read from algolia server...

ADDITIVE # 2: I try to debug this problem, but in my phpstorm searching for "search" substring under "/vendor/algolia" subdirectory I did not find any entry...

I found Builder class defined in vendor/algolia/scout-extended/src/Builder.php file and dumping it :

class Builder extends BaseBuilder
{
    public function __construct($model, $query, $callback = null, $softDelete = false)
    {
        dd($model);
        parent::__construct($model, (string) $query, $callback, $softDelete);
        $this->optionalFilters = collect();
    }

I see :

enter image description here

I dump method in the same file:

    /**
     * Get the results of the search.
     *
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public function get()
    {
        echo '::GET $this::'.print_r($this,true);
        $data = $this->engine()->get($this);
        dd($data);
        return $this->engine()->get($this);
    }
// I see also :  

enter image description here

 ...

    /**
     * Get the engine that should handle the query.
     *
     * @return mixed
     */
    protected function engine()
    {
        return $this->model->searchableUsing();  // I did not find definition of this method - phpstorm did not help...
        // I tried to search under "/vendor" subdirectory but failed...
    }

and in sql-traces I see that data are returned from db...

Any hints how to salve this issue or debug it ?

ADDITIVE # 3 :

I found this https://www.youtube.com/watch?v=2o6_eRtdm-4 video with importing data from Laravel app into Algolia and reading data from Algolia in laravel app.

I have installed the source of the project locally (I used Algolia credentials of my app) and checking telescope queries see the same : Post entries which are read from Algolia are also searched in laravel app with Post model and I found sql lines in logs :

select
*
from
`tasks`
where
`posts`.`id` in (
99,
98,
97
...
)

Looks like misunderstood how Algolia works and in which cases in can be used.

I can not read data from Algolia which are not in my mysql db ?

I moved Post model(but not posts table with data) from the example app and try to read posts data(which were imported in example app) from algolia, but got error :

ObjectID seems invalid. You may need to re-import your data using the `scout-reimport` Artisan command.

Dose it mean that I can not read Algolia data which were imported by some other app ?

0

There are 0 best solutions below