Search code examples
djangoelasticsearchdjango-haystackattributeerror

AttributeError: 'module' object has no attribute 'ElasticSearchError' : Using Haystack Elasticsearch


Using Django & Haystack with ElasticSearch.

After installing haystack and ES, and Rebuilding Index

./manage.py rebuild_index 

WARNING: This will irreparably remove EVERYTHING from your search index in connection 'default'. Your choices after this are to restore from backups or rebuild via the rebuild_index command. Are you sure you wish to continue? [y/N] y

Removing all documents from your index because you said so.
All documents removed.
Indexing 1039 <django.utils.functional.__proxy__ object at 0x10ca3ded0>.

AttributeError: 'module' object has no attribute 'ElasticSearchError'

Updating index has the same problem

/manage.py update_index 
Indexing 1039 <django.utils.functional.__proxy__ object at 0x10ea49d90>.
AttributeError: 'module' object has no attribute 'ElasticSearchError'

Clear index works fine though ( probably because there is no index )

./manage.py clear_index   

WARNING: This will irreparably remove EVERYTHING from your search index in connection    'default'.
Your choices after this are to restore from backups or rebuild via the `rebuild_index` command.
Are you sure you wish to continue? [y/N] y

Removing all documents from your index because you said so. All documents removed.

Versions

django-haystack==2.0.0-beta
pyelasticsearch==0.5
elasticsearch==0.20.6

localhost:9200 says :

{
  "ok" : true,
  "status" : 200,
  "name" : "Jigsaw",
  "version" : {
    "number" : "0.20.6",
    "snapshot_build" : false
  },
  "tagline" : "You Know, for Search"
}

Haystack settings :

HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
        'URL': 'http://127.0.0.1:9200/',
        'INDEX_NAME': 'haystack',
    },
}

search_indexes.py :

import datetime
import haystack
from haystack import indexes
from app.models import City

class CityIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    name = indexes.CharField(model_attr='name')
    state = indexes.CharField(model_attr='state')
    country = indexes.CharField(model_attr='country')
    lat = indexes.FloatField(model_attr='latitude')
    lon = indexes.FloatField(model_attr='longitude')
    alt = indexes.FloatField(model_attr='altitude')
    pop = indexes.IntegerField(model_attr='population')

    def get_model(self):
        return City

Any help - why I am getting error ?


Solution

  • Solved it !

    After debugging the process using pdb

    ./manage.py rebuild_index
    

    At line 222 - in /haystack/backend/elasticsearch_backend.py

    Changed

    except (requests.RequestException, pyelasticsearch.ElasticSearchError), e:
    

    To

    # except (requests.RequestException, pyelasticsearch.ElasticSearchError), e:
    except Exception as inst:
        import pdb; pdb.set_trace()
    

    I found out the core error was this

    'ElasticSearch' object has no attribute 'from_python'.
    

    To which I found solution here - https://github.com/toastdriven/django-haystack/issues/514#issuecomment-4058230

    The version of pyelasticsearch I was using was from http://github.com/rhec/pyelasticsearch,

    So I installed pyelasticsearch from a fork - http://github.com/toastdriven/pyelasticsearch using :

    pip install --upgrade  git+https://github.com/toastdriven/pyelasticsearch.git@3bfe1a90eab6c2dfb0989047212f4bc9fb814803#egg=pyelasticsearch
    

    and That fixed it & Index was build !