Reputation: 5380
What is the most elegant way to insert a new document (if not already exists) or update (increase counter by 1) of an already existed document?
This one:
res = elasticsearch.update(
index='stories-test',
doc_type='news',
id=1,
body={
"doc":
{
"author": "me",
"visits": 1
},
'doc_as_upsert': True
},
script={
"inline": "ctx._source.visits += visit",
"params": {
"visit": 1
}
}
)
Troughs the following error:
RequestError: TransportError(400, u'action_request_validation_exception', u"Validation Failed: 1: can't provide both script and doc;")
Upvotes: 3
Views: 1588
Reputation: 1323
You can include "doc"
field in the body to update.
es = Elasticsearch()
doc = NewsSerializer(news).data
es.update(index="news_index", doc_type='news', id=1, body={"doc": doc})
Upvotes: 1
Reputation: 361
You can't use the update query with both doc and script params. You can do all the stuff in the script
field using the params
field in it.
You may find more information in this post:
Upvotes: 0