Elasticsearch-py: The es.update API throwing TransportError(400, 'action_request_validation_exception', 'Validation Failed: 1: script or doc is missing;')

Created on 13 Sep 2017  路  2Comments  路  Source: elastic/elasticsearch-py

In signals.py I want to add body ={
"suggest" : {
"input": [ instance.name,instance.username ],
"weight" : 34
}
}
instance.indexing() # index for the searching
instance.auto_indexing() # index for autocomplete
searchable_user = UserSearch.objects.get(user=instance)
id = searchable_user.user_id
searchable_user = UserSearch.objects.get(user=instance)
id = searchable_user.user_id
if es.exists("autoidx","user_search",id):
body = {"suggest": {"input": [instance.name, instance.username, instance.company,instance.company_address]}}
es.update("autoidx", "user_search", id, body=body)
print("Bye")
The last print is not coming and throwing the above error.I want to add the suggester part into each and every doc in autoidx index so that it can come in autocompletion

Thanks

Most helpful comment

As for the nature of the error you're receiving.
A 400 error indicates that the body your sending to ES is malformed. It is expecting the body to be in the form of

{
    "doc":{},
    "script":{}
}

You can see what the ES API is expecting when doing an update by looking at the docs here: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html#_updates_with_a_partial_document

I think what you need to do is in your if statement, you should format the body so that it conforms to the body that the ES API is expecting.

Please let us know if you have any other issues.

All 2 comments

@riemannzeta1191 FYI you can use the ` character 3 time in a row to create a code block. It makes things a little easier to read. Just a comment for future use :)

for example

body = {
    "suggest" : {
        "input": [ instance.name,instance.username ],
        "weight" : 34
    }
}
instance.indexing() # index for the searching
instance.auto_indexing() # index for autocomplete
searchable_user = UserSearch.objects.get(user=instance)
id = searchable_user.user_id
searchable_user = UserSearch.objects.get(user=instance)
id = searchable_user.user_id
if es.exists("autoidx","user_search",id):
    body = {"suggest": {"input": [instance.name, instance.username, instance.company,instance.company_address]}}
    es.update("autoidx", "user_search", id, body=body)
print("Bye")

As for the nature of the error you're receiving.
A 400 error indicates that the body your sending to ES is malformed. It is expecting the body to be in the form of

{
    "doc":{},
    "script":{}
}

You can see what the ES API is expecting when doing an update by looking at the docs here: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html#_updates_with_a_partial_document

I think what you need to do is in your if statement, you should format the body so that it conforms to the body that the ES API is expecting.

Please let us know if you have any other issues.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alanbacon picture alanbacon  路  3Comments

jaisharma639 picture jaisharma639  路  6Comments

ApproximateIdentity picture ApproximateIdentity  路  5Comments

kuzhao picture kuzhao  路  6Comments

gcornut picture gcornut  路  5Comments