Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Meilisearch & global search #6138

Closed
beNjiox opened this issue Jan 9, 2024 · 1 comment
Closed

Meilisearch & global search #6138

beNjiox opened this issue Jan 9, 2024 · 1 comment

Comments

@beNjiox
Copy link

beNjiox commented Jan 9, 2024

  • Laravel Version: 10.39
  • Nova Version: 4.32.11
  • PHP Version: 8.3.1
  • Database Driver & Version: Postgresql 12
  • Operating System and Version: Mac Os M2 (Somona)
  • Browser type and version: Arc
  • Reproduction Repository: https://github.com/###/###
  • Meilisearch : "meilisearch/meilisearch-php": "^1.5"

Description:

I'm having an issue with the results displayed on the global search when using scout with meilisearch. I have an index "orders" which contains a "public id" of format XXXXX-XXXXX-XXXXX.
When I type an order id, I don't get the results in the order it should be. The issue is not present when searching for the same order in the index views of the resource.

When digging in the SQL query made to be used, the IDs of the rows in the DB are in the correct order. It just get reordered incorrectly.

This looks like a bug that already existed with Algolia: laravel/scout#369.

CleanShot 2024-01-09 at 07 58 13@2x

CleanShot 2024-01-09 at 07 58 59@2x

When searching for the term direct on Meilisearch, it's ordered correctly

CleanShot 2024-01-09 at 08 01 11@2x

Basically, I believe that there's some sort of reordering for the global search that override the original sorting. While trying to dig in the source, I saw the order being "killed" here : vendor/laravel/scout/src/Engines/MeilisearchEngine.php:282 (map method).

CleanShot 2024-01-09 at 08 06 09@2x

CleanShot 2024-01-09 at 08 07 03@2x

CleanShot 2024-01-09 at 08 08 40@2x

Here you can see, the IDs are different than the one expected. I have nothing in my resource class that could override the ordering.

image

Hope this is clear enough, happy to help futher if needed

Detailed steps to reproduce the issue on a fresh Nova installation:

@beNjiox
Copy link
Author

beNjiox commented Jan 9, 2024

By digging further myself, I believe I found the issue.

When queryScoutModelsByIds(Builder $builder, array $ids) is being invoked with a list of IDs, there's a limit being done in the query

        $query = static::usesSoftDelete()
            ? $this->withTrashed() : $this->newQuery();

        if ($builder->queryCallback) {
            call_user_func($builder->queryCallback, $query); // <-- this adds a LIMIT 5
        }

        $whereIn = in_array($this->getScoutKeyType(), ['int', 'integer']) ?
            'whereIntegerInRaw' :
            'whereIn';

        return $query->{$whereIn}(
            $this->qualifyColumn($this->getScoutKeyName()), $ids
        );

The problem is that the result of the query does not contain the most relevant record (in my use case, the query returns 11 records.)

CleanShot 2024-01-09 at 08 36 59@2x

Thus, when the code tries to reorder the results by position, it doesn't work since the original record is not present.

To account for this, If in my resource, I set up

    public static $globalSearchResults = 11;

I see my most relevant record being first, but this is not ideal as it would take a lot of room.

CleanShot 2024-01-09 at 08 39 12@2x


Not sure how this can be fixed, but I hope it will give you ideas.
One way to do it is to not limit the results, but only the display, but that may imply performance concern, I suppose.

@laravel laravel locked and limited conversation to collaborators Jan 9, 2024
@davidhemphill davidhemphill converted this issue into discussion #6142 Jan 9, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant