3

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 2

1

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})
Sign up to request clarification or add additional context in comments.

Comments

0

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:

Elastic Search Partial Update

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.