狮身人面像搜索:部分关键字

时间:2014-01-07 07:33:21

标签: php mysql full-text-search sphinx keyword-search

我开始使用 Sphinx ,我尝试安装sphinx。一切正常,但我想搜索部分关键字。我想知道如何在网页上显示结果 例
我搜索“吃了” 结果应该是:'','更新','','讨厌'等。
我的sphinx.con:

source src1
{
  type          = mysql
  sql_host      = localhost
  sql_user      = username
  sql_pass      = *******
  sql_db        = db_name
  sql_port      = 3306  # optional, default is 3306

  sql_query     = \
    SELECT id, msgtext, created \
    FROM agi_sms

  sql_attr_string = msgtext
  sql_attr_timestamp    = created

  sql_query_info        = SELECT * FROM agi_sms WHERE id=$id

}

   index test1
   {
  source            = src1
  path          = C:/Sphinx/data/test1
  docinfo           = extern
  charset_type      = sbcs
  enable_star = 1
  morphology = stem_en
  min_infix_len = 2
  infix_fields = msgtext
  dict = keywords

    }



    indexer
    {
       mem_limit        = 32M
    }


    searchd
    {
  listen            = 9312
  listen            = 9306:mysql41
  log           = C:/Sphinx/log/searchd.log
  query_log     = C:/Sphinx/log/query.log
  read_timeout      = 5
  max_children      = 30
  pid_file      = C:/Sphinx/log/searchd.pid
  max_matches       = 1000
  seamless_rotate       = 1
  preopen_indexes       = 1
  unlink_old        = 1
  workers           = threads # for RT to work
  binlog_path       = C:/Sphinx/data
    }

这是我的php文件:

$cl = new SphinxClient ();
$q = $_POST['search'];
$sql = "";
$mode = SPH_MATCH_EXTENDED2;
$host = "localhost";
$port = 9312;
$index = "*";
$groupby = "";
$filter = "created";
$filtervals = array();
$distinct = "";
$sortby = "";

$sortexpr = "";
$limit = 20;
$ranker = SPH_RANK_PROXIMITY_BM25;
$select = "";

////////////
// do query
////////////

  $cl = new SphinxClient();
  $cl->SetServer( "localhost", 9312 );
  $cl->SetMatchMode( SPH_MATCH_ANY );
  $cl->SetFilter( 'created', array( 3 ) );

$res = $cl->Query ( $q, $index );
////////////////
// print me out
////////////////

if ( $res===false )
{
  print "Query failed: " . $cl->GetLastError() . ".\n";

} else
{
  if ( $cl->GetLastWarning() )
    print "WARNING: " . $cl->GetLastWarning() . "\n\n";

  print "<li>Query '$q' retrieved $res[total] of $res[total_found] matches in $res[time] sec.\n</li>";
  print "<li>Query stats:\n";
  if ( is_array($res["words"]) )
    foreach ( $res["words"] as $word => $info )
      print "    '$word' found $info[hits] times in $info[docs] documents\n</li>";
  print "\n";


}  

结果:

Query 'good' retrieved 0 of 0 matches in 0.007 sec.
Query stats: 'good' found 7534 times in 7534 documents

但如果我试图搜索'goo',它什么都没搜索。

Query 'goo' retrieved 0 of 0 matches in 0.000 sec.
Query stats: 'goo' found 0 times in 0 documents

我想得到结果,所有内容都有一个单词/字母' goo '。

像这样:

Array(1 element) {
    0 Object {
        id = 1,
        message = Good Morning
    }
    2 Object {
        id = 2,
        message = Good Day!
    }
}

1 个答案:

答案 0 :(得分:1)

  

$ cl-&gt; SetFilter('created',array(3));   你确定你有创建= 3的记录吗?   同样对于通配符搜索,你应该做'goo *'(标记*)或设置expand_keywords = 1.