windows11 php Sphinx安装

2022-06-22  本文已影响0人  街头民工

参考文档
参考文档
参考文档

下载 Sphinx:官网下载地址

image.png

下载后解压

image.png
source src1
{
    #数据库的配置信息
    type            = mysql
    sql_host        = 127.0.0.1
    sql_user        = bos_2
    sql_pass        = 123456
    sql_db          = bos_2
    sql_port        = 3306
    sql_query_pre   = SET NAMES utf8
    # 一个数据源中只能有一个主查询,这条语句取出的数据就是sphin将要创建全文索引的语句
    # 主查询的要求:第一个字段必须是ID,如果名字不为ID,取个别名叫id(类型必须是非零唯一、不重复的整数)
    # sphinx只能对属性字段排序,sphinx要排序的字段必须取出该字段,sphin排序必须将某个字段定义成一个属性
    sql_query       = SELECT id,search_gather FROM 表名称;
}

# index定义
# 配置索引--》生成的索引文件
# 说明:一个数据源对应一个索引的配置
index test1
{
    source          = src1
    path            = E:/sphinx/sphinx-3.4.1/data/test1
    mlock         = 0
    enable_star    = 1
    min_word_len  = 2
    min_prefix_len = 0
    min_infix_len = 2
    ngram_len = 1
    # 需要分词的字符,如果要搜索中文,去掉前面的注释
    ngram_chars = U+3000..U+2FA1F
        dict            = keywords
        mlock           = 0
        morphology      = none
        min_word_len    = 1
        charset_type    = utf-8
        charset_table   = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F
}


indexer
{
    mem_limit       = 128M
}


searchd
{
    listen          = 9312
    listen          = 9306:mysql41
    log             = E:/sphinx/sphinx-3.4.1/log/searchd.log
    query_log       = E:/sphinx/sphinx-3.4.1/log/query.log
    read_timeout    = 5
    # 最大返回的记录数
    #@max_children    = 30
    pid_file        = E:/sphinx/sphinx-3.4.1/log/searchd.pid
    max_matches     = 1000000

    # windows下启动searchd服务一定要注释掉这个
    # seamless_rotate = 1
    preopen_indexes = 1
    unlink_old      = 1
    workers         = threads
    binlog_path     = E:/sphinx/sphinx-3.4.1/data
}
image.png

把目录share/doc/api下的sphinxapi.php 复制到你项目当中

image.png
include './sphinxapi.php';
$cl = new SphinxClient();
$q = $_GET['key']?'':'*ph*'; //模拟关键字
//$mode = SPH_MATCH_ALL;
$host = "127.0.0.1";// sphinx的服务地址  此处用的是本地服务 切记 不是数据库地址!!!
$port = 9312;// sphinx监听端口号
$index = "test1";   // 此处为配置文件中配置的索引项
$cl->SetServer ( $host, $port );
$cl->SetConnectTimeout(10);
$cl->SetArrayResult(true);
$cl->SetLimits(1,1000);//要获取所有数据是这里第三个参数控制,默认是1000,太大会影响效率
//$cl->SetMatchMode(SPH_MATCH_ALL);//这个关闭它,不然会提示警告
$res = $cl->Query( $q, $index );
foreach ($res['matches'] as $key=>$value){
    if(empty($id_str)){
        $id_str = $value['id'];
    }else{
        $id_str .= ','.$value['id'];
    }
}
print_r(count(explode(',',$id_str)));
if ( $res===false )
{
    print "Query failed: " . $cl->GetLastError() . ".\n";

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

    print "hcl_1_Query '$q' retrieved $res[total] of $res[total_found] matches in $res[time] sec.\n";
    print "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";
    print "\n";



}

上一篇 下一篇

猜你喜欢

热点阅读