简体   繁体   English

使用Elastic Search后端无法“更像这样”在Haystack中返回任何结果

[英]Having trouble getting “more like this” to return any results in Haystack using Elastic Search backend

I don't seem to be able to get the more_like_this tag in Haystack to return any results. 我似乎无法在Haystack中获得more_like_this标签以返回任何结果。 Not sure if it's down to the data I've input, but I've tried it with some documents that should very similar. 不知道它是否取决于我输入的数据,但是我已经尝试了一些应该非常相似的文档。

I've verified it doesn't work even with a fairly simple index like this: 我已经证明即使使用这样的简单索引也无法正常工作:

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

    def get_model(self):
        return Paper

Again the simplified model looks like: 同样,简化模型如下所示:

class Paper(Publishable):
    title = models.CharField(max_length=255)
    abstract = models.TextField()

    def __unicode__(self):
        return self.title

The search template looks like: 搜索模板如下所示:

{% autoescape off %}
{{ object.title }}
{{ object.abstract|striptags }}
{% endautoescape %}

At the moment I'm just looking to see that's in the returned result from the tag like this: 目前,我只是想看看这是从标记返回的结果中,如下所示:

{% more_like_this paper as related_papers limit 1 %}
{{ related_papers }}

Not sure if there is anything else I need to do. 不知道我还有什么需要做的。 The elastic search docs mention "In order to use the mlt feature a mlt_field needs to be either be stored, store term_vector or source needs to be enabled." 弹性搜索文档提到“为了使用mlt功能,需要存储mlt_field,必须启用存储term_vector或源。” However I'm not sure what that equates to in Haystack terms. 但是,我不确定这在Haystack术语中意味着什么。

I should note that I'm not seeing any errors - it's just that I get back an empty list/result set from more_like_this . 我应该注意,我没有看到任何错误-只是我从more_like_this返回了一个空列表/结果集。

Oh and Elastic Search is version 1.1.1. 哦,Elastic Search是1.1.1版。

Old question, but still a recurring problem. 旧问题,但仍然是反复出现的问题。

The solution is simple: provide enough information in your search template for Elastic Search to be able to calculate similarities. 解决方案很简单:在搜索模板中提供足够的信息以供Elastic Search计算相似度。

If you have tags, full description, genres, languages, or anything else that could serve to find matches, add them to the search template. 如果您有标签,完整说明,流派,语言或其他可用于查找匹配项的内容,请将它们添加到搜索模板中。

To add a list (of tags for instance), you could do something like this in your search template: 要添加(例如,标签的)列表,您可以在搜索模板中执行以下操作:

{% load search_tags %}
{% render_tags object %}

And in the search_tags add a template tag called render_tags : 然后在search_tags添加一个名为render_tags的模板标签:

from django.template import Library
from django.utils.safestring import mark_safe

register = Library()


@register.simple_tag
def render_tags(obj):
    return mark_safe(' '.join([t.tag for t in obj.tags.all()]))

This assumes you have a ManyToMany relation to tags in your model. 假设您与模型中的标签有ManyToMany关系。

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

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