Can I create a document with the update API if the document doesn’t exist yet

This is doable using the update api. It does require that you define the id of each document, since the update api requires the id of the document to determine its presence.

Given an index created with the following documents:

PUT /cars/car/1 
{ "color": "blue", "brand": "mercedes" }
PUT /cars/car/2
{ "color": "blue", "brand": "toyota" }

We can get the upsert functionality you want using the update api with the following api call.

POST /cars/car/3/_update
{
    "doc": {
        "color" : "brown",
        "brand" : "ford"
    },
    "doc_as_upsert" : true
}

This api call will add the document to the index since it does not exist.

Running the call a second time after changing the color of the car, will update the document, instead of creating a new document.

POST /cars/car/3/_update
{
    "doc": {
        "color" : "black",
        "brand" : "ford"
    },
    "doc_as_upsert" : true
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)