代码之家  ›  专栏  ›  技术社区  ›  Srikar Appalaraju Tonetel

Django中的搜索应用程序

  •  1
  • Srikar Appalaraju Tonetel  · 技术社区  · 14 年前

    我正在使用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位。所有的热门结果都是垃圾。我怎么能 order sort 我的结果使用 Django-sphinx

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

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

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

    1 回复  |  直到 14 年前
        1
  •  2
  •   Steven Rumbalski    14 年前

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

    首先,有排序模式sph_sort_相关性、sph_sort_attr_desc、sph_sort_attr_asc、sph_sort_time_段、sph_sort_扩展。我假设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/ . 如果对您有用,请通知我们。