【问题标题】:haystack with elastic search creating one modelresult table for multiple search_indexes使用弹性搜索的 haystack 为多个 search_indexes 创建一个模型结果表
【发布时间】:2013-06-02 10:09:53
【问题描述】:

最近我试图将我网站上的搜索功能从使用 xapian 转移到 elasticsearch。我的应用程序中有两个 search_indexes,搜索时在页面中并排显示结果。

其中一个 search_indexes 在帐户应用程序中,用于索引用户模型。在我的代码中,为了只检索用户模型的匹配查询,我正在执行以下操作

        users_form = form_class(request.GET, searchqueryset=searchqueryset, load_all=load_all)
        users = users_form.search().order_by('date_joined').models(User, )[:8]

我使用的是 .model(MODEL_NAME) 但显然这在 elasticsearch 中不再适用。

我尝试在elasticsearch上查看我的索引详情,发现两个索引都是在Index:Haystack和Type:Modelresult下创建的

不知道如何解决这个问题,以前有人遇到过吗?

更新(以下是我的索引):

class UserIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    username = indexes.CharField(model_attr='username')
    date_joined = indexes.DateTimeField(model_attr='date_joined')
    full_name = indexes.CharField(model_attr='full_name', )
    bio = indexes.CharField(model_attr='bio', null=True)

    def get_model(self):
        return User

    def index_queryset(self, using=None):
        return self.get_model().objects.filter(is_active=True, email_is_valid=True)

【问题讨论】:

    标签: django elasticsearch django-haystack


    【解决方案1】:

    我遇到过类似的问题,可以通过手动过滤内容类型来解决:

    # assuming the app providing your User model is called `auth`
    users = users_form.search().order_by('date_joined').filter(django_ct='auth.user')[:8]
    

    【讨论】:

    • @MoJ.Mughrabi:只是忘了添加,您可以执行print users.query 之类的操作来查看生成的查询,这对于调试这个很有用...
    猜你喜欢
    • 2015-09-21
    • 2013-04-16
    • 1970-01-01
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多