简体   繁体   English

Django-Haystack 没有找到任何字段

[英]Django-Haystack not finding any field

I am trying to write a little search engine with django-haystac and whoosh.我正在尝试用 django-haystac 和 whoosh 编写一个小搜索引擎。 I adapted their tutorial, I've created my index from a JSON file and query it successfully with QueryParser and now I'm trying to use their view.我改编了他们的教程,我从 JSON 文件创建了我的索引,并使用 QueryParser 成功查询它,现在我正在尝试使用他们的视图。

when I try to access the search url at: http://127.0.0.1:8000/my_search/ I get the error:当我尝试在以下位置访问搜索 url 时: http://127.0.0.1:8000/my_search/我收到错误:

The index 'PaperIndex' must have one (and only one) SearchField with document=True.索引 'PaperIndex' 必须有一个(并且只有一个)SearchField 且 document=True。

If I remove the search_indexes.py I can access the search page, but then of course it does not work as it doesn't have any indecies to search.如果我删除了 search_indexes.py,我可以访问搜索页面,但是它当然不起作用,因为它没有任何不妥搜索。

By debugging it seems it does not pickup any fields, but it does see the class.通过调试,它似乎没有拾取任何字段,但它确实看到了 class。

I tried several things but nothing worked我尝试了几件事,但没有任何效果

my search_indexes.py:我的 search_indexes.py:

from haystack import indexes
from my_search.models import Paper


class PaperIndex(indexes.SearchIndex, indexes.Indexable):
    """
    This is a search index
    """

    title = indexes.CharField(model_attr='title'),
    abstract = indexes.CharField(document=True,
       use_template=False, model_attr='abstract'),


    def get_model(self):
        return Paper

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

my models.py:我的模型.py:

from django.db import models


class Paper(models.Model):
    paper_url = models.CharField(max_length=200),
    title = models.CharField(max_length=200),
    abstract = models.TextField()
    authors = models.CharField(max_length=200),
    date = models.DateTimeField(max_length=200),

    def __unicode__(self):
        return self.title

thanks!谢谢!

Haystack uses an extra field for the document=True field. Haystack 为document=True字段使用了一个额外的字段。 The text = indexes.CharField(document=True) is not on the model, and haystack dumps a bunch of search-able text in there. text = indexes.CharField(document=True)不在 model 上,干草堆在其中转储了一堆可搜索的文本。

Haystack provides a helper method prepare_text() to populate this field. Haystack 提供了一个辅助方法prepare_text()来填充此字段。 Alternatively, the template method can be used, which is simply a txt file with django template style model properties on it.或者,可以使用模板方法,它只是一个带有 django 模板样式 model 属性的 txt 文件。

class PaperIndex(indexes.SearchIndex, indexes.Indexable):
    """
    This is a search index
    """

    text = indexes.CharField(document=True)

    title = indexes.CharField(model_attr='title'),
    abstract = indexes.CharField(model_attr='abstract'),

    def get_model(self):
        return Paper

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM