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

发布于 2024-07-26 22:14:02 字数 1801 浏览 3 评论 0原文

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

谢谢!


编辑

在回答 Van Gale 时,

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

I was going through the django-sphinx documentation, and it looks like it allows you to filter search results using attributes,

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]

From some examples, these attributes seem to be something you specify in the sphinx configuration file, rather than being actual column values in the table. The configuration file allows something like this,

# 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

But it turns out that you can only specify Foreign Keys as this value. As illustrated in another example,

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)

When the search result is printed, you get,

print results[0]._sphinx
{'id': u'5246', 'weight': 200, 'attrs': {'state_id': 3, 'country_id': 0}}

As you can see, in attrs - state_id and country_id - being FKs, show up. But listings doesn't.

And here lies my problem. If I want to filter my sphinx search results using an aribtrary column foo in my model - how would I do it?

Thanks!


Edit

In answer to Van Gale,

I'm actually using sql_attr_uint rather than sql_group_column here.. and as I mentioned in the example above.. even the example from the author of Django Sphinx (link given above) doesn't show the attribute in the _Sphinx dict if its not a FK.. (See the "As you can see" statement above). Also, I already have the SQL_Query string too.. it selects all columns in my table.. (individually, not *)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我只土不豪 2024-08-02 22:14:02

距离我使用 django-sphinx 做一个项目已经快一年了,所以我对此的记忆有点模糊。 但是,知道我能够过滤普通列,我只会发布我的 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 字符串。

It's been almost a year since I did a project using django-sphinx, so my memory is a bit hazy on this. But, knowing I was able to filter on normal columns, I'll just post the relevant portions of my sphinx config and maybe that will help.

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

As you can see, I had a non-fk column (approved) and did filter on it in the django view. So I'm guessing your problem is you need sql_attr_uint instead of sql_group_column, and add the sql_query strings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文