在Django中搜索应用程序

问题描述:

我正在使用django构建搜索应用程序& sphinx。我得到了设置工作,但是当我搜索时,我得到不相关的结果。这是我做的 -在Django中搜索应用程序

# this is in my trial_data Model 
search  = SphinxSearch(
       index = 'trial_data trial_datastemmed', 
       weights = {'name': 100,}, 
       mode  = 'SPH_MATCH_ALL', 
       rankmode = 'SPH_RANK_BM25', 
       ) 

当我搜索我得到这个(从我的试验数据) -

from trial.models import * 
res = trial_data.search.query('godfather') 
for i in res: print i 
3 Godfathers (7.000000) 
Bonanno: A Godfather's Story (1999) (6.400000) 
Disco Godfather (4.300000) 
Godfather (6.100000) 
Godfather: The Legend Continues (0.000000) 
Herschell Gordon Lewis: The Godfather of Gore (2010) (6.900000) 
Mafia: Farewell to the Godfather (0.000000) 
Mumbai Godfather (2.600000) 
Russian Godfathers (2005) (7.000000) 
Stan Tracey: The Godfather of British Jazz (2003) (6.200000) 
The Black Godfather (3.500000) 
The Burglar's Godfather (0.000000) 
The Fairy Godfather (0.000000) 
The Fairy Godfather (0.000000) 
The Godfather (9.200000) 
The Godfather (1991) (6.400000) 

的问题是“教父最相关的结果“显示在第19位。所有最重要的结果都是垃圾。我如何才能ordersort我的结果使用Django-sphinx

相反,我可以做些什么来使结果更加相关。

注:我使用python的2.6.x + Django的1.2.x版本+狮身人面像0.99 + Django的狮身人面像2.3.3 + MySQL的

而且,我定制&的数据只有100行与只有一个字段name可搜索。还有一个字段rating(这是你在括号中看到的)。 rating字段是一个属性(不可搜索)。

据我所知,有两种方法可以解决这个问题。

首先,有分类模式SPH_SORT_RELEVANCE,SPH_SORT_ATTR_DESC,SPH_SORT_ATTR_ASC,SPH_SORT_TIME_SEGMENTS,SPH_SORT_EXTENDED。我假设SphinxSearch构造函数中的关键字是sortmode,但我找不到文档。

search  = SphinxSearch(
       index = 'trial_data trial_datastemmed', 
       weights = {'name': 100,}, 
       mode  = 'SPH_MATCH_ALL', 
       rankmode = 'SPH_RANK_BM25', 
       sortmode = 'SPH_SORT_RELEVANCE', # this was added 
       ) 

其次,你可以在查询时指定排序方式:

res = trial_data.search.query('godfather').order_by('@relevance') 

这两个答案都是猜测从看http://djangosnippets.org/snippets/231/。让我们知道它是否适用于您。

+0

'.order_by('@ weight')'在这里没有效果,因为我只用一个字段'name'进行搜索。如果您从多个字段搜索,则“@ weight”是有意义的。所以回来了,我已经试过了。它对'result_set'没有影响。 – 2010-10-27 19:59:08

+0

@movieyoda我编辑了我的答案,根据http://www.davidcramer.net/code/79/in-depth-django-sphinx-tutorial.html将“@weight”更改为“@relevance”。 – 2010-10-27 20:03:32