I am new to golang and I'm struggling in inserting new documents with the same id in Elasticsearch. Until now I can insert the document correctly in the index, but if the document's id is repeated, the timestamp updates (because is the only thing that is changing). However, what I am looking to do is not to update the document, in other words, do nothing at all.
In the case of Python, to accomplish this I do the following thing:
es = Elasticsearch(
[elastic_url],
http_auth=(elastic_user, elastic_password),
verify_certs=True,
)
return es.create(index=index_name, id=document_id, body=data, ignore=409)
And in golang I'm using the following code:
req := esapi.IndexRequest{
Index: "test_index",
DocumentID: "2",
Body: strings.NewReader(data),
Refresh: "false",
}
res, err := req.Do(ctx, es)
It would be awesome some help! Thank you in advance!
CodePudding user response:
There is an API available in esapi
for checking existence of a document id in a particular index.
ExistRequest - By checking the status code of the response from this request we can confirm whether that document id is present or not (200 if present and 404 if not present)
// prepare existence checking request
req := esapi.ExistsRequest{
Index: index,
DocumentID: id,
}
// send existence checking request
// client here is *elastic.Client
ctx := context.Background()
resp, err := req.Do(ctx, client)
if err != nil {
// handle error
}
status := resp.StatusCode
if status == 200 {
fmt.Println("Exist")
} else if status == 404 {
fmt.Println("Not found")
}