简体   繁体   English

如何使用Sphinx PHP API根据排名获取Sphinx结果?

[英]How to get the Sphinx results based on ranking using Sphinx PHP API?

Is it possible to display the Sphinx results based on ranking using sphinx PHP API, 是否可以使用sphinx PHP API根据排名显示Sphinx结果,

For example,
----------------------------------------------
Searched_Query  |  Result        |  Ranks 
----------------------------------------------
Sony Ericsson     Sony Ericsson     Best Match
                  Ericsson          Good Match
                  Sony DVD          Fair Match
                  DVD               Poor Match
----------------------------------------------

If is it so, then kindly attach me the sample or reference URL. 如果是这样,请附上示例或参考网址。

Help is greatly appreciated ! 非常感谢帮助!

Thanks, Raja 谢谢,拉贾

require_once ( "sphinxapi.php" );
$cl = new SphinxClient ();
$cl->SetServer ("localhost", 9312); 
$cl->SetMatchMode ( SPH_RANK_SPH04 );  /*use this*/
$cl->SetMatchMode ( SPH_MATCH_EXTENDED2 );
$cl->SetSortMode ( SPH_SORT_EXTENDED, "@weight DESC" );
$res = $cl->Query ( "Sony Ericsson", "your_sphinx_index_name" );
if ( $res===false ){
    print "ERROR: Query failed: " . $cl->GetLastError() . ".\n";
}else {
    if ( $cl->GetLastWarning() ){
    print "WARNING: ".$cl->GetLastWarning() . "\n\n";
    }
    // result processing is here
}

Yes and no. 是的,没有。 Sphinx by default does ranking - and results are ordered best first. 狮身人面像默认情况下会排名-结果排名最佳。 (but this can be changed) (但是可以更改)

But an important consideration here is 'matching' - does the record even match the query. 但是这里重要的考虑因素是“匹配”-记录是否与查询匹配。 (which is slightly different to the task of 'ranking' which gives a score to each match, and usually orders by it) (这与“排名”的任务稍有不同,后者的任务是为每场比赛提供一个分数,通常按顺序进行排序)

To get close you will need to use the "MATCH ANY" style matching. 为了接近,您将需要使用“ MATCH ANY”样式匹配。 Which means will only require one of the words. 这意味着只需要其中一个字。 By default is match "ALL" - needing all words. 默认情况下,匹配项“ ALL”-需要所有单词。 Can also emulate match any with EXTENDED match mode, either using Quorum or a 'OR' query. 还可以使用Quorum或'OR'查询,以EXTENDED匹配模式模拟任何匹配。

... read more about these terms in the sphinx documentation. ...在sphinx文档中阅读有关这些术语的更多信息。

I said 'close' because sphinx wont really give you the last result in your example. 我说“关闭”是因为狮身人面像不会真正给您示例中的最后结果。 Because it shares no common words. 因为它没有共同的词。 The first three all share a common word. 前三个都有一个共同的词。

Perhaps you are thinking of the "WITH EXPANSION" option in mysql full-text search. 也许您正在考虑mysql全文搜索中的“ WITH EXPANSION”选项。 This will do this. 这样就可以了。 Sphinx doesnt have a directly comparable function, so it would have to be implemented yourself. Sphinx没有直接可比的功能,因此必须自己实现。 (eg run a 'any qyery' grab, the top 100 results, work out the common words (in your code)), and form a second query to sphinx. (例如,运行“ any qy​​ery”抓取,获得前100个结果,计算出常用词(在您的代码中)),然后对狮身人面像进行第二次查询。 use this second query as the results shown to the user) 使用第二个查询作为显示给用户的结果)

[having said that there is another option, could form a query that matches all documents, and then just relied on ranking to get the 'matches' to show near the top. [说过,还有另一种选择,可以构成一个匹配所有文档的查询,然后仅依靠排名来使“匹配项”显示在顶部附近。 I wouldnt recommend this tho for a general search] 我不建议将此作为一般搜索]

require_once ( "sphinxapi.php" );

$cl = new SphinxClient ();

$cl->SetServer ("localhost", 9312);
$cl->SetMatchMode ( SPH_MATCH_EXTENDED2 );
$cl->SetSortMode ( SPH_SORT_EXTENDED, "@weight DESC" );
$res = $cl->Query ( "Sony Ericsson", "your_sphinx_index_name" );
if ( $res===false )
{
        print "ERROR: Query failed: " . $cl->GetLastError() . ".\n";
} else {
        if ( $cl->GetLastWarning() ) print "WARNING: " . $cl->GetLastWarning() . "\n\n";

        // result processing is here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM