meilisearch / meilisearch-laravel-scout

MeiliSearch integration for Laravel Scout
https://github.com/meilisearch/MeiliSearch
MIT License
465 stars 43 forks source link

A primary key is already present. It's impossible to update it #105

Closed fla closed 3 years ago

fla commented 3 years ago

New instalation of the latest version of Laravel 8, with one some users in the database.

When running this command: php artisan scout:import "App\Models\User"

The response is:

MeiliSearch\Exceptions\HTTPRequestException 

  A primary key is already present. It's impossible to update it

  at [Project Path]\vendor\meilisearch\meilisearch-php\src\Http\Client.php:182
    178▕     private function parseResponse(ResponseInterface $response)
    179▕     {
    180▕         if ($response->getStatusCode() >= 300) {
    181▕             $body = json_decode($response->getBody()->getContents(), true) ?? $response->getReasonPhrase();
  ➜ 182▕             throw new HTTPRequestException($response->getStatusCode(), $body);
    183▕         }
    184▕
    185▕         return json_decode($response->getBody()->getContents(), true);
    186▕     }

  1   [Project Path]\vendor\meilisearch\meilisearch-php\src\Http\Client.php:165
      MeiliSearch\Http\Client::parseResponse(Object(GuzzleHttp\Psr7\Response))

  2   [Project Path]\vendor\meilisearch\meilisearch-php\src\Http\Client.php:101
      MeiliSearch\Http\Client::execute(Object(GuzzleHttp\Psr7\Request))

Seems like this scout driver is making the post request to 127.0.0.1:63076 "POST /indexes/users/documents?primaryKey=id HTTP/1.1" 400 232 "-" "GuzzleHttp/7" 0.000152 and because the post contains the query param primaryKey=id the import fails.

This seems to be an issue with the meilisearch documentation as it clearly states on this page

If you want to set the primary key of your index through this route, it only has to be done the first time you add documents to the index. After which it will be ignored if given.

However when I test the post with Insomnia/Postman the same error happens if I add ?primaryKey=id so the documentation is wrong or meilisearch doesn't handle properly the primaryKey properly (shouldn't raise an error if the primaryKey is the same with the one already defined on the index). I'll probably open an issue on the meilisearch documentation repo too but this issue needs to be fixed in this scout driver too.

I've been trying to set up this scout driver in my project for the last 2 days and seems like there are a couple of more issues and I started to question myself, is meilisearch production ready? They seem to change a lot of things and leave documentation behind with the updates. Does anyone else use it in production for let's say 1M small documents?

curquiza commented 3 years ago

Hello @fla

I don't succeed to reproduce the issue you mention on Postman

What is the version of MeiliSearch you are using? We had this issue on a MeiliSearch release candidate (RC) of the v0.19.0 but we fix it and this behavior does not exist in the v0.19.0.

If you use docker can you launch those commands to launch MeiliSearch:

docker pull getmeili/meilisearch:latest # Fetch the latest version of MeiliSearch image from Docker Hub
docker run -it --rm -p 7700:7700 getmeili/meilisearch:latest ./meilisearch --master-key=masterKey

if you don't use docker, can you download it this way:

curl -L https://install.meilisearch.com | sh
./meilisearch

If it does not fix anything and you are already using the v0.19.0 of MeiliSearch can you exactly detail the steps you've done with Postan/Insomnia on the MeiliSearch repository? Because this would be a MeiliSearch issue and not an issue related to the laraval-scout plugin.

fla commented 3 years ago

You are right, it works with docker so I think the problem is on my setup. I am using windows10 so I had to build it from source (v0.19.0 not RC). This might be the issue but I can't think of any reason, I'll just use docker for now.

Thank you for helping me figure this out.