meilisearch / meilisearch-php

PHP wrapper for the Meilisearch API
https://meilisearch.com
MIT License
609 stars 97 forks source link

Meilisearch giving error 405 http status #364

Closed rzakhanov closed 2 years ago

rzakhanov commented 2 years ago

Hi dears , meilisearch giving such error when search (request type post)

Fatal error: Uncaught Meilisearch InvalidResponseBodyException: Http Status: 405 thrown in /var/www/html/vendor/meilisearch/meilisearch-php/src/Http/Client.php on line 186

This error dont giving in meilisearch itself servise (related 127.0.0.1:7700)

VPS info:

4 cores | RAM 8 GB | 100 GB (100% NVMe) | 200 Mbit/s

Also i connect meilisearch from php with docker

Docker conf is:

meilisearch:
          image: 'getmeili/meilisearch:latest'
          ports:
          - '7700:7700'
          volumes:
          - './Docker/meilisearch/volume/:/meili_data'
          networks:
            customnetwork:
              ipv4_address: 172.20.0.13
          healthcheck:
              test: ["CMD", "wget", "--no-verbose", "--spider",  "5"]
              retries: 3
              timeout: 5s

Connection define is :

<?php

MEILISEARCH_CONNECT = 'http://172.20.0.13:7700';

const MEILISEARCH_MASTER_KEY = 'masterKey';

const MEILISEARCH_INDEX_KEY = 'products';
brunoocasali commented 2 years ago

Hi @rzakhanov!

Can you give some examples of how you are making the request? The 405 errors on Meilisearch are common when there is a mismatch between meilisearch-php version (latest 0.24.0) and the Meilisearch engine on 0.27.

rzakhanov commented 2 years ago

I found solution . Problem at /src/Endpoints/Indexes.php in 180 line:

This was get method. Example: $this->http->post i changed this to $this->http->get and this worked. Because in your documentation meilisearch updateFilterableAttributes and updateSearchableAttributes method related GET method but in php package was POST . Need fix that. But have new problem again :(


    public function rawSearch(?string $query, array $searchParams = []): array
    {
        $parameters = array_merge(
            ['q' => $query],
            $searchParams
        );

        return $this->http->get(self::PATH.'/'.$this->uid.'/search', $parameters);
    }
rzakhanov commented 2 years ago

New problem errors Fatal error: Uncaught TypeError: Cannot assign null to property MeiliSearch\Search\SearchResult::$nbHits of type int in /var/www/html/vendor/meilisearch/meilisearch-php/src/Search/SearchResult.php:49 Stack trace: #0 /var/www/html/vendor/meilisearch/meilisearch-php/src/Endpoints/Indexes.php(175): MeiliSearch\Search\SearchResult->__construct(Array) #1 /var/www/html/vendor/WEBSOFT/Services/Competition/ContextService/StoreModules/Core/MeiliSearch.php(69): MeiliSearch\Endpoints\Indexes->search('s22', Array) #2 /var/www/html/vendor/WEBSOFT/Http/Models/Competition/Main.php(53): vendor\WEBSOFT\Services\Competition\ContextService\StoreModules\Core\MeiliSearch->search('s22', 1, 1, NULL, NULL) #3 /var/www/html/vendor/WEBSOFT/Http/Controllers/Competition/ListProducts/Run.php(384): vendor\WEBSOFT\Http\Models\Competition\Main->searchFirstSimilar('s22', 1, true, NULL, NULL) #4 /var/www/html/vendor/WEBSOFT/Http/Controllers/Competition/ListProducts/Run.php(398): vendor\WEBSOFT\Http\Controllers\Competition\ListProducts\Run->vendor\WEBSOFT\Http\Controllers\Competition\ListProducts{closure}('s22', 1, true, NULL, NULL) #5 [internal function]: vendor\WEBSOFT\Http\Controllers\Competition\ListProducts\Run->saveProduct() #6 /var/www/html/app/Core/Main/Engine.php(244): call_user_func_array(Array, Array) #7 /var/www/html/app/Run/Conf.php(142): Core\Main\Engine::AppStart() #8 /var/www/html/index.php(11): require_once('/var/www/html/a...') #9 {main} thrown in /var/www/html/vendor/meilisearch/meilisearch-php/src/Search/SearchResult.php on line 49

brunoocasali commented 2 years ago

I'm still not getting what's going on in your case. Sorry about that.

Can you check again which version of the Meilisearch engine you are using? To do that, you can check here https://docs.meilisearch.com/reference/api/version.html#get-version-of-meilisearch

And I want to ensure you have the latest meilisearch-php version 0.24.0, can you share this file with me /src/MeiliSearch.php from your local vendor?

rzakhanov commented 2 years ago

I didn't find problem, but i solved it with custom native curl library in php. For example i using now such code

            return json_decode( $this->customRequest('/indexes/products/search', ['q' => $query,'filter'=> $filter],'POST'),true)['hits']?:[];

at the moment this working. But problem unfortunately it remains.. :(

rzakhanov commented 2 years ago

This is customRequest function source:

  public function customRequest($path = null, $params = [], $method = 'GET')
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, MEILISEARCH_CONNECT . '/' . $path);

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0);
        curl_setopt($curl, CURLOPT_TIMEOUT, 25); //timeout in seconds
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, strtoupper($method));

        if ($params) {
            curl_setopt($curl, CURLOPT_HTTPHEADER, [
                "content-type: application/json; charset=UTF-8",
                "Authorization: Bearer " . MEILISEARCH_MASTER_KEY,
            ]);
            #curl_setopt($curl, CURLOPT_POSTFIELDS,http_build_query($params));
            curl_setopt($curl, CURLOPT_POSTFIELDS, (is_string($params) ? $params : json_encode($params)));
        }

        $res = curl_exec($curl);
        curl_close($curl);

        return $res;
    }
rzakhanov commented 2 years ago

I'm still not getting what's going on in your case. Sorry about that.

Can you check again which version of the Meilisearch engine you are using? To do that, you can check here https://docs.meilisearch.com/reference/api/version.html#get-version-of-meilisearch

And I want to ensure you have the latest meilisearch-php version 0.24.0, can you share this file with me /src/MeiliSearch.php from your local vendor?

Yes of course man : Meilisearch source is:

<?php

declare(strict_types=1);

namespace MeiliSearch;

class MeiliSearch
{
    public const VERSION = '0.23.2';

    public static function qualifiedVersion()
    {
        return sprintf('Meilisearch PHP (v%s)', MeiliSearch::VERSION);
    }
}
brunoocasali commented 2 years ago

If you want to use a plain curl is up to you, but the idea of our SDKs is to help you integrate with Meili easily :)

So, I spotted some trouble in your environment.

First, you must use a version of the Meilisearch engine that is compatible with meilisearch-php. So if you have the engine version 0.27, you should stay on the version of meilisearch-php you have.

But I assume you are using the new version of Meili, so you should run composer update meilisearch/meilisearch-php to get the meilisearch-php v0.24.0, this should solve your problem.

Btw: Our docs only show the latest version of the Meilisearch engine, that's why you got these issues 😄.

rzakhanov commented 2 years ago

If you want to use a plain curl is up to you, but the idea of our SDKs is to help you integrate with Meili easily :)

So, I spotted some trouble in your environment.

First, you must use a version of the Meilisearch engine that is compatible with meilisearch-php. So if you have the engine version 0.27, you should stay on the version of meilisearch-php you have.

But I assume you are using the new version of Meili, so you should run composer update meilisearch/meilisearch-php to get the meilisearch-php v0.24.0, this should solve your problem.

Btw: Our docs only show the latest version of the Meilisearch engine, that's why you got these issues smile.

I will check in next project :) Thanks for helping again ! :)

eroshenko commented 2 years ago

Hi @brunoocasali I have same trouble:

Error notice: MeiliSearch\Exceptions\InvalidResponseBodyException

In

MeiliSearch\Http\Client::parseResponse
vendor/meilisearch/meilisearch-php/src/Http/Client.php:186

Laravel version: 8.* Package version: "0.28.1" "meilisearch/meilisearch-php": "^0.24.1",

My code version one:

$index = $client->index('movies_index_new');
$index->updateSortableAttributes(['cinemas.name']);

My code version two:

        $index = $client->index('movies_index_new');
        $index->updateSettings([
            'sortableAttributes' => [
                'cinemas.name'
            ]
        ]);

I need custom sorting and filtering. But in both code versions I have the error MeiliSearch\Exceptions\InvalidResponseBodyException

But if I use simple code it's worked fine:

return Movie::search($q)->get();

brunoocasali commented 2 years ago

Hi @eroshenko!

Can you confirm the Meilisearch engine version you are using? Because we have tests that cover this code: https://github.com/meilisearch/meilisearch-php/blob/main/tests/Settings/SearchableAttributesTest.php#L25-L31

So the only thing that can break that is if you use a different version :/

eroshenko commented 2 years ago

@brunoocasali Package version: "0.28.1" "meilisearch/meilisearch-php": "^0.24.1",

eroshenko commented 2 years ago

image

eroshenko commented 2 years ago

I'm going to the line 186 and dump response data image

What I saw: image

Response don't have in header 'content-type'

brunoocasali commented 2 years ago

Can you add a breakpoint in the line 185 and send me the complete value of the $response? Also, can you send me the Meilisearch log of that request

like this:

[2022-08-12T20:23:48Z INFO  actix_server::server] Actix runtime found; starting in Actix runtime
[2022-08-12T20:23:50Z INFO  actix_web::middleware::logger] 172.17.0.1 "GET /indexes HTTP/1.1" 200 46 "-" "PostmanRuntime/7.29.2" 0.001288
eroshenko commented 2 years ago

@brunoocasali

Can you add a breakpoint in the line 185 and send me the complete value of the $response?

GuzzleHttp\Psr7\Response {#366
  -reasonPhrase: "Method Not Allowed"
  -statusCode: 405
  -headers: array:4 [
    "content-length" => array:1 [
      0 => "0"
    ]
    "vary" => array:1 [
      0 => "Origin, Access-Control-Request-Method, Access-Control-Request-Headers"
    ]
    "access-control-allow-origin" => array:1 [
      0 => "*"
    ]
    "date" => array:1 [
      0 => "Tue, 16 Aug 2022 14:14:41 GMT"
    ]
  ]
  -headerNames: array:4 [
    "content-length" => "content-length"
    "vary" => "vary"
    "access-control-allow-origin" => "access-control-allow-origin"
    "date" => "date"
  ]
  -protocol: "1.1"
  -stream: GuzzleHttp\Psr7\Stream {#363
    -stream: stream resource @10
      wrapper_type: "PHP"
      stream_type: "TEMP"
      mode: "w+b"
      unread_bytes: 0
      seekable: true
      uri: "php://temp"
      options: []
    }
    -size: null
    -seekable: true
    -readable: true
    -writable: true
    -uri: "php://temp"
    -customMetadata: []
  }
}
MeiliSearch\Exceptions\InvalidResponseBodyException:  in file /home/vagrant/code/kino.wine/vendor/meilisearch/meilisearch-php/src/Http/Client.php on line 186
eroshenko commented 2 years ago

Also, can you send me the Meilisearch log of that request

image

eroshenko commented 2 years ago

Hmn... I restart meili and get the result of search... I'm confused. I'm try more cases

AlexDanault commented 2 years ago

I save the exact same problem as @eroshenko and I am currently investigating this.

My error is:

MeiliSearch\Exceptions\InvalidResponseBodyException
  at vendor/meilisearch/meilisearch-php/src/Http/Client.php:187

Meilisearch log shows:

[2022-08-18T23:20:58Z INFO  actix_web::middleware::logger] 172.29.0.9 "GET /indexes/aquapassionstore_collections HTTP/1.1" 200 180 "-" "Meilisearch PHP (v0.24.2)" 0.001634
[2022-08-18T23:20:58Z INFO  actix_web::middleware::logger] 172.29.0.9 "PUT /indexes/aquapassionstore_collections/settings/filterable-attributes HTTP/1.1" 405 0 "-" "Meilisearch PHP (v0.24.2)" 0.000070

The package meilisearch/meilisearch-php I am using is version v0.24.2.

Downgrading to 0.24.1 and 0.24.0 did not fix the issue.

Downgrading to 0.23.3 FIXED the issue.

So until this is investigated and fixed, this command will save you:

composer require meilisearch/meilisearch-php:0.23.3

brunoocasali commented 2 years ago

Which Meilisearch engine version are you using @AlexDanault ?

AlexDanault commented 2 years ago

Which Meilisearch engine version are you using @AlexDanault ?

Hi Bruno,

888b     d888          d8b 888 d8b                                            888
8888b   d8888          Y8P 888 Y8P                                            888
88888b.d88888              888                                                888
888Y88888P888  .d88b.  888 888 888 .d8888b   .d88b.   8888b.  888d888 .d8888b 88888b.
888 Y888P 888 d8P  Y8b 888 888 888 88K      d8P  Y8b     "88b 888P"  d88P"    888 "88b
888  Y8P  888 88888888 888 888 888 "Y8888b. 88888888 .d888888 888    888      888  888
888   "   888 Y8b.     888 888 888      X88 Y8b.     888  888 888    Y88b.    888  888
888       888  "Y8888  888 888 888  88888P'  "Y8888  "Y888888 888     "Y8888P 888  888

Database path:          "./data.ms"
Server listening on:    "http://0.0.0.0:7700"
Environment:            "development"
Commit SHA:             "unknown"
Commit date:            "unknown"
Package version:        "0.26.1"

This is my version of Meilisearch, I'm using it in a docker container, using the image getmeili/meilisearch:latest .

AlexDanault commented 2 years ago

Also, here's the Meilisearch log when the error happens using v0.24.0+:

[2022-08-18T23:20:58Z INFO  actix_web::middleware::logger] 172.29.0.9 "GET /indexes/aquapassionstore_collections HTTP/1.1" 200 180 "-" "Meilisearch PHP (v0.24.2)" 0.001634
[2022-08-18T23:20:58Z INFO  actix_web::middleware::logger] 172.29.0.9 "PUT /indexes/aquapassionstore_collections/settings/filterable-attributes HTTP/1.1" 405 0 "-" "Meilisearch PHP (v0.24.2)" 0.000070

And here's the log for when there's no error using v0.23.3:

[2022-08-18T23:39:57Z INFO  actix_web::middleware::logger] 172.29.0.9 "GET /indexes/aquapassionstore_collections HTTP/1.1" 404 173 "-" "Meilisearch PHP (v0.23.3)" 0.000833
[2022-08-18T23:39:57Z INFO  actix_web::middleware::logger] 172.29.0.9 "POST /indexes HTTP/1.1" 202 139 "-" "Meilisearch PHP (v0.23.3)" 0.013180
[2022-08-18T23:39:58Z INFO  actix_web::middleware::logger] 172.29.0.9 "GET /indexes/aquapassionstore_collections HTTP/1.1" 200 182 "-" "Meilisearch PHP (v0.23.3)" 0.001316
[2022-08-18T23:39:58Z INFO  actix_web::middleware::logger] 172.29.0.9 "POST /indexes/aquapassionstore_collections/settings/filterable-attributes HTTP/1.1" 202 140 "-" "Meilisearch PHP (v0.23.3)" 0.014299
[2022-08-18T23:39:58Z INFO  actix_web::middleware::logger] 172.29.0.9 "POST /indexes/aquapassionstore_collections/settings/sortable-attributes HTTP/1.1" 202 140 "-" "Meilisearch PHP (v0.23.3)" 0.015330

The difference is the method, as the error message implies.

AlexDanault commented 2 years ago

https://github.com/meilisearch/meilisearch/commit/10a71fdb102096fdce4d724136d23ab2dbdffda3#diff-41a053a24e66c30acb6b769ac33463c3f30235773e9a7c1740c1cbb2f14c2622R123

Looks like this is the commit that changed the faulty method on the route in question.

Before the update, the updte routes were all POST, and now they're PUT :

https://github.com/meilisearch/meilisearch/commit/10a71fdb102096fdce4d724136d23ab2dbdffda3#diff-41a053a24e66c30acb6b769ac33463c3f30235773e9a7c1740c1cbb2f14c2622L103

brunoocasali commented 2 years ago

Hi @AlexDanault, thanks for providing that info,

By quoting the IBM blog:

It is generally better to explicitly define a different sequential tag for your images every time, and not rely on the latest tag.

When you use getmeili/meilisearch:latest you are not actually using the latest version. So that's why you got those problems.

Since you're running Meilisearch "0.26.1":

Commit date: "unknown" Package version: "0.26.1"

You need to use a version that supports v0.26.1 which is https://github.com/meilisearch/meilisearch-php/releases/tag/v0.23.0.

If you want to use a new Meilisearch engine version, you should check the release notes from this PHP repository to make sure you have a compatible version of your library 😃

I hope it helps!

AlexDanault commented 2 years ago

I'm okay with those conclusions, as my stuff was working when I found out the problem and workaround.

Some notes however:

  1. It is annoying that the "latest" of two of your packages don't work together. The latest Docker image doesn't work with the latest PHP package. For this isssue, I think you should update your Docker image to run the 0.28 version of Meilisearch.

  2. It is problematic that the PHP package doesn't do any type of version checking against the server. The server API was changed in MS 0.28.0 , the PHP package v0.24.0 was updated too (good) but should refuse to connect to anything lower than MS 0.28.0, as doing so will reseult in a mysterious crash (the one reported here). A version check would have prevented that.

Letme know if you need anything.

brunoocasali commented 2 years ago
  1. It is annoying that the "latest" of two of your packages don't work together. The latest Docker image doesn't work with the latest PHP package. For this issue, I think you should update your Docker image to run the 0.28 version of Meilisearch.

I understand your frustration, but you need to know that latest tag behavior is not something we can control, because it entirely depends on the users' environment (unfortunately).

I also suggested the docs team change that instruction from the main docs website https://docs.meilisearch.com/learn/getting_started/quick_start.html#setup-and-installation if you check it out, you'll see a specific tag currently v0.28.0 instead of latest to prevent that from happening.

  1. It is problematic that the PHP package doesn't do any type of version checking against the server. The server API was changed in MS 0.28.0 , the PHP package v0.24.0 was updated too (good) but should refuse to connect to anything lower than MS 0.28.0, as doing so will reseult in a mysterious crash (the one reported here). A version check would have prevented that.

That's a pretty good idea, and this could also be applied to other SDKs (we received this kind of feedback before). Unfortunately, due to some time restrictions, we did not have enough time to create a solid plan to implement it yet.

Thanks for using Meilisearch 🤘

brunoocasali commented 2 years ago

I'll close this issue since too many topics were initiated and closed. To better management, I will close this one, but I encourage to open new issues if this is the case :)