4

将 Django 和 Haystack 与 ElasticSearch 一起使用。

安装完 haystack 和 ES,并重建索引后

./manage.py rebuild_index 

警告:这将无法挽回地从连接“默认”中的搜索索引中删除所有内容。在此之后您的选择是从备份中恢复或通过rebuild_index命令重建。您确定要继续吗?[是/否] 是

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'

更新索引有同样的问题

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

清除索引工作正常(可能是因为没有索引)

./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

因为您这么说,所以从您的索引中删除所有文档。删除所有文件。

版本

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

本地主机:9200 说:

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

干草堆设置:

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

任何帮助 - 为什么我会出错?

4

1 回答 1

2

解决了!

使用 pdb 调试进程后

./manage.py rebuild_index

在第 222 行 - 在 /haystack/backend/elasticsearch_backend.py

改变了

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

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

我发现核心错误是这个

'ElasticSearch' object has no attribute 'from_python'.

我在这里找到了解决方案 - https://github.com/toastdriven/django-haystack/issues/514#issuecomment-4058230

我使用的 pyelasticsearch 版本来自http://github.com/rhec/pyelasticsearch

所以我从一个叉子安装了 pyelasticsearch - http://github.com/toastdriven/pyelasticsearch使用:

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

并且修复了它并建立了索引!

于 2013-04-13T14:05:33.800 回答