主要字段名称(document = True)

Ada*_*ver 1 python django django-haystack

Django Haystack 文档说:

**Warning**
When you choose a document=True field, it should be consistently named across all of your SearchIndex classes to avoid confusing the backend. The convention is to name this field text.

There is nothing special about the text field name used in all of the examples. It could be anything; you could call it pink_polka_dot and it won’t matter. It’s simply a convention to call it text.
Run Code Online (Sandbox Code Playgroud)

但我不明白这意味着什么.这是他们的示例模型:

从myapp.models import中的haystack导入索引导入datetime

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, using=None):
        """Used when the entire index for model is updated."""
        return self.get_model().objects.filter(pub_date__lte=datetime.datetime.now())
Run Code Online (Sandbox Code Playgroud)

我引用的文字是指我的模型主要字段,并且我应该将其称为"文本"或search_indexes.py中定义的类?

如果在search_indexes.py中的类,它在上面的示例中附加到哪个字段名称?它没有model_attr!

text = indexes.CharField(document=True, use_template=True)
Run Code Online (Sandbox Code Playgroud)

如果我的实际应用程序模型,我希望如何重构一个项目与许多应用程序调用他们的主要文本字段"文本"!

请指教.谢谢.

Ben*_*tin 7

您的SearchIndex定义不需要反映您的模型定义,它需要将来自不同模型的数据映射到通用搜索文档.

  1. 为什么文本字段需要一致地命名?
  2. 地图内容是如何获取的?(为什么没有model_attr关键字)

Haystack文档建议您的SearchIndex字段应在您的SearchIndex定义中一致地命名- 而不是您的模型字段需要一致地命名.搜索索引定义和模型定义之间存在重大区别.您不需要担心模型字段和搜索字段之间的1-1映射.

退出模型并首先考虑要搜索的内容.您会通过常见的搜索视图搜索几种不同的模型吗?假设您有两种型号:

class Note(models.Model):
    title = models.CharField(max_length=40)
    body = models.TextField()

class Memo(models.Model):
    subject = models.CharField(max_length=50)
    content = models.TextField()
    author = models.ForeignKey(StaffMember)
Run Code Online (Sandbox Code Playgroud)

我们想要创建一个简单的搜索视图,该视图仅搜索模型的主要内容以及内容对象的名称或名称(名称,标题,主题等).

这是一个糟糕的例子(不要这样做):

class NoteIndex(indexes.SearchIndex, indexes.Indexable):
    body = indexes.CharField(document=True, use_template=True)
    title = indexes.CharField(model_attr='title')

    def get_model(self):
        return Note

class MemoIndex(indexes.SearchIndex, indexes.Indexable):
    content = indexes.CharField(document=True, use_template=True)
    subject = indexes.CharField(model_attr='subject')

    def get_model(self):
        return Memo
Run Code Online (Sandbox Code Playgroud)

在这个错误示例中,每个搜索索引定义了主要内容字段和内容名称字段(标题或主题).但你现在怎么搜索呢?如果您根据subject您将错过的Note内容对内容运行查询,同样如果您查询body.

更好的例子(这样做):

class NoteIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    title = indexes.CharField(model_attr='title')

    def get_model(self):
        return Note

class MemoIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    title = indexes.CharField(model_attr='subject')

    def get_model(self):
        return Memo
Run Code Online (Sandbox Code Playgroud)

请注意,字段名称不一定与模型字段名称匹配.您只需定义SearchIndex字段应从哪个模型属性获取其数据.

您在搜索引擎中搜索文档,而不是数据库中的行,因此SeachIndex定义将数据库中的内容(一个表或多个查询)映射到搜索文档.该SearchIndex定义是一个转型,每个SearchField你指定转换数据.

至于你关于失踪的问题model_attr,那只是拉入内容的一种方式.您还可以从模板中呈现文本内容,这是text上面的字段所做的(请参阅该文档SearchField API文档).该model_attr源适用于简单的字符字段.