尝试使用php+sphinx

22 篇文章 0 订阅

中文 sphinx 官方网站  http://www.coreseek.cn

 

首先应该装好所有的需要软件包,防止到时候不知道出错了,啥原因。然后按官方说明做就可以了。

 

 

##以下为正常索引全部数据时的提示信息 

# /usr/local/coreseek/bin/indexer -c etc/csft.conf --all

 

 

##以下为正常索引指定数据时的提示信息:

# /usr/local/coreseek/bin/indexer -c etc/csft.conf xml

##以下为正常测试搜索时的提示信息:

# /usr/local/coreseek/bin/search -c etc/csft.conf

 ##如要停止搜索服务,请使用/usr/local/coreseek/bin/searchd -c etc/csft.conf --stop

 

 

##一、安装:
##download http://www.coreseek.cn/uploads/csft/3.2/coreseek-3.2.13.tar.gz
#tar xzvf coreseek-3.2.13.tar.gz


##切换到root用户,确保拥有完整的权限来安装软件
#su root
##或者
#sudo root bash
#su -


#cd coreseek-3.2.13

##中文测试环境检查:
# locale
##以下为核心项,locale为zh_CN.UTF-8,就可以正常显示和输入中文;locale设置功能由操作系统自身支持,BSD/Linux均可开启;该功能,不是coreseek提供的!!!
##如果不能正常显示中文,则后面的中文测试部分无法正常进行,但不会影响coreseek的实际功能;
    LANG=zh_CN.UTF-8
    LANGUAGE=zh_CN:zh
    LC_ALL="zh_CN.UTF-8"

##如果显示的不是zh_CN.UTF-8,请参考以下网址进行设置:
##locale设置
##命令行显示中文
##putty显示中文
##SecureCRT显示中文


##确保可以正常显示,否则请检查当前环境的locale配置,以及当前环境或者客户端已设置好支持UTF-8中文字符显示
#cat testpack/var/test/test.xml


##安装coreseek开发的mmseg,为coreseek提供中文分词功能
#cd mmseg-3.2.13
#./bootstrap
##ubuntu环境下,需要使用ACLOCAL_FLAGS="-I /usr/share/aclocal" ./bootstrap
#./configure --prefix=/usr/local/mmseg3
#make
#make install
##安装完成后,mmseg使用的词典和配置文件,将自动安装到/usr/local/mmseg3/etc中

##中文分词测试,如果显示不正常,请检查当前环境下的locale和UTF-8中文字符显示设置
# /usr/local/mmseg3/bin/mmseg -d /usr/local/mmseg3/etc src/t1.txt
    中文/x 分/x 词/x 测试/x
    中国人/x 上海市/x

Word Splite took: 1 ms.

##coreseek安装错误预警:如果编译中出现如下错误,可以按照解决方法处理:
##DWORD定义冲突:
    /usr/local/mmseg3/include/mmseg/csr_typedefs.h:64: 错误: 相互冲突的声明‘typedef unsigned int DWORD’
    /usr/include/sqltypes.h:97: 错误: ‘DWORD’早先被声明为‘typedef long unsigned int DWORD’

##解决方法:vi /usr/local/mmseg3/include/mmseg/csr_typedefs.h,到64行,注释该行,保存退出重新编译即可!


##安装coreseek:
#cd csft-3.2.13
#./configure --prefix=/usr/local/coreseek --without-python --without-mysql --with-mmseg --with-mmseg-includes=/usr/local/mmseg3/include/mmseg/ --with-mmseg-libs=/usr/local/mmseg3/lib/
#make
#make install

##配置测试,测试是否可以正确运行
#/usr/local/coreseek/bin/indexer -c /usr/local/coreseek/etc/sphinx-min.conf.dist
##以下为正常测试时的提示信息:
    Coreseek Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)]
    Copyright (c) 2007-2010,
    Beijing Choice Software Technologies Inc (http://www.coreseek.com)

     using config file '/usr/local/coreseek/etc/sphinx-min.conf.dist'...
    total 0 reads, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg
    total 0 writes, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg

##至此,coreseek基础环境可以正常安装,我们可以开始后续的工作啦。
##注意:基础环境仅支持xml数据源;如需要mysql或者python数据源,请继续按照下面的步骤操作!!!


##二、数据源支持

##mysql数据源支持和xml数据源支持,系统需要安装相关的基础依赖库,否则无法支持对应的功能
##freebsd6/7/8:
#pkg_add -r mysql50-client libxml2 expat

##centos5.4/5.5: fedora12/13 : rhel5.5
#yum install mysql-devel libxml2-devel expat-devel

##debian5 : ubuntu9/10:
#apt-get install mysql-client libmysqlclient15-dev   libxml2-dev libexpat1-dev

##gentoo10:
##安装环境自带了libmysqlclient libxml2 libexpat

##opensuse10/11:
##yast安装 libmysqlclient-devel libxml2 libxml2-devel libexpat-devel

##slackware13.0/13.1:
#slapt-get --install mysql libxml2 expat

##archlinux-2010:
#pacman -S libmysqlclient   libxml2 expat

##重新安装coreseek,以支持mysql数据源和xml数据源
#cd csft-3.2.13
#./configure --prefix=/usr/local/coreseek --with-mmseg --with-mmseg-includes=/usr/local/mmseg3/include/mmseg/ --with-mmseg-libs=/usr/local/mmseg3/lib/
#make
#make install

##三、coreseek中文全文检索测试
#cd testpack
# /usr/local/coreseek/bin/indexer -c etc/csft.conf
##以下为正常情况下的提示信息:
    Coreseek Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)]
    Copyright (c) 2007-2010,
    Beijing Choice Software Technologies Inc (http://www.coreseek.com)

     using config file 'etc/csft.conf'...
    total 0 reads, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg
    total 0 writes, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg

# /usr/local/coreseek/bin/indexer -c etc/csft.conf --all
##以下为正常索引全部数据时的提示信息:
    Coreseek Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)]
    Copyright (c) 2007-2010,
    Beijing Choice Software Technologies Inc (http://www.coreseek.com)

     using config file 'etc/csft.conf'...
    indexing index 'xml'...
    collected 3 docs, 0.0 MB
    sorted 0.0 Mhits, 100.0% done
    total 3 docs, 7585 bytes
    total 0.075 sec, 101043 bytes/sec, 39.96 docs/sec
    total 2 reads, 0.000 sec, 5.6 kb/call avg, 0.0 msec/call avg
    total 7 writes, 0.000 sec, 3.9 kb/call avg, 0.0 msec/call avg

# /usr/local/coreseek/bin/indexer -c etc/csft.conf xml
##以下为正常索引指定数据时的提示信息:
    Coreseek Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)]
    Copyright (c) 2007-2010,
    Beijing Choice Software Technologies Inc (http://www.coreseek.com)

     using config file 'etc/csft.conf'...
    indexing index 'xml'...
    collected 3 docs, 0.0 MB
    sorted 0.0 Mhits, 100.0% done
    total 3 docs, 7585 bytes
    total 0.069 sec, 109614 bytes/sec, 43.35 docs/sec
    total 2 reads, 0.000 sec, 5.6 kb/call avg, 0.0 msec/call avg
    total 7 writes, 0.000 sec, 3.9 kb/call avg, 0.0 msec/call avg

# /usr/local/coreseek/bin/search -c etc/csft.conf
##以下为正常测试搜索时的提示信息:
    Coreseek Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)]
    Copyright (c) 2007-2010,
    Beijing Choice Software Technologies Inc (http://www.coreseek.com)

     using config file 'etc/csft.conf'...
    index 'xml': query '': returned 3 matches of 3 total in 0.093 sec

    displaying matches:
    1. document=1, weight=1, published=Thu Apr  1 22:20:07 2010, author_id=1
    2. document=2, weight=1, published=Thu Apr  1 23:25:48 2010, author_id=1
    3. document=3, weight=1, published=Thu Apr  1 12:01:00 2010, author_id=2

    words:


# /usr/local/coreseek/bin/search -c etc/csft.conf -a Twittter和Opera都提供了搜索服务
##以下为正常测试搜索关键词时的提示信息:
    Coreseek Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)]
    Copyright (c) 2007-2010,
    Beijing Choice Software Technologies Inc (http://www.coreseek.com)

     using config file 'etc/csft.conf'...
    index 'xml': query 'Twittter和Opera都提供了搜索服务 ': returned 3 matches of 3 total in 0.038 sec

    displaying matches:
    1. document=3, weight=24, published=Thu Apr  1 12:01:00 2010, author_id=2
    2. document=1, weight=4, published=Thu Apr  1 22:20:07 2010, author_id=1
    3. document=2, weight=3, published=Thu Apr  1 23:25:48 2010, author_id=1

    words:
    1. 'twittter': 1 documents, 3 hits
    2. '和': 3 documents, 15 hits
    3. 'opera': 1 documents, 25 hits
    4. '都': 2 documents, 4 hits
    5. '提供': 0 documents, 0 hits
    6. '了': 3 documents, 18 hits
    7. '搜索': 2 documents, 5 hits
    8. '服务': 1 documents, 1 hits

# /usr/local/coreseek/bin/searchd -c etc/csft.conf
##以下为正常开启搜索服务时的提示信息:
    Coreseek Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)]
    Copyright (c) 2007-2010,
    Beijing Choice Software Technologies Inc (http://www.coreseek.com)

    using config file 'etc/csft.conf'...
    listening on all interfaces, port=9312
##如要停止搜索服务,请使用/usr/local/coreseek/bin/searchd -c etc/csft.conf --stop

##然后,请参考csft-3.2.13下api目录中的相关文件,使用PHP、Python、Ruby、Java来测试搜索服务;也可以前往< ahref="/products-install/step_by_step/">搜索服务建立三步曲,查看第三步使用PHP测试。

##四、继续
##通过以上步骤,coreseek已经安装测试完成,可以提供正常的xml数据源索引以及提供对应的搜索服务了
##下一步工作,请查看手册,准备好mysql数据信息,以及进行mysql数据源的测试,并在您的应用中调用搜索服务;mysql数据源的配置可参考testpack/etc/csft_mysql.conf文件

 

 

 

 

然后开始配置数据源 

 

数据表字段取值对应到Coreseek的索引中,其关系如下:

数据库:
SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content ROM documents
id     : 自增字段,表的主键,整数字段
group_id   : 整数字段
date_added : datetime字段,使用UNIX_TIMESTAMP转换为整数的timestamp
title     : 字符串字段
content   : 文本字段

Coreseek索引:
id        :对应SQL查询的第一个字段,系统自动使用,内部属性名为@id,使用SetFilter()过滤
sql_attr_uint   :对应值是整数,以上group_id、date_added都可使用,使用SetFilter()过滤
sql_attr_timestamp:对应值是UNIX的timestamp,以上date_added可使用,可用SetFilter()过滤,多排序用
sql_attr_str2ordinal:对应值是字符串,以上title可以使用,不可用于过滤,仅用于排序
全文检索字段   :对应值不限,非id、“sql_attr_类型”设置的字段自动成为全文字段,使用Query()搜索

 

#源定义
source mysql
{
type = mysql

sql_host = localhost
sql_user = root
sql_pass =
sql_db = test
sql_port = 3306
sql_query_pre = SET NAMES utf8

sql_query = SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content ROM documents
#sql_query第一列id需为整数
#title、content作为字符串/文本字段,被全文索引
sql_attr_uint = group_id #从SQL读取到的值必须为整数
sql_attr_timestamp = date_added      #从SQL读取到的值必须为整数,作为时间属性

sql_query_info = SELECT * FROM documents WHERE id=$id  #命令行查询时,从数据库读取原始数据信息
}

#index定义
index mysql
{
source = mysql             #对应的source名称
path = var/data/mysql
docinfo = extern
mlock = 0
morphology = none
min_word_len = 1
html_strip = 0
charset_dictpath = /usr/local/mmseg3/etc/ #BSD、Linux环境下设置,/符号结尾
#charset_dictpath = etc/ #Windows环境下设置,/符号结尾
charset_type = zh_cn.utf-8
}

 

 

第三步:测试

  首先,索引:使用bin目录下的indexer,执行:indexer -c 配置文件的路径 index名称

# /usr/local/coreseek/bin/indexer -c etc/csft.conf --all    ##--all表示全部索引
##Windows下面为bin/indexer -c etc/csft.conf --all
##以下为正常索引全部数据时的提示信息:部分数据信息根据实际数据情况变化
    Coreseek Fulltext 3.2 [ Sphinx 0.9.9-release (r2117)]
    Copyright (c) 2007-2010,
    Beijing Choice Software Technologies Inc (http://www.coreseek.com)

     using config file 'etc/csft.conf'...
    indexing index '索引名称'...
    collected 3 docs, 0.0 MB
    sorted 0.0 Mhits, 100.0% done
    total 3 docs, 7585 bytes
    total 0.075 sec, 101043 bytes/sec, 39.96 docs/sec
    total 2 reads, 0.000 sec, 5.6 kb/call avg, 0.0 msec/call avg
    total 7 writes, 0.000 sec, 3.9 kb/call avg, 0.0 msec/call avg

 

-----------------------------------------------------------------------------------------------------------------------

 

/var/tmp/coreseek-3.2.13/testpack/etc    //csft.conf配置文件目录

 

以下为正常索引全部数据时的提示信息

 /usr/local/coreseek/bin/indexer -c etc/csft.conf --all  //开始(端口需关闭)

 

 

//这里必须切换到对应目录才可以哦,否则会报错

 例 cd /usr/local/coreseek

 、、开始

 

 /usr/local/coreseek/bin/searchd -c /var/tmp/coreseek-3.2.13/testpack/etc/csft.conf

 

、、终止

/usr/local/coreseek/bin/searchd -c /var/tmp/coreseek-3.2.13/testpack/etc/csft.conf --stop

 

 

 

 

如果出现  connection to localhost:9312 failed (errno=13, msg=Permission denied)

的错误。

 

使用
gh
setenforce 0 设置SELinux 成为permissive模式

 

就可以了

 

 

/usr/local/coreseek/bin/indexer --all --rotate

 

不用关闭sphinx服务直接更新全部索引

 

/usr/local/coreseek/bin/indexer --merge --rotate

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值