Exists method doesnot work

I have the following code which checks if product exists in the index and if it exists it try to delete it.

But exists method always gives me true. I never index the product. so when it try to do some action like get or delete, it gives 404 error

404 Not Found: {"_index":"products","_id":"644f9549c911b","found":false}

here is my code:

$params = ['id' => $product->uid, 'index' => 'products'];
        // check if the document exists in the index
        $product_exists = $this->client->exists($params);

        if ($product_exists) {
            // specify the version of the document to delete
            // here I get 404
            $params['version'] = $this->client->get($params)['_version'];

            // delete the document from the index
            $this->client->delete($params);
            Log::info("Product deleted from the index.");
        } else {
            Log::info("Product does not exist in the index.");
        }

Hi Gurjap, Welcome to Elastic Community. Need more info around this. As i can see you using php sdk to perform operation on Elasticsearch.

What is the output of below query:

GET products/_doc/644f9549c911b
{"_index":"products","_id":"644f9549c911b","found":false}

Which mean the product 644f9549c911b is actually not exists in your index. Can you tell me what response you getting in $product_exists variable:

var_dump($product_exists);

OR

print_r($product_exists);

Psy Shell v0.11.5 (PHP 7.4.33 — cli) by Justin Hileman
> use Elastic\Elasticsearch\ClientBuilder;
> >>> $client = ClientBuilder::create()->setHosts([config('services.elasticsearch.host')])->build();
> => Elastic\Elasticsearch\Client {#4354}
> 
> >>> $params = ['index' => 'products', 'id'=> "644f9549c911b"]
> => [
>      "index" => "products",
>      "id" => "644f9549c911b",
>    ]
> 
> >>> $a = $client->exists($params)
> => Elastic\Elasticsearch\Response\Elasticsearch {#4365}
> 
> >>> var_dump($a)
> object(Elastic\Elasticsearch\Response\Elasticsearch)#4365 (1) {
>   ["asArray":protected]=>
>   uninitialized(array)
>   ["asObject":protected]=>
>   uninitialized(object)
>   ["asString":protected]=>
>   uninitialized(string)
>   ["response":protected]=>
>   object(GuzzleHttp\Psr7\Response)#4367 (6) {
>     ["reasonPhrase":"GuzzleHttp\Psr7\Response":private]=>
>     string(9) "Not Found"
>     ["statusCode":"GuzzleHttp\Psr7\Response":private]=>
>     int(404)
>     ["headers":"GuzzleHttp\Psr7\Response":private]=>
>     array(3) {
>       ["X-elastic-product"]=>
>       array(1) {
>         [0]=>
>         string(13) "Elasticsearch"
>       }
>       ["content-type"]=>
>       array(1) {
>         [0]=>
>         string(52) "application/vnd.elasticsearch+json;compatible-with=8"
>       }
>       ["content-length"]=>
>       array(1) {
>         [0]=>
>         string(2) "57"
>       }
>     }
>     ["headerNames":"GuzzleHttp\Psr7\Response":private]=>
>     array(3) {
>       ["x-elastic-product"]=>
>       string(17) "X-elastic-product"
>       ["content-type"]=>
>       string(12) "content-type"
>       ["content-length"]=>
>       string(14) "content-length"
>     }
>     ["protocol":"GuzzleHttp\Psr7\Response":private]=>
>     string(3) "1.1"
>     ["stream":"GuzzleHttp\Psr7\Response":private]=>
>     object(GuzzleHttp\Psr7\Stream)#4364 (7) {
>       ["stream":"GuzzleHttp\Psr7\Stream":private]=>
>       resource(896) of type (stream)
>       ["size":"GuzzleHttp\Psr7\Stream":private]=>
>       NULL
>       ["seekable":"GuzzleHttp\Psr7\Stream":private]=>
> :

I thought exists method will return true or false. But this giving some obect.

It was my bad. I should not trust on ChatGPT much.

I found the solution. It was returing Guzzle object.
I should use asBool() to make it boolean.

Thank you for your help.

1 Like

Yeah, it might sound right but that's as far as you can go for a lot of it.

Perfect. Thanks

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.