Elasticsearch-py: TypeError: string indices must be integers

Created on 28 Nov 2016  路  8Comments  路  Source: elastic/elasticsearch-py

If a request (client.index in my case) causes Transport error, trying to print the error results in

TypeError: string indices must be integers

from env/lib/python3.5/site-packages/elasticsearch/exceptions.py:55:

 cause = ', %r' % self.info['error']['root_cause'][0]['reason']

where self.info['error'] is a string ('MapperParsingException[...' in my case).

Most helpful comment

Never mind, I realized the issue.

For people who may be slow to realize it like I was:

If you do have a version mismatch, fix it, then be sure to restart your ipython notebook kernel after you do. Rerunning the import cells does not do the job. %load_ext autoreload with %autoreload might work, but I haven't tested that.

Anyway, all set.

All 8 comments

@mjrk
Can you provide a minimum reproduction? I'm unable to reproduce this.

test.py:

#!/usr/bin/env python

from elasticsearch import TransportError

def bar():
    try:
        raise TransportError(599, "Error!", {})
    except TransportError as terr:
        print(terr)

bar()

(virtenv) > python --version
Python 3.5.2
(virtenv) > ./test.py
TransportError(599, 'Error!')

You need an index 'test' with a doc type that has a created_at field with type date_time, python 3.5.2:

    import elasticsearch
    raw_model = {
        'created_at': '2016-11-30T09:14:18.010790'
    }
    try:
        client = elasticsearch.Elasticsearch(hosts=["localhost:9200"])
        result = client.index(
            index='test',
            doc_type="doc_type_where_created_at_is_date_time",
            id="someid",
            body=raw_model
        )
    except elasticsearch.exceptions.TransportError as e:
        str(e)
        # => TypeError: string indices must be integers

The actually is the MapperParsingException because of the date format (missing tz info).

I get the error in elasticsearch-py 2.4 and 5.0.0. I quickly worked around it:

        if e.info:
            if isinstance(e.info['error'], str):
                e.info['error'] = {
                    'root_cause': [
                         {
                            'reason': e.info['error']
                         }
                    ]
                }

This looks like a mismatch between your elasticsearch and elasticsearch-py version (#337). Are you sure you are using the correct version of the library for your version of elasticsearch?

Thanks!

You are right!

I installed the 2.4 (using esvm) but different node.js installs got confused and I did not notice the older version was running ...

As I did not find a built-in version assertion, I added one and leave it here for reference:

version = client.info()['version']['number']
from distutils.version import StrictVersion
assert StrictVersion(version) >= StrictVersion("2.0.0")

Sorry for the confusion and thanks a lot for the hint!

Hello,

Sorry to comment on this as it has been closed a while, but I am having the same problem and I don't think that it is a version mismatch for me.

Code below, from documentation page:

from datetime import datetime
from elasticsearch import Elasticsearch

doc = {
    'author': 'kimchy',
    'text': 'Elasticsearch: cool. bonsai cool.',
    'timestamp': datetime.now(),
}
res = es.index(index="test-index", doc_type='tweet', id=1, body=doc)
print(res['created'])

gives:

TypeError                                 Traceback (most recent call last)
c:\anaconda\envs\bighead\lib\site-packages\IPython\core\interactiveshell.pyc in run_code(self, code_obj, result)
   2900             if result is not None:
   2901                 result.error_in_exec = sys.exc_info()[1]
-> 2902             self.showtraceback()
   2903         else:
   2904             outflag = 0

c:\anaconda\envs\bighead\lib\site-packages\IPython\core\interactiveshell.pyc in showtraceback(self, exc_tuple, filename, tb_offset, exception_only)
   1830                                             value, tb, tb_offset=tb_offset)
   1831 
-> 1832                     self._showtraceback(etype, value, stb)
   1833                     if self.call_pdb:
   1834                         # drop into debugger

c:\anaconda\envs\bighead\lib\site-packages\ipykernel\zmqshell.pyc in _showtraceback(self, etype, evalue, stb)
    509             u'traceback' : stb,
    510             u'ename' : unicode_type(etype.__name__),
--> 511             u'evalue' : py3compat.safe_unicode(evalue),
    512         }
    513 

c:\anaconda\envs\bighead\lib\site-packages\ipython_genutils\py3compat.pyc in safe_unicode(e)
     63     """
     64     try:
---> 65         return unicode_type(e)
     66     except UnicodeError:
     67         pass

c:\anaconda\envs\bighead\lib\site-packages\elasticsearch\exceptions.pyc in __str__(self)
     53         try:
     54             if self.info:
---> 55                 cause = ', %r' % self.info['error']['root_cause'][0]['reason']
     56         except LookupError:
     57             pass

TypeError: string indices must be integers

version for elastic-py = 6.2.2
version of elasticsearch = "version" : { "number" : "6.2.2",...

Thank you for any thoughts in advance,
Tommy

Never mind, I realized the issue.

For people who may be slow to realize it like I was:

If you do have a version mismatch, fix it, then be sure to restart your ipython notebook kernel after you do. Rerunning the import cells does not do the job. %load_ext autoreload with %autoreload might work, but I haven't tested that.

Anyway, all set.

I am having this issue with using elastic cloud. the version on cloud is 6.5.4 it seems, how do i fix this? I dont see a 6.5.4 version for the pypi package. please help

@aliirz you don't need to match the version of elasticsearch-py with the version of Elasticsearch, down to the patch level. The only requirement is that the major versions match.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

grepsr picture grepsr  路  3Comments

tlinhart picture tlinhart  路  5Comments

edemauro picture edemauro  路  4Comments

alanbacon picture alanbacon  路  3Comments

kuzhao picture kuzhao  路  6Comments