# Exists method doesnot work

**URL:** <https://discuss.elastic.co/t/exists-method-doesnot-work/331863>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [May 1, 2023, 11:25am UTC](https://discuss.elastic.co/t/exists-method-doesnot-work/331863 "2023-05-01T11:25:52Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gurjap\_Singh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gurjap_singh/32/120477_2.png) [@Gurjap\_Singh](https://discuss.elastic.co/u/Gurjap_Singh)\
**Post date:** [May 1, 2023, 11:25am UTC](https://discuss.elastic.co/t/exists-method-doesnot-work/331863/1 "2023-05-01T11:25:52Z")

</div>

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:

```auto
$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.");
        }

```

---

<div class="post-metadata">

**Author:** ![ashishtiwari1993](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ashishtiwari1993/32/135241_2.png) [@ashishtiwari1993](https://discuss.elastic.co/u/ashishtiwari1993)\
**Post date:** [May 1, 2023, 2:46pm UTC](https://discuss.elastic.co/t/exists-method-doesnot-work/331863/2 "2023-05-01T14:46:05Z")

</div>

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:

```auto
GET products/_doc/644f9549c911b

```

---

<div class="post-metadata">

**Author:** ![Gurjap\_Singh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gurjap_singh/32/120477_2.png) [@Gurjap\_Singh](https://discuss.elastic.co/u/Gurjap_Singh)\
**Post date:** [May 1, 2023, 5:58pm UTC](https://discuss.elastic.co/t/exists-method-doesnot-work/331863/3 "2023-05-01T17:58:05Z")

</div>

> [@ashishtiwari1993](#):
>
> `644f9549c911b`

```auto
{"_index":"products","_id":"644f9549c911b","found":false}

```

---

<div class="post-metadata">

**Author:** ![ashishtiwari1993](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ashishtiwari1993/32/135241_2.png) [@ashishtiwari1993](https://discuss.elastic.co/u/ashishtiwari1993)\
**Post date:** [May 1, 2023, 11:05pm UTC](https://discuss.elastic.co/t/exists-method-doesnot-work/331863/4 "2023-05-01T23:05:43Z")

</div>

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);`

---

<div class="post-metadata">

**Author:** ![Gurjap\_Singh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gurjap_singh/32/120477_2.png) [@Gurjap\_Singh](https://discuss.elastic.co/u/Gurjap_Singh)\
**Post date:** [May 2, 2023, 4:57am UTC](https://discuss.elastic.co/t/exists-method-doesnot-work/331863/5 "2023-05-02T04:57:10Z")

</div>

> [@ashishtiwari1993](#):
>
> 644f9549c911b

```auto
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]=>
> :

```

---

<div class="post-metadata">

**Author:** ![Gurjap\_Singh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gurjap_singh/32/120477_2.png) [@Gurjap\_Singh](https://discuss.elastic.co/u/Gurjap_Singh)\
**Post date:** [May 2, 2023, 4:58am UTC](https://discuss.elastic.co/t/exists-method-doesnot-work/331863/6 "2023-05-02T04:58:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Gurjap\_Singh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gurjap_singh/32/120477_2.png) [@Gurjap\_Singh](https://discuss.elastic.co/u/Gurjap_Singh)\
**Post date:** [May 2, 2023, 5:27am UTC](https://discuss.elastic.co/t/exists-method-doesnot-work/331863/7 "2023-05-02T05:27:38Z")

</div>

> [@Gurjap\_Singh](#):
>
> ```auto
> $a = $client->exists($params)
> > => Elastic\Elasticsearch\Response\Elasticsearch {#4365}
> 
> ```

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.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/6/d/6d30ee0a1bd61b1fca25fb0c1b9fc272b746b9e3.png)

Thank you for your help.

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [May 2, 2023, 6:40am UTC](https://discuss.elastic.co/t/exists-method-doesnot-work/331863/8 "2023-05-02T06:40:52Z")

</div>

> [@Gurjap\_Singh](#):
>
> It was my bad. I should not trust on ChatGPT much.

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

---

<div class="post-metadata">

**Author:** ![ashishtiwari1993](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ashishtiwari1993/32/135241_2.png) [@ashishtiwari1993](https://discuss.elastic.co/u/ashishtiwari1993)\
**Post date:** [May 2, 2023, 10:52am UTC](https://discuss.elastic.co/t/exists-method-doesnot-work/331863/9 "2023-05-02T10:52:59Z")

</div>

Perfect. Thanks

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 30, 2023, 10:53am UTC](https://discuss.elastic.co/t/exists-method-doesnot-work/331863/10 "2023-05-30T10:53:24Z")

</div>

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