LAMP+Coreseek中文检索引擎使用详解

栏目: 编程工具 · 发布时间: 6年前

内容简介:有关于Coreseek的介绍、在LAMP上的安装及简单的测试可以参考我的另一篇文章CentOS 7下安装Coreseek中文检索引擎安装完之后我们还要修改PHP.ini文件:一开始是没有sphinx配置文件的,我们要首先建立sphinx.conf配置文件:

简要:

有关于Coreseek的介绍、在LAMP上的安装及简单的测试可以参考我的另一篇文章CentOS 7下安装Coreseek中文检索引擎 http://www.linuxidc.com/Linux/2017-05/143599.htm

一、安装 PHP 的sphinx扩展:

=====================1、安装libsphinxclient================

#linsphinxclient在解压缩出来的coreseek-3.2.14里面

cd /usr/local/src/coreseek/csft-3.2.14/api/libsphinxclient

#安装在 /usr/local/sphinxclient
./configure  --prefix=/usr/local/sphinxclient

make && make install

====================2、安装sphinxPHP扩展===================

cd /usr/local/src

wget http://pecl.php.net/get/sphinx-1.1.0.tgz

tar -zxvf sphinx-1.1.0.tgz

cd sphinx-1.1.0

# 使用安装php时生成的phpize来生成configure配置文件
/usr/local/php/bin/phpize   (或 /usr/bin/phpize) 
# 具体用哪个要取决于你的phpize文件所在的目录,这时你应该用 whereis phpize 来查看路径 

./configure --with-php-config=/usr/bin/php-config --with-sphinx=/usr/local/sphinxclient
# 其中 php-config 和 phpize 所在的目录是相同的,比如上面我用 /usr/bin/phpize,则在这一步我用 ./configure –with-php-config=/usr/bin/php-config。而/usr/local/sphinxclient就是上面的libsphinxclient的安装目录

make && make install

#注意,如果你的php版本是5.4,那么在这一步中会出现错误,提示在 sphinx.c:105:2,可以按下面方式修改:

#vim sphinx.c,找到105行

#把 retval = std_hnd->read_property(object, member, type TSRMLS_CC); 修改成 retval = std_hnd->read_property(object, member, type TSRMLS_CC, NULL);

#重新编译安装

#./configure --with-php-config=/usr/bin/php-config --with-sphinx=/usr/local/sphinxclient

#make && make install

安装完之后我们还要修改PHP.ini文件:

vim /etc/php.ini    #具体php.ini的位置自己查哈

#在php.ini文件的最后一行添加:
extension="sphinx.so"

#重启apache服务器
systemctl restart httpd.service

重启之后,在某个php文件里面添加 echo phpinfo();,在详情页面中假如有:

LAMP+Coreseek中文检索引擎使用详解

则说明安装 sphinx 的PHP扩展成功啦!

二、sphinx配置文件和测试表

一开始是没有sphinx配置文件的,我们要首先建立sphinx.conf配置文件:

cd /usr/local/coreseek/etc

cp sphinx.conf.dist sphinx.conf     #sphinx配置文件的默认名就是sphinx.conf

在这里先介绍一些sphinx.conf配置文件的结构:

#主数据源
source main{
{
#增量数据源
source delta:main{
}
#主数据索引
index main{
}
#增量数据索引
index delta:main{
}
#分布式索引
index dist1{
}
#索引器
indexer{
}
#服务进程
searchd{
}

由于初始的配置文件有很多注释,我们为了简单起见,可以将那些注释都删除了,以后要用到注释的时候可以参照sphinx.conf.dist,下面我贴出我的简化后的配置文件:

#主数据源(命名为main)
source main
{
    type                    = mysql

    sql_host                = localhost
    sql_user                = root
    sql_pass                = zhongjin
    sql_db                  = test
    sql_port                = 3306  # optional, default is 3306
    #sql_sock               = /var/lib/mysql/mysql.sock
    sql_query_pre           = SET NAMES UTF8    #mysql检索字符集
    sql_query_pre           = SET SESSION query_cache_type=OFF  #关闭缓存
    sql_query               = \
        SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content \
        FROM documents  #获取数据的 SQL 语句
    #sql_attr_timestamp     = date_added    #排序字段
    sql_ranged_throttle = 0
    sql_query_info      = SELECT * FROM documents WHERE id=$id  #这里的id对应于数据表的主键
}

#增量数据源(暂时用不到,先注释了)
#source src1throttled : main
#{
#   sql_ranged_throttle         = 100
#}

#主数据索引
index main
{
    source          = main  #指定主数据源
    path            = /usr/local/coreseek/var/data/main     #索引数据存放路径
    docinfo         = extern
    mlock           = 0
    morphology      = none


    #stopwords          = G:\data\stopwords.txt

    #wordforms          = G:\data\wordforms.txt
    #exceptions     = /data/exceptions.txt

    min_word_len        = 1
    charset_type        = utf-8
    html_strip              = 0
    #charset_table     = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44    F
}

#增量数据索引(暂时用不到)
#index test1stemmed : test1
#{
#   path            = /usr/local/coreseek/var/data/test1stemmed
#   morphology      = stem_en
#}

#分布式索引(暂时用不到)
#index dist1
#{
#   type                = distributed
#
#   local               = test1
#   local               = test1stemmed
#   agent               = localhost:9313:remote1
#   agent               = localhost:9314:remote2,remote3
#   agent_connect_timeout   = 1000
#
#   agent_query_timeout     = 3000
#}

#索引器(基本不用改)
indexer
{
    mem_limit           = 128M
}

#服务进程(不用修改)
searchd
{
    log                 = /usr/local/coreseek/var/log/searchd.log
    query_log           = /usr/local/coreseek/var/log/query.log
    read_timeout        = 5
    client_timeout      = 300

    max_children        = 30

    pid_file            = /usr/local/coreseek/var/log/searchd.pid

    max_matches         = 1000

    seamless_rotate     = 1

    preopen_indexes     = 0
    unlink_old          = 1

    mva_updates_pool    = 1M
    max_packet_size     = 8M

    max_filters         = 256
    max_filter_values   = 4096
}

在主数据源的设置中,sql_query 和 sql_query_info 字段都是 From document,这里用的是 安装 coreseek 时提供的测试表,位于 /usr/local/coreseek/etc/example.sql,根据设置,我们创建数据库和导入测试数据表:

mysql -uroot -pzhongjin

CREATE DATABASE test;

USE test;

#导入数据
SOURCE /usr/local/coreseek/etc/example.sql;

SHOW TABLES;

#+----------------+
#| Tables_in_test |
#+----------------+
#| documents      |
#| tags           |
#+----------------+

SELECT * FROM documents;

现在该表里面有四条数据,待会我们就是用该表来测试 sphinx。

现在配置文件有了,测试数据也有了,下面就是测试了。

三、sphinx搜索测试:

1、先创建索引:在新增数据之后,都得重新索引一次

创建索引命令:indexer,

-c 指定配置文件

–all 对所有索引重新编制索引

–rotate 用于轮换索引,主要是在不停止服务的时候增加索引

–merge 合并索引

针对 test:documents 创建索引

#基本所有的命令都在该目录下
cd /usr/local/coreseek/bin

./indexer -c /usr/local/coreseek/etc/sphinx.conf --all

LAMP+Coreseek中文检索引擎使用详解

可以看到,我们的配置文件,还有就是我们索引的是 main 主数据源,总共有四个文档,正好对应数据表。

2、查询关键字

查询命令:search

-c 指定配置文件

查关键字 test:

cd /usr/local/coreseek/bin 

./search -c /usr/local/coreseek/etc/sphinx.conf test

LAMP+Coreseek中文检索引擎使用详解

查询的结果如上图所示,图中的结果表明,test 在第一篇文档中出现两次,在第二篇文档中出现两次,在第三篇文档中出现一次,总共在三个文档中出现,总共出现5次。

查关键字 group:

./search -c /usr/local/coreseek/etc/sphinx.conf group

LAMP+Coreseek中文检索引擎使用详解 从结果中可以发现,group只在一个文档中出现一次,但是查表发现,数据表中有 group 和 groups,为什么 groups 没有被匹配出来?因为在英文分词中是以空格进行分词的,因此group和groups是两个不同的单词。

现在我们插入一条数据,看看能不能查询出来:

mysql -uroot -pzhongjin test

INSERT INTO documents(group_id,group_id2,date_added,title,content) VALUES(3,9,NOW(),'zhongjin','zhongjin is a student');

查关键字 zhongjin

./search -c /usr/local/coreseek/etc/sphinx.conf zhongjin;

结果是 zhongjin 关键字在0个文档中出现0次,为什么?

因为我们没有索引该条记录呀,前面不是说了嘛,新增数据之后,都得重新进行索引(这个有解决方案,后面有机会再说)

./indexer -c /usr/local/coreseek/etc/sphinx.conf --all

./search -c /usr/local/coreseek/etc/sphinx.conf zhongjin;

这回出来了吧。

3、试试中文?

mysql -uroot -pzhongjin test

INSERT INTO documents(group_id,group_id2,date_added,title,content) VALUES(3,9,NOW(),'LSGO实验室','华北电力大学LSGO实验室');

./indexer -c /usr/local/coreseek/etc/sphinx.conf --all

./search -c /usr/local/coreseek/etc/sphinx.conf LSGO实验室;

LAMP+Coreseek中文检索引擎使用详解

从结果来看,sphinx将‘LSGO实验室’分成了‘LSGO’和‘实验室’,而且‘lsgo’被匹配出来了,但是 ‘LSGO实验室’没出来!为什么?

因为我这里测试的是 sphinx,仅仅对英文起作用,对中文不起作用。

下面我讲对中文检索引擎进行搜索测试

更多详情见请继续阅读下一页的精彩内容 http://www.linuxidc.com/Linux/2017-05/143600p2.htm


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Dynamic Programming

Dynamic Programming

Richard Bellman / Dover Publications / 2003-03-04 / USD 19.95

An introduction to the mathematical theory of multistage decision processes, this text takes a "functional equation" approach to the discovery of optimum policies. The text examines existence and uniq......一起来看看 《Dynamic Programming》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

SHA 加密
SHA 加密

SHA 加密工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具