【问题标题】:django-haystack won't index my datadjango-haystack 不会索引我的数据
【发布时间】:2012-04-25 19:01:18
【问题描述】:

我正在按照haystack documentation. 上的说明进行操作

我没有得到 SearchQuerySet().all() 的结果。

我认为问题出在这里

$ ./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 0 notes. // <-- here 0 notes!

mysite/note/search_indexes.py 看起来像

import datetime
import haystack
from haystack import indexes
from note.models import Note

class NoteIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    author = indexes.CharField(model_attr='user')
    pub_date = indexes.DateTimeField(model_attr='pub_date')

    def get_model(self):
        return Note

    def index_queryset(self):
        """Used when the entire index for model is updated."""
        return self.get_model().objects.filter(pub_date__lte=datetime.datetime.now())

我有 mysite/note/templates/search/indexes/note/Note_text.txt

{{ object.title }}
{{ object.user.get_full_name }}
{{ object.body }}

Debugging haystack document 提及

你有运行 haystack.autodiscover 的 search_sites.py 吗?

您是否在主 haystack.site 上注册了您的模型(通常 在你的 search_indexes.py)?

但在第一篇文章中没有提到 search_sites.py 、 haystack.autodiscover 、 haystack.site 。
我很混乱。他们的文档是否处理不同的 haystack 版本?

我的设置是..

haystack 版本 2.0.0.beta
django 1.3.1
solr 3.6.0
sqlite 3

【问题讨论】:

  • 在我的(愚蠢的)情况下,rebuild_index 没有获得任何产品的索引,因为没有任何产品(数据库连接不正确)。

标签: solr indexing django-haystack


【解决方案1】:
def index_queryset(self):
        """Used when the entire index for model is updated."""
        return self.get_model().objects.filter(pub_date__lte=datetime.datetime.now())

是罪魁祸首。

我不知道为什么,但是注释掉可以解决问题。
我猜我系统中的“时间”有点混乱。

【讨论】:

    【解决方案2】:

    应该是……

    def index_queryset(self, using=None):

    我不知道这是否会解决您的问题,但这是该方法的正确签名。

    【讨论】:

      【解决方案3】:

      删除 def index_queryset(self) 是有意义的。它构建了一个常规的 Django ORM QuerySet,它决定将哪些对象放入全文索引。您的示例 index_queryset 将对象限制为仅过去的时间戳现在之前)。

      所以,您确实遇到了日期时间处理问题。检查您的 SQL 数据库的时区及其存储时间的方式。

      UTC 语言环境中的时间戳比纽约和美国大部分地区早大约 +5 小时。 SQLite 通过在未来选择 UTC 时间给我带来了同样的问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-21
        • 1970-01-01
        • 2015-10-22
        • 1970-01-01
        • 2012-11-16
        相关资源
        最近更新 更多