Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch js how to upsert a document

I would like to do a upsert for a document. Is it currently possible to do that via nodejs elasticsearch API?

https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html

I looked through the API, and I could only see an upsert option for update.

Does that mean currently there aren't any way for me to upsert a document?

Thanks

like image 570
Zhen Liu Avatar asked Feb 06 '18 22:02

Zhen Liu


People also ask

How do I update a document in Elasticsearch?

Updates a document using the specified script. If the Elasticsearch security features are enabled, you must have the index or write index privilege for the target index or index alias. Enables you to script document updates.

How to Upsert a document using NodeJS elasticseach API?

till now there is no upsert method for NodeJS Elasticseach API so work around you can check the existence of document if it not exist insert it if it exist update it. Show activity on this post. Set the doc_as_upsert parameter to true when calling the update method to upsert a document.

What are the security features of Elasticsearch?

If the Elasticsearch security features are enabled, you must have the index or write index privilege for the target index or index alias. Enables you to script document updates. The script can update, delete, or skip modifying the document.

How do I get Elasticsearch to always reindex a document?

By default, the document is only reindexed if the new _source field differs from the old. Setting detect_noop to false will cause Elasticsearch to always update the document, even if it hasn’t changed.


1 Answers

Set the doc_as_upsert parameter to true when calling the update method to upsert a document.

const response = await client.update({
  index: 'myindex',
  type: 'mytype',
  id: '1',
  body: {
    doc: {
      title: 'Updated'
    },
    doc_as_upsert: true
  }
});
like image 57
Tibor Baksa Avatar answered Nov 17 '22 05:11

Tibor Baksa