【问题标题】:Search app in Django在 Django 中搜索应用程序
【发布时间】:2010-10-27 19:29:21
【问题描述】:

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

# 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 位。所有排名靠前的结果都是垃圾。我如何使用Django-sphinxordersort 我的结果。

相反,我可以做些什么来使用此设置使结果更相关。

注意:我使用的是 python 2.6.x + django 1.2.x + sphinx 0.99 + django-sphinx 2.3.3 + mysql

另外,我定制的数据只有大约 100 行,只有一个字段 name 可搜索。还有一个字段rating(就是您在括号中看到的)。 rating 字段是一个属性(不可搜索)。

【问题讨论】:

    标签: python django search sphinx django-sphinx


    【解决方案1】:

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

    首先,排序模式有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/ 得出的。让我们知道它是否对您有用。

    【讨论】:

    • .order_by('@weight') 在这里无效,因为我只搜索一个字段 name。如果您从多个字段进行搜索,@weight 是有意义的。所以回来,我试过这个。对result_set没有影响。
    • @movieyoda 我根据davidcramer.net/code/79/in-depth-django-sphinx-tutorial.html 编辑了我的答案以将“@weight”更改为“@relevance”。
    猜你喜欢
    • 1970-01-01
    • 2013-04-19
    • 2020-07-21
    • 2012-05-20
    • 1970-01-01
    • 1970-01-01
    • 2014-06-21
    • 2018-01-06
    • 2012-06-28
    相关资源
    最近更新 更多