简体   繁体   English

django-haystack教程中哪些模型的字段被编入索引?

[英]Which fields of the model in the django-haystack tutorial get indexed?

I'm trying to get my head around the django-haystack tutorial in order to add search functionality to my application. 我正试图绕过django-haystack教程 ,以便为我的应用程序添加搜索功能。 Unfortunately, I don't quite understand some key parts when it comes to build the search index. 不幸的是,在构建搜索索引时,我不太了解一些关键部分。

In the tutorial, the following django model serves as an example: 在本教程中,以下django模型作为示例:

class Note(models.Model):
    user = models.ForeignKey(User)
    pub_date = models.DateTimeField()
    title = models.CharField(max_length=200)
    body = models.TextField()

The respective index class for the Note model is this: 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

Last but not least, I'm asked to create a data template which looks like this: 最后但同样重要的是,我被要求创建一个如下所示的数据模板:

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

After reading the whole tutorial, I'm still confused about what is getting indexed now. 阅读完整个教程之后,我仍然对现在索引的内容感到困惑。 As far as I understand, the contents of the fields author and pub_date will be used to create the index. 据我所知,字段authorpub_date的内容将用于创建索引。 The field text is simply for providing some settings. 字段text仅用于提供一些设置。 And the data template specifies how the search results will be displayed later on, ie, which fields of the model to use to be displayed in the search results. 并且数据模板指定稍后将如何显示搜索结果,即,用于在搜索结果中显示的模型的哪些字段。

Is this correct or am I completely wrong? 这是正确的还是我完全错了? The tutorial and the documentation are quite vague in a lot of aspects in my opinion. 在我看来,教程和文档在很多方面都很模糊。 Thank you very much in advance. 非常感谢你提前。

You're right, the tutorial seems a little vague, but here's how I understand it. 你是对的,教程看起来有点模糊,但这是我理解它的方式。 For each instance of the Note model, Haystack renders the data template using that instance and indexes the rendered templates. 对于Note模型的每个实例,Haystack使用该实例呈现数据模板并为呈现的模板编制索引。 The rendered template is the "document" for the instance. 呈现的模板是实例的“文档”。 The tutorial says, "This allows us to use a data template (rather than error prone concatenation) to build the document the search engine will use in searching." 该教程说:“这允许我们使用数据模板(而不是容易出错的连接)来构建搜索引擎将在搜索中使用的文档。” So if you only wanted the title field to be searchable, you would only include {{ object.title }} in the data template. 因此,如果您只希望title字段可搜索,则只能在数据模板中包含{{ object.title }}

So the other fields in the NoteIndex model are used for filtering search query results. 因此, NoteIndex模型中的其他字段用于过滤搜索查询结果。 If your index model looked just like this: 如果你的索引模型看起来像这样:

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

you would not be able to issue a search query that says, "Give me all the Notes published in the last year where foo appears in the document text." 你将无法发出一个搜索查询,上面写着“给我去年在文档文本中出现foo所有注释。” If you include pub_date as a field in your NoteIndex (as they do in the tutorial) then you can make a query such as the following: 如果将pub_date作为NoteIndex的字段NoteIndex (就像在教程中那样),那么您可以进行如下查询:

recent_results = SearchQuerySet().filter(content='foo').order_by('-pub_date')[:5]

which asks for the 5 most recently published documents that contain the word foo . 它要求包含单词foo的5个最近发布的文档。 I suppose that, without including pub_date in the NoteIndex model, you could query for content='foo' and then filter the results yourself, but I'd imagine it's a much more efficient query if you tell Haystack at indexing time about the fields you might want to filter on. 我想,如果NoteIndex模型中包含pub_date ,你可以查询content='foo' ,然后自己过滤结果,但我想如果你告诉Haystack索引时关于你的字段,这是一个更有效的查询可能想要过滤。

As for how the search results will be displayed, you use a different template to specify that. 至于如何显示搜索结果,您可以使用其他模板来指定搜索结果。 In the most basic Haystack usage, which they show in the tutorial, the template for displaying search results goes in search/search.html: http://django-haystack.readthedocs.org/en/latest/tutorial.html#search-template You can iterate through the search results and print out whatever fields of the model instance ( result.object ) that you'd like. 在他们在教程中显示的最基本的Haystack用法中,用于显示搜索结果的模板位于search / search.html中: http//django-haystack.readthedocs.org/en/latest/tutorial.html#search-模板您可以遍历搜索结果并打印出您想要的模型实例( result.object )的任何字段。

In your class definition, 在你的课程定义中,

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')

Haystack stores the index of user attribute of database as author , and the index of the database field pub_date as pub_date in index Haystack将数据库的user属性索引存储为author ,将数据库字段pub_date的索引存储为索引中的pub_date

The template includes only the "searchable" fields. 该模板仅包括“可搜索”字段。 For example, you might want to save some sensitive data in the search indices you can hide it from the search by not specifying it in the template. 例如,您可能希望在搜索索引中保存一些敏感数据,您可以通过不在模板中指定它来将其隐藏在搜索中。

text can be thought of as free text search text可以被认为是自由文本搜索

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

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