php – 如何在Sphinx中查询完全匹配的词组?

前端之家收集整理的这篇文章主要介绍了php – 如何在Sphinx中查询完全匹配的词组?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
似乎Sphinx正在逐字搜索文档.我不知道如何在文档中搜索确切的短语.我尝试了SPH_MATCH_ALL,SPH_MATCH_PHRASE,但都是逐字搜索文档.我在我的 PHP应用程序中使用它.

如何查询Sphinx以匹配精确的字符串?

这是我的代码

$sphinx = new SphinxClient();
$mode = SPH_MATCH_PHRASE;
$sphinx->setServer('127.0.0.1',9312);
$sphinx->setLimits(0,1);
$sphinx->setMaxQueryTime(5000);
$sphinx->setMatchMode($mode);
$sphinx->setFieldWeights(array('name' => 100));
$sphinx->setArrayResult(true);

$result = $sphinx->query('Lorem ipsum dolor sit amet,consectetur adipiscing elit.');
print_r($result);

返回结果如下:

Array (
    [error] =>
    [warning] =>
    [status] => 0
    [fields] => Array (
        [0] => name
        [1] => company
        [2] => image
        [3] => price
    )
    [attrs] => Array ()
    [total] => 0
    [total_found] => 0
    [time] => 0.000
    [words] => Array (
        [lorem] => Array (
            [docs] => 0
            [hits] => 0
        )
        [ipsum] => Array (
            [docs] => 0
            [hits] => 0
        )
        [dolor] => Array (
            [docs] => 0
            [hits] => 0
        )
        [sit] => Array (
            [docs] => 0
            [hits] => 0
        )
        [amet] => Array (
            [docs] => 0
            [hits] => 0
        )
        [consectetur] => Array (
            [docs] => 0
            [hits] => 0
        )
        [adipiscing] => Array (
            [docs] => 0
            [hits] => 0
        )
        [elit] => Array (
            [docs] => 0
            [hits] => 0
        )
    )
)

如你所见,Sphinx正在逐字搜索文件……

最好的方法是使用SPH_MATCH_EXTENDED2语法并使用双引号进行查询.
$sphinx->SetMatchMode(SPH_MATCH_EXTENDED2);
$sphinx->Query('"Lorem ipsum dolor"');

Extended syntax

原文链接:https://www.f2er.com/php/133205.html

猜你在找的PHP相关文章