nitefrog
nitefrog

Reputation: 1830

How do I get passed Elasticsearch Updating or Inserting using Python TypeError?

I have a weird issue. If I use curl with the payload I am sending to Elasticsearch I have no issues. When I try to pass JSON to elastic via a web service using Python I get, depending on how I set the payload

To help debug this I took the code out and ran it inline to try to figure it out.

To create the record I run the following.

edit_author1 = {"name": "Sheldon Sid", "myid": 18}
resp = es.index(index="radsearch", doc_type="default", id=18, body={"doc": edit_author1})

This works for the initial creation of the record when using es.create or es.index and it returns the following just as it does if I use curl from the terminal.

{
  "_index": "radsearch",
  "_type": "default",
  "_id": "18",
  "_version": 1,
  "found": true,
  "_source": {
    "doc": {
      "name": "Sheldon Sid",
      "myid": 18
    }
  }
}

It works as expected. When I try to run an update where I have the following

resp = es.update(index="radsearch", doc_type="default", id=18, body={'doc': edit_author1})

The above returns

{
  "_index": "radsearch",
  "_type": "default",
  "_id": "18",
  "_version": 2,
  "found": true,
  "_source": {
    "doc": {
      "name": "Sheldon Sid",
      "myid": 18
    **},
    "myid": 18,
    "name": "Sheldon Sid"
  }**
}

But the document repeats itself as a separate entry under doc. If I run it via curl it updates the document and does not create a second entry as above.

If I try to run create or update without the doc key I get a TypeError: unhashable type: 'dict'

resp = es.update(index="radsearch", doc_type="default", id=18, body={edit_author1})
TypeError: unhashable type: 'dict'

If I try to run update as

resp = es.update(index="radsearch", doc_type="default", id=18, body=edit_author1)

I get

elasticsearch.exceptions.RequestError: 
TransportError(400, 'action_request_validation_exception', 
                    'Validation Failed: 1: script or doc is missing;')

Any ideas on how to fix this as I am stuck?

Kind Regards.

Upvotes: 1

Views: 2488

Answers (2)

Praneeth
Praneeth

Reputation: 751

The index query is using a rather unnecessary field doc, while the update query is working as expected it updated the _source , not the doc inside it.

For the error, the body used isn't valid. Use

resp = es.update(index="radsearch", doc_type="default", id=18, 
 body={"doc": edit_author1})

Refer: example-usage

Upvotes: 1

nitefrog
nitefrog

Reputation: 1830

Finally found the answer. It is a formatting issue. I was close. In order to use the Elasticsearch API you need to append doc for you are going to run an update.

For the initial insert this works

edit_author1 = {"name": "Sheldon Sid", "myid": 18}

When you want to update you will need to modify it like this

edit_author1 = {"doc":{"name": "Sheldon Sid2", "myid": 18})

Notice the doc in front. If you do not add the doc on an update and try to run the command

resp = es.update(index="myindex",
                     doc_type="default",
                     id=18,
                     body=edit_author1)

You will receive the error

elasticsearch.exceptions.RequestError: 
TransportError(400, 'action_request_validation_exception', 
                'Validation Failed: 1: script or doc is missing;')

Upvotes: 4

Related Questions