主字段名(document = True) [英] Main field name (document=True)

查看:147
本文介绍了主字段名(document = True)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django Haystack docs say

Django Haystack docs say:

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

但是我不明白什么意思。这是他们的示例模型:

But I don't get what it means. This is their example model:

import datetime
from haystack import indexes
from myapp.models import注意

import datetime from haystack import indexes from myapp.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, using=None):
        """Used when the entire index for model is updated."""
        return self.get_model().objects.filter(pub_date__lte=datetime.datetime.now())

我引用的文字是引用我的模型主域并且说我应该称之为文本或者在search_indexes.py中定义的类?

Is the text I quoted referring to MY model main field and saying I should call it "text" or to the class defined in search_indexes.py?

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

If to the class in search_indexes.py, where is the field name it's attached to in the example above? It doesn't have model_attr!

text = indexes.CharField(document=True, use_template=True)

如果对我的实际应用程序模型,我预计将重构一个项目与许多应用程序,以称其主文本字段文字!

And if to my actual app models, how I am expected to refactor a project with many many apps to call their main text field "text"!

请指教。谢谢。

推荐答案

您的 SearchIndex 定义不需要反映您的模型定义,需要将来自不同模型的数据映射到常见的搜索文档。

Your SearchIndex definition does not need to reflect your model definition, it needs to map data from different models to a common search document.


  1. 为什么文本字段需要一直命名?

  2. 地图内容是如何来源的? (为什么没有 model_attr 关键字)

  1. Why does the text field need to be named consistently?
  2. How is map content sourced? (Why is there no model_attr keyword)

干草堆文档建议您的 SearchIndex 字段应在您的 SearchIndex 定义之间一致地命名,而不是您的模型字段需要一致地命名。搜索索引定义和模型定义之间有很大的区别。您不需要,也不应该担心模型字段和搜索字段之间的1-1映射。

The Haystack documentation is advising that your SearchIndex field should be named consistently across your SearchIndex definitions - not that your model fields need to be named consistently. There's a major distinction between the search index definitions and model definitions. You do not need to and probably should not worry about a 1-1 mapping between model fields and search fields.

从您的模型中退出,首先想想你想要的寻找。您会通过常见的搜索视图搜索几种不同的模型吗?假设你有两个模型:

Step back from your models and think first about what you want to search. Will you be searching several different models through a common search view? Let's say your have two models:

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)

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

We want to create a simple search view that searches only the primary content of the model as well as the title or name of the content object (name, title, subject, etc.).

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

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

在这个不好的例子中,每个搜索索引 定义主要内容字段和内容名称字段(标题或主题)。但是现在如何搜索?如果您根据主题针对内容运行查询,则会错过注意内容,同样如果您对

In this bad example, each search index does define a primary content field and a content name field (title or subject). But how do you search it now? If you run a query against content based on the subject you'll miss Note content, and similarly if you query against the 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

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

Note that the field names do not necessarily match the model field names. You just define which model attribute from which the SearchIndex field should source its data.

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

You search documents in the search engine, not rows in the database, so the SeachIndex definition maps content from the database (one table or a query over many) to a search document. The SearchIndex definition is a transformation, and each SearchField transforms data as you specify.

关于您缺少的 model_attr 的问题,这只是一种方法来拉入内容。您还可以从模板中呈现文本内容,这是上述文本字段的内容(请参阅 SearchField API文档)。 model_attr 源对简单字符字段起作用。

As to your question about the missing model_attr, that's just one way to pull in the content. You can also render textual content from a template, which is what the text field above does (see the SearchField API documentation on that one). The model_attr source works well for simple character fields.

这篇关于主字段名(document = True)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆