N
N
nicolaa2020-11-24 08:48:12
elasticsearch
nicolaa, 2020-11-24 08:48:12

How to check for the existence of id in elasticsearch?

There is a function for deleting a record from the database and deleting a record from ElasticSearch by id from the database. I encountered
a problem that there is a record in the database, but there is no record in ElasticSearch, and when trying to delete a non-existent record from ES, an error occurs
How to check for the existence of id in ES?

Tried like this -

$items = $elastic->search([
            'index' => 'baza',
            'type' => 'prod',
            'body' => [
                "query" => [
                    "ids" => [
                        "values" => [$id]
                    ]
                ]
            ]
        ]);

But I get an error from Laravel
Elasticsearch\Common\Exceptions\Missing404Exception
{"_index":"baza","_type":"prod","_id":"1569568","_version":1,"result":"not_found","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":500770,"_primary_term":1}

5fbc9e9a699f4055947381.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nicolaa, 2020-11-25
@nicolaa

After reading the documentation, I found a suitable way for me

GET my-index-000001/_search
{
  "query": {
    "terms": {
      "_id": [ "1", "2" ] 
    }
  }
}

In conjunction with Laravel / ElasticSearch, it turned out like this -
$items = $elastic->search([
            'index' => 'baza',
            'body' => [
                "query" => [
                    "terms" => [
                        "_id" => [ $id ]
                    ]
                ]
            ]
        ]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question