[elasticsearch-py] problem update array of json

Hello,
this is my first post but I spent a lot of time for searching a solution to my problem.
I have a document of this type:
doc = {
"id": 1,
"sites": {
"google.com": [{
"date": "01/01/1970",
"last_visit": "30/11/2017",
"username": "Matt",
},
{
"date": "01/01/1994",
"last_visit": "25/11/2017",
"username": "George",
}],
"yahoo.com": [{
"date": "01/01/1976",
"last_visit": "30/05/2017",
"username": "Luis",
}]
}
}

This is the script I use to update this document via Python 2.7 (elasticsearch-py for Elasticsearch 6):
body={"script":"ctx._source.sites += params.sites",
"params":{
"sites": {
site_variable: [{
"date": date_var,
"last_visit": visit_var,
"username": user_var,
}]
}
}
})

And this is the error:

Traceback (most recent call last):
File "elastic_test2.py", line 84, in
line2json()
File "elastic_test2.py", line 77, in line2json
"username": user_var
File "build/bdist.cygwin-2.7.0-i686/egg/elasticsearch/client/utils.py", line 76, in _wrapped
File "build/bdist.cygwin-2.7.0-i686/egg/elasticsearch/client/init.py", line 528, in update
File "build/bdist.cygwin-2.7.0-i686/egg/elasticsearch/transport.py", line 314, in perform_request
File "build/bdist.cygwin-2.7.0-i686/egg/elasticsearch/connection/http_urllib3.py", line 161, in perform_request
File "build/bdist.cygwin-2.7.0-i686/egg/elasticsearch/connection/base.py", line 125, in _raise_error
elasticsearch.exceptions.RequestError: TransportError(400, u'action_request_validation_exception', u'Validation Failed: 1: script or doc is missing;

I also tried to enclose body content in ''' and ''' but in that case I have the error of

'Unable to serialize set(....)

For those looking for a solution to this problem,
I solved adding "inline" parameter inside the script.

Eg.:
es.update(
index="test",
doc_type="type",
id=3,
body={
"script" : {
"inline":"ctx._source.data.domains.add(params.newDomain)",
"params":{
"newDomain":"elastic.co"
}
}
})

Bye! :slight_smile:

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.