laravel / scout

Laravel Scout provides a driver based solution to searching your Eloquent models.
https://laravel.com/docs/scout
MIT License
1.54k stars 327 forks source link

Typesense driver makes unnecessary api cals #818

Closed ingria closed 5 months ago

ingria commented 5 months ago

Scout Version

10.8.5

Scout Driver

Typesense

Laravel Version

10

PHP Version

8.3.3

Database Driver & Version

No response

SDK Version

No response

Meilisearch CLI Version

No response

Description

At the moment, typesence-driver makes two http requests on any model update, even if the changed field is not in the document schema. This causes performance problems if there are a lot of updates.

Real life example: I have a collection with over 10000 documents in it. Each document is updated hourly. Each update changes one model attribute, which triggers 2 request to typesense server. That's 5.5 completely unnecessary requests per second.

This issue has been fixed in Typesense official driver (https://github.com/typesense/laravel-scout-typesense-driver/issues/23), but not in Laravel official driver.

Steps To Reproduce

1) Create a model with two attributes:

// MyModel.php
public function toSearchableArray(): array {
    return [
        'id' => (string) $this->id,
        'name' => $this->name,
    ];
}

// config/scout.php
[
  'typesense' => [
    'model-settings' => [
      'App\Models\MyModel' => [
        'collection-schema' => [
          'name' => 'my-model',
          'fields' => [
            [
              'name' => 'id',
              'type' => 'string',
            ],
            [
              'name' => 'name',
              'type' => 'string',
            ],
          ],
        ],
      ],
    ],
  ],
];

2) Update any non-existent attribute:

MyModel::first()->update([
    'some_field' => 'test',
]);

// scout sends GET to /collections/my-model
// scout sends POST to /collections/my-model/documents/import

MyModel::first()->update([
    'some_date' => now(),
]);

// scout sends GET to /collections/my-model
// scout sends POST to /collections/my-model/documents/import
AbdullahFaqeir commented 5 months ago

Hey @ingria,

I'll make sure to submit a PR to fix this in the scout official driver.

CC : @jasonbosco

github-actions[bot] commented 5 months ago

Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!

AbdullahFaqeir commented 5 months ago

Fixed with #820, should be closed.