【问题标题】:Not showing results for single character search不显示单字符搜索结果
【发布时间】:2017-11-30 18:41:30
【问题描述】:

我正在尝试在控制台上查看使用 sphinx/php 的单字符搜索结果。但它没有显示结果。理想情况下,所有结果都应以该特定字符开头

标签表

mysql> 从标签中选择 *;

+--------+---------+--------------------+

| tag_id |关键词 |属于表名 |

+--------+---------+--------------------+

| 1 |鹿 |物种 |

| 2 |狩猎 |活动 |

+--------+---------+--------------------+

集合中的 2 行(0.00 秒)

php 文件

<?php
include_once 'sphinxapi.php';
// Build search query
$cl = new SphinxClient();
$cl->SetServer('127.0.0.1', 9312);
$cl->SetMatchMode(SPH_MATCH_EXTENDED);
$cl->SetRankingMode (SPH_RANK_SPH04);
// Execute the query
$query = 'hun';
$partialQueryStr = " @keyword $query";
$cl->AddQuery($partialQueryStr, 'customsearch');
$result = $cl->RunQueries();
print_r($result);

if ( $result === false ) {
      echo "Query failed: " . $cl->GetLastError() . ".\n";
  }
  else {
      if ( $cl->GetLastWarning() ) {
          echo "WARNING: " . $cl->GetLastWarning() . "<br>";
      }

        if ($result['matches'] > 0) {
            print_r($result['matches']);        
        } else {
                echo 'No results found';        
        }
  }
exit;
?>

Sphinx.conf

    source customsearch {
    type = mysql
    sql_host = localhost
    sql_user = root
    sql_pass = hello123
    sql_db = testsphinx
    sql_port = 3306

    sql_query = \
                SELECT \
                UUID_SHORT() AS sphinxid, \
                tg.keyword AS keyword, \
                tg.belongstotablename AS ref \
                FROM tags AS tg;

    sql_attr_uint = sphinxid
    sql_field_string = keyword
    sql_field_string = ref
    }

    index customsearch {
        source = customsearch
        path = /etc/sphinx/data/customsearch
        docinfo                 = extern
        dict                    = keywords
        morphology              = stem_en
        #morphology             = soundex
        min_stemming_len        = 1
        min_prefix_len          = 1
    }

    searchd {
    listen = 9312
    listen = 9306:mysql41
    log = /home/xxx/www/log/searchd.log
    query_log = /home/xxx/sphinx/log/query.log
    read_timeout = 5
    max_children = 30
    pid_file = /home/xxx/sphinx/log/searchd.pid
    preopen_indexes = 1
    unlink_old = 1
    }

    indexer
    {
        mem_limit       = 136M
    }

控制台结果

vikas@vikas-pc:~$ php /www/fwv2/php/search/practicesearch.php 
Array
(
    [0] => Array
        (
            [error] => 
            [warning] => 
            [status] => 0
            [fields] => Array
                (
                    [0] => keyword
                    [1] => ref
                )

            [attrs] => Array
                (
                    [keyword] => 7
                    [ref] => 7
                )

            [total] => 0
            [total_found] => 0
            [time] => 0.000
            [words] => Array
                (
                    [hun] => Array
                        (
                            [docs] => 0
                            [hits] => 0
                        )

                )

        )

)
PHP Notice:  Undefined index: matches in /home/xxx//practicesearch.php on line 24
No results found

根据我的理解,它应该为“hun”查询带来 1 条记录。

【问题讨论】:

    标签: php mysql sphinx


    【解决方案1】:

    您确实有min_prefix_len,它启用了“部分单词匹配”或至少“单词匹配的开头” - 默认情况下,sphinx 只匹配整个单词。

    所以要真正匹配,需要* 通配符,例如

    $query = 'hun*';
    

    那么你应该得到匹配。

    ... 或者可以查看expand_keywords 选项,该选项一旦在索引上启用,实际上会透明/自动添加 *。


    ...但也要注意设置min_prefix_len=1 可能不适用于dict=keywords - 它已被悄悄更改为min_prefix_len=2

    见:http://sphinxsearch.com/forum/view.html?id=14123

    ...该线程有一些解决方法(包括使用dict=crc 或以其他方式计算掩码)

    【讨论】: