使用属性过滤 Django-sphinx 结果?

2024-01-12

我正在经历 django-sphinx文档 http://code.google.com/p/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

但事实证明你只能指定外键作为这个值。如图所示另一个例子 http://www.davidcramer.net/code/django/433/setting-up-django-and-sphinx-full-text-search-django-sphinx.html,

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 搜索结果 - 我该怎么做?

Thanks!


Edit

在回答范·盖尔时,

我实际上在这里使用 sql_attr_uint 而不是 sql_group_column .. 正如我在上面的示例中提到的.. 即使 Django Sphinx 作者的示例(上面给出的链接)也不会显示 _Sphinx 字典中的属性,如果它不是FK..(请参阅上面的“如您所见”声明)。另外,我也已经有了 SQL_Query 字符串..它选择我表中的所有列..(单独,不是 *)


距离我使用 django-sphinx 做一个项目已经快一年了,所以我对此的记忆有点模糊。但是,知道我能够过滤普通列,我将发布我的 sphinx 配置的相关部分,也许这会有所帮助。

狮身人面像.conf:

sql_query_pre       =
sql_query_post      =
sql_query           = SELECT `id`, `content_type_id`, `site_id`, `user_id`, `title`, `abstract`, `summary`, `fulltext`, `approved` FROM `basedoc`
sql_query_info      = SELECT * FROM `basedoc` WHERE `id` = $id

sql_attr_uint    = content_type_id
sql_attr_uint    = site_id
sql_attr_uint    = user_id
sql_attr_uint    = approved

正如您所看到的,我有一个非 fk 列(已批准)并在 django 视图中对其进行了过滤。所以我猜你的问题是你需要sql_attr_uint代替sql_group_column,并添加sql_query字符串。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用属性过滤 Django-sphinx 结果? 的相关文章

随机推荐