csft 3.1rc sphinx 在windows 2003下 中文全文搜索 安装实例 附sphinx-php-api

  自从看过了张宴写的基于Sphinx+MySQL的千万级数据全文检索(搜索引擎)架构设计[原创]  之后,一直对sphinx这个系统很感兴趣,不过实在是对linux不熟悉,所以一直也没有自己测试一下,直到sphinx出了windows的版本的,查看了一些资料,终于在windows 2003 服务器上安装好并且调试成功了sphinx 中文全文搜索。

我的网站,之前在mysql中用like %keyword% ,然后将query的内容存放到文本cache中,不过mysql 的中文like效率实在是太低了,我甚至都将文本cache的更新时间设置为3600*24*7。就这样还经常有新词把mysql查down。后来就直接用了百度搜索,百度收录的数量大概是网站数据库的一半,25万条左右,有很多事没有收录的,总之不尽人意啊。所以看到了sphinx windows版本,就开始安装调试了

开始准备,网站的content内容太大了,大概有8个G左右,所以放弃content的全文搜索,只做了title的索引
将数据库中的 id,title 两个字段进行全文索引,一共50万条+数据,索引之后25M左右。上线之后跑的还不错。

1 下载经过coreseek编译过的sphinx windows版本,coreseek是中文的sphinx爱好者社区,站上有一些sphinx资料,有空的时候可以翻翻
 http://www.coreseek.cn/news/5/68/   下载Windows版本 解压缩到D:\csft
2下载中文词库  标准词库  解压缩到 D:\csft\data
3 下载 activepython 2.5.4.4 (重要,一定要2.5版本)https://www.activestate.com/activepython/downloads/
4 将 D:\csft\conf\csft.conf.in  拷贝到 D:\csft\bin\csft.conf  ,在D:\csft\下新建一个data目录用来存放索引文件,一个log目录方日志文件
5 开始修改 csft.conf  这里比较麻烦,需要多注意

   type        = mysql # 数据源,我这里是mysql
    sql_host    = localhost # 数据库服务器
    sql_user    = root # 数据库用户名
    sql_pass    = ” # 数据库密码
    sql_db      = test # 数据库
    sql_port    = 3306 # 数据库端口

   sql_query_pre   = SET NAMES utf8 # 去掉此行前面的注释,如果你的数据库是uft8编码的
#sql_query 是你希望执行的mysql的语句,我这里只做了id,title,足够了。\是一个回车

    sql_query                = \   
        SELECT id, title \
        FROM main_db

  index test1
    {
    # 放索引的目录
     path   = D:/csft/data/
    # 编码
     charset_type  = utf-8
     #  指定utf-8的编码表
     charset_table  = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F
     # 简单分词,只支持0和1,如果要搜索中文,请指定为1
     ngram_len    = 1
    # 需要分词的字符,如果要搜索中文,去掉前面的注释
     ngram_chars   = U+3000..U+2FA1F
    }
   #我把这里也注释掉了。不知道怎么用
    # index test1stemmed : test1
    # {
     # path   = @CONFDIR@/data/test1stemmed
     # morphology  = stem_en
    # }

    # 如果没有分布式索引,注释掉下面的内容

    # index dist1
    # {
     # ‘distributed’ index type MUST be specified
     # type    = distributed

     # local index to be searched
     # there can be many local indexes configured
     # local    = test1
     # local    = test1stemmed

     # remote agent
     # multiple remote agents may be specified
     # syntax is ‘hostname:port:index1,[index2[,...]]
     # agent    = localhost:3313:remote1
     # agent    = localhost:3314:remote2,remote3

     # remote agent connection timeout, milliseconds
     # optional, default is 1000 ms, ie. 1 sec
     # agent_connect_timeout = 1000

     # remote agent query timeout, milliseconds
     # optional, default is 3000 ms, ie. 3 sec
     # agent_query_timeout  = 3000
    # }
#索引设置
indexer
{
    mem_limit            = 64M  #服务器内存比较大,这里改成64M了

}
    # 搜索服务需要修改的部分
    searchd
    {
     # 日志
     log     = D:/csft/log/searchd.log

     # PID file, searchd process ID file name
     pid_file   = D:/csft/log/searchd.pid

     # windows下启动searchd服务一定要注释掉这个
     # seamless_rotate  = 1
    }

6  处理词库   unigram.txt  就是data的压缩包解压缩出来的
d:\csft\bin\mmseg -u d:\csft\data\unigram.txt  
把生成的文件改名为uni.lib 

确认data文件夹下已经解压缩好了的uni.lib 即可。

7 建立索引。
命令行下
  D:\csft\bin>indexer.exe –all

Snap3

8     D:\csft\bin>search.exe show   如果有内容,说明索引正常。

9  启动服务器端

D:\csft\bin>searchd.exe
Coreseek Full Text Server 3.1
 Copyright (c) 2006-2009 coreseek.com
WARNING: forcing –console mode on Windows
using config file ‘./csft.conf’…
listening on all interfaces, port=3312
accepting connections

10 调用php api  
sphinxapi.php  在官方的安装包里面。csft3.1rc里面居然不包含,真晕,我直接拆了放在服务器上。sphinx-php-api
并且,对原test.php 进行了修改,可以直接在在浏览器中访问。
   <?php
    require ’sphinxapi.php’;
    $s = new SphinxClient();
    $s->SetServer(’localhost’,3312);
    $result = $s->Query(’show’);
    var_dump($result);
    ?>

php d:/csft/api/foo.php

11 调试php 
仔细看看 test.php 虽然是英文的。。。。
我实际用的时候修改了几处,也一并写出来吧,为了像我这样的半个技术的人也可以用

$sortby = “@relevance DESC, @id DESC”;
这里表示 ,用最大的匹配内容 ,id倒序排列。
$limit 控制返回的内容的范围,和mysql使用方法一样 0,20   或 20,20
正常的话,会返回 
$res[total] 总条目 
$res[matches] 匹配的id 和id的权重
$res[time] 查询花费时间

嗯。我就需要这几个就够了。 获得返回的id 然后用”,”连接 $sql_in   。然后拼到一句sql中
SELECT *  FROM `main_db` WHERE `id` IN (“.$sql_in.”)   就得到自己想要的结果了

12 调试没问题,把sphinx-php-api 整理成一个类,然后调试网站,没问题。接下来需要把searchd.exe注册成一个服务
D:\csft\bin>searchd.exe –install –config d:/csft/bin/csft.conf
Coreseek Full Text Server 3.1
 Copyright (c) 2006-2009 coreseek.com
Installing service…
Service ‘searchd’ installed succesfully.

然后到服务管理里面找到searchd 启动服务,我第一次启动的时候提示

在 本地计算机 无法启动 searchd 服务。

错误 1067: 进程意外终止。

—————————
确定  
—————————
后来经过检查,还是是因为配置文件的问题,仔细检查配置文件中,所有的路径,凡是没有注释掉的语句中,@CONFDIR@/这个都得修成实际路径,就没有问题了。 应该是sphinx 在服务状态下比在命令行下执行检查要严格。

==============================
下载压缩包中有一个我改过的测试文件,叫做new.php ,放到服务器上,用浏览器访问youdomain/new.php 可以输入关键词提交,来看测试结果。方便一些。

You can skip to the end and leave a response. Pinging is currently not allowed.

One Response to “csft 3.1rc sphinx 在windows 2003下 中文全文搜索 安装实例 附sphinx-php-api”

  1. nzinfo 说:

    你可以考虑把正文也建到索引里。 8个G实在不算大。

Leave a Reply

You must be logged in to post a comment.

Subscribe to RSS Feed Follow me on Twitter!