我正在浏览 django-sphinx文档,看起来它允许您使用属性过滤搜索结果,
queryset = MyModel.search.query('query')
results1 = queryset.order_by('@weight', '@id', 'my_attribute')
results2 = queryset.filter(my_attribute=5)
results3 = queryset.filter(my_other_attribute=[5, 3,4])
results4 = queryset.exclude(my_attribute=5)[0:10]
从一些示例中,这些属性似乎是您在 sphinx 配置文件中指定的内容,而不是表中的实际列值。配置文件允许这样的事情,
# ForeignKey's
# Apparently sql_group_column is now replaced by sql_attr_uint
sql_group_column = country_id
sql_group_column = state_id
sql_group_column = listings
# DateField's and DateTimeField's
sql_date_column = date_added
但事实证明,您只能将外键指定为该值。如另一个例子所示,
Class City(models.Model):
...
# Comment: The below should probly be country_id and state_id
country_id = models.ForeignKey(Country)
state_id = models.ForeignKey(State, blank=True, null=True)
listings = models.PositiveIntegerField(editable=False, default=0)
打印搜索结果时,您会得到,
print results[0]._sphinx
{'id': u'5246', 'weight': 200, 'attrs': {'state_id': 3, 'country_id': 0}}
如您所见,在 attrs - state_id 和 country_id - 作为 FK 中,出现了。但列表没有。
这就是我的问题。如果我想在我的模型中使用任意列 foo 过滤我的 sphinx 搜索结果 - 我该怎么做?
谢谢!
编辑
在回答范盖尔时,
我实际上在这里使用 sql_attr_uint 而不是 sql_group_column .. 正如我在上面的示例中提到的.. 即使是 Django Sphinx 的作者的示例(上面给出的链接)也没有显示 _Sphinx 字典中的属性,如果它不是FK..(请参阅上面的“如您所见”声明)。另外,我也已经有了 SQL_Query 字符串..它选择了我表中的所有列..(单独,不是 *)