How to insert new or update old document using elasticsearch-py?

Viewed 1460

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;")
2 Answers
Related