Skip to content
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

OrderBy with Meilisearch and Laravel Scout does not work properly #48946

Closed
ismailnakkar opened this issue Nov 8, 2023 · 3 comments
Closed

Comments

@ismailnakkar
Copy link

ismailnakkar commented Nov 8, 2023

Laravel Version

10.31

PHP Version

8.1

Database Driver & Version

No response

Description

OrderBy in Meilisearch does not work properly (Laravel Scout)

When using Meilisearch with Laravel Scout and using the orderBy feature, the results are NOT correct:

Illuminate\Support\Collection {#5719
    all: [
      21637,
      52590,
      67247,
      5060,
      63499,
      78632,
      70287,
      34339,
      67494,
      9276,
    ],
  }

Steps To Reproduce

  • Create Post Model as follows:
public function toSearchableArray(): array
{
    return Arr::only($this->toArray(), [
        self::ID_COLUMN,
        self::TITLE_COLUMN,
        self::DESCRIPTION_COLUMN,
        self::VIEWS_COLUMN,
        self::CLICKS_COLUMN
    ]);
}
  • Add Meilisearch and add the following configs:
  'meilisearch' => [
      'host' => env('MEILISEARCH_HOST', 'http://localhost:7700'),
      'key' => env('MEILISEARCH_KEY'),
      'index-settings' => [
          Post::class => [
               'sortableAttributes'=> [Post::ID_COLUMN, Post::VIEWS_COLUMN, Post::CLICKS_COLUMN],
           ],
      ],
  ],
  • Create fake data:
 return [
      Post::TITLE_COLUMN       => $this->faker->realText(100),
      Post::DESCRIPTION_COLUMN => $this->faker->realText(),
      Post::VIEWS_COLUMN => $views = (int)$this->faker->numberBetween(10, 100000),
      Post::CLICKS_COLUMN => (int) ($views / rand(2, 4))
  ];
  • Get the sorted results:

\App\Models\Post::search('t')->orderBy('views')->get()->pluck('views');

@driesvints
Copy link
Member

@mmachatschek hmm from what I can tell they have the sortableAttributes correct here. Any idea why it's not working for them?

@ismailnakkar
Copy link
Author

It seems that the issue was the rankingRules as Meilisearch was prioritizing "words" more than "sort", so the solution was adding the following:

'rankingRules' => ['sort']

@driesvints
Copy link
Member

Thanks @ismailnakkar. If anything should be made more clear in the docs we'd appreciate a PR 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants