如何在sphinx-django中使用两个模型进行单索引搜索

问题描述 投票:0回答:1

我在django项目中使用django-sphinxql来满足搜索需求。我想在我的应用程序中使用两个模型进行一些查询搜索。型号如下所示

Class Model1(models.Model):
    name = models.CharField(max_length=50)
    model2 = models.ForeignKey(Model2, on_delete=models.CASCADE)

Class Model2(models.Model):
    caption = models.CharField(max_length=50)

我想启用上面的名称和标题字段搜索,以便返回任何匹配项的Model1,例如如果query =“abc”匹配caption,则响应应该是Model1,我将如何实现我为Model1创建的索引,但不知道如何在Model2中添加标题。我的Model1索引如下

class Model1Index(indexes.Index):
    name = fields.Text(model_attr='name')
    class Meta:
        model = Model1
        settings.INDEXES['source_params'] = {'sql_field_string': ['name'],}

快速帮助表示赞赏。

django search sphinx sphinxql django-sphinx
1个回答
0
投票

对于Sphinx中的外键字段,我们可以使用双下划线(__)指向特定字段以进行索引。用户model_attr为此

在上面的例子中

class Model1Index(indexes.Index): name = fields.Text(model_attr='name') caption = fields.Text(model_attr='model2__caption') class Meta: model = Model1 settings.INDEXES['source_params'] = {'sql_field_string': ['name'],}

可以定义。

参考http://django-sphinxql.readthedocs.io/en/latest/indexes.html

© www.soinside.com 2019 - 2024. All rights reserved.