Django-sphinx结果过滤使用属性? [英] Django-sphinx result filtering using attributes?

查看:215
本文介绍了Django-sphinx结果过滤使用属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览django-sphinx 文档,它看起来像它允许您使用属性过滤搜索结果

  queryset = MyModel.search.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的
#显然sql_group_column现在被sql_attr_uint
sql_group_column = country_id
sql_group_column = state_id
sql_group_column = listing

#DateField和DateTimeField的
sql_date_column = date_added

但事实证明,您只能将外键指定为此值。如另一个例子

  Class City(models.Model):
...
#评论:下面应该是country_id和state_id
country_id = models.ForeignKey(Country)
state_id = models.ForeignKey(State,blank = True,null = True)
listing = models.PositiveIntegerField(editable = False,default = 0)

当打印出搜索结果时,你得到,

 打印结果[0] ._ sphinx 
{'id':u'5246','weight ':200,'attrs':{'state_id':3,'country_id':0}}

正如你所看到的,在attrs中,state_id和country_id是FKs。但是列表没有。



这里是我的问题。如果我想使用我的模型中的aribtrary列foo来过滤我的狮身人质的搜索结果 - 我该怎么做?



谢谢!






编辑



在答复范甘尔,


$ b $我实际上使用sql_attr_uint而不是sql_group_column这里..正如我在上面的例子中提到的。甚至Django Sphinx的作者(上面给出的链接)的例子并没有显示_Sphinx dict中的属性如果它不是FK ..(见上面的你可以看到)。另外,我已经有SQL_Query字符串了..它选择我的表中的所有列..(单独,不*)

解决方案

自从我使用django-sphinx做了一个项目已经差不多一年了,所以我的记忆有点朦胧。但是,知道我能够按照常规列进行过滤,我只会发布我的sphinx配置的相关部分,也许这将有所帮助。



sphinx。 conf:

  sql_query_pre = 
sql_query_post =
sql_query = SELECT`id`, content_type_id`,`site_id``````````````````````````````````````````````````````````````````````````````````````` = $ id

sql_attr_uint = content_type_id
sql_attr_uint = site_id
sql_attr_uint = user_id
sql_attr_uint =已批准

如您所见,我有一个非fk列(已批准),并在django视图中对其进行过滤。所以我猜你的问题是你需要 sql_attr_uint 而不是 sql_group_column ,并添加 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 *)

解决方案

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.

这篇关于Django-sphinx结果过滤使用属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆