2010-10-27 33 views
1

我正在使用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字段是一個屬性(不可搜索)。

回答

2

據我所知,有兩種方法可以解決這個問題。

首先,有分類模式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