一、sphinx 全文检索
通过sphinx检索到id,然后到mysql里面拿到记录

什么是劝我呢检索?
结构化数据: 具有固定格式或者长度的数据
非结构化数据: 标题 内容 等不定长的数据
非机构化数据还叫做"全文数据"

检索分为两个过程:
索引创建 和 搜索索引

检索结构
php -> sphinx -> mysql
非结构化数据又叫全文数据,非固定长度字段例如文章标题搜索这类适用sphinx

全文数据搜索:
顺序扫描 : 如like查找
索引扫描 : 把非结构化数据中的内容提取出来一部分重新组织,这部分数据就叫做索引

全文检索分为两个过程
1.创建索引
2.搜索索引

创建索引:
sphinx通过语言处理组件会把搜索语句中的标点符号,语气助词给过滤掉,然后处理完毕后会
分析出一些词语,传给索引组件,然后排序去除重复词语.

搜索索引:
搜索关键字 提交 -> sphinx 分析出文档ID -> mysql通过ID查询数据并返回

中文分词需要coreseek
coreseek 支持中文的全文检索引擎

sphinx的安装
下载源码包 进行源码安装
./configure --prefix=/usr/local/sphinx --with-mysql=/usr/local/mysql
make && make install

sphinx 中重要的三个命令(bin目录下)
Indexer 创建索引命令
Searchd 启动进程命令
Search 命令行搜索命令

配置sphinx
cd /usr/local/sphinx/etc
cp sphinx.conf.dist sphinx.conf
vi sphinx.conf

配置文件
名为main的主数据源:source main{}
继承名为main的增量数据源:source delta:main{}
名为main的主索引:index main{}
继承名为main的增量索引:index delta:main{}
分布式索引:index dist1{}
索引器:indexer{}
服务进程:searchd{}

修改配置文件中主数据源的mysql配置,测试环境的数据源名为main,假设mysql中test有张表叫post
source main
{
# data source type. mandatory, no default value
# known types are mysql, pgsql, mssql, xmlpipe, xmlpipe2, odbc
type = mysql

#####################################################################
## SQL settings (for 'mysql' and 'pgsql' types)
#####################################################################

# some straightforward parameters for SQL source types
sql_host = localhost
sql_user = root
sql_pass = admin
sql_db = test
sql_port = 3306 # optional, default is 3306

sql_sock = /tmp/mysqld.sock #如果是linux下需要开启,指定sock文件

sql_query_pre = SET NAMES utf8
sql_query_pre = SET SESSION query_cache_type=OFF #关闭缓存

# main document fetch query
# mandatory, integer document ID field MUST be the first selected column
sql_query = \ #获取数据的sql语句
SELECT id,title,content FROM post

#sql_attr_uint = group_id #对排序字段进行注释

#sql_attr_timestamp = date_added #对排序字段进行注释

sql_query_info = SELECT * FROM post WHERE id=$id

}
注释掉所有增量数据源,修改主索引

index main
{
source = main

# index files path and file name, without extension
# mandatory, path must be writable, extensions will be auto-appended
path = /usr/local/sphinx/var/data/main

docinfo = extern
mlock = 0
morphology = none
# default is 1 (index everything)
min_word_len = 1

# charset encoding type
# optional, default is 'sbcs'
# known types are 'sbcs' (Single Byte CharSet) and 'utf-8'
charset_type = utf-8
# 'utf-8' default value is
# charset_table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F

}

注释掉所有的增量索引和分布式索引

配置完成后,创建索引
命令 indexer
-c 指定配置文件
--all 对所有索引重新编制索引
--rotate 用于轮换索引,主要是在不停止服务的时候,增加索引
--merge 合并索引
/usr/local/sphinx/sbin/indexer -c
/usr/local/sphinx/etc/sphinx --all
如果报错./indexer: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such file or directory
解决方式:cp /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18

测试
建立索引
[root@localhost bin]# ./indexer --all
Sphinx 0.9.9-release (r2117)
Copyright (c) 2001-2009, Andrew Aksyonoff

using config file '/usr/local/sphinx/etc/sphinx.conf'...
indexing index 'main'...
collected 2 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 2 docs, 47 bytes
total 0.033 sec, 1414 bytes/sec, 60.21 docs/sec
total 1 reads, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg
total 5 writes, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg

搜索
[root@localhost bin]# ./search linux1
Sphinx 0.9.9-release (r2117)
Copyright (c) 2001-2009, Andrew Aksyonoff

using config file '/usr/local/sphinx/etc/sphinx.conf'...
index 'main': query 'linux1 ': returned 1 matches of 1 total in 0.000 sec

displaying matches:
1. document=1, weight=1
id=1
title=linux1
content=salflsdkjsdglds

words:
1. 'linux1': 1 documents, 1 hits

sphinx相当于把数据库数据索引静态化到本地 来加快查询速度 避免消耗数据库,给数据库给降压,当数据更新的时候,需要重新indexer --all 或者用 indexer main重建所有索引 才会搜出新的数据

安装CORESEEK
coreseek里有2个文件夹 一个是mmseg中文分词包 还有一个是csft(其实就是sphinx)包 都要安装

首先安装mmseg中文分词
./configure --prefix=/usr/local/mmseg
编译时可能会报错config.status: error: cannot find input file: src/Makefile.in
通过automake来解决
首先检查是否安装了libtool如果没有
yum -y install libtool
automake
如果automake报错 原因可能是下列
Libtool library used but `LIBTOOL' is undefined
The usual way to define `LIBTOOL' is to add `AC_PROG_LIBTOOL'
to `configure.ac' and run `aclocal' and `autoconf' again.
If `AC_PROG_LIBTOOL' is in `configure.ac', make sure
its definition is in aclocal's search path.

原因分析
aclocal是个扫描程序, 负责扫描configure.ac中所有的宏定义并展开,
上面产生的原因就是找不到LIBTOOL宏的定义之处造成的.原因就是aclocal与libtool没有安装在一个相同目录下面aclocal是去默认 安装目录 /usr/share/aclocal下面搜索所有的.m4文件找所定义的宏,但是由于安装了多个aclocal,可能aclocal目录不存在,实际目录为/usr/share/aclocal1.10等,
这就造成了aclocal找不到m4文件的情况, 解决办法就是将文件夹aclocal1.10重命名为aclocal.或者显示指定路径 aclocal -I /usr/share/aclocal1.10 -I /usr/share/libtool/m4 --install 或者把/usr/share/libtool/m4下面的文件都copy至/usr/share/aclocal1.10中.
本人的机器上是/usr/share/下有2个文件夹 一个是aclocal,一个是aclocal-1.11,首先将aclocal的所有文件拷贝到aclocal-1.11下
cp -R /user/share/aclocal/* /usr/share/aclocal-1.11/

然后重新运行aclocal和autoconf
aclocal && autoconf
最后再次运行automake
automake

然后继续mmseg的安装
./configure --prefix=/usr/local/mmseg
make && make install

如果make 报错
There is an easy fix when you get such messages as "X--tag=CXX: command not found".
Just type:
export echo=echo
And try again.

所以输入
export echo=echo
然后再次运行安装
make && make install

安装csft
./configure --prefix=/usr/local/coreseek --with-mysql=/usr/local/mysql --with-mmseg=/usr/local/mmseg --with-mmseg-includes=/usr/local/mmseg/include/mmseg/ --with-mmseg-libs=/usr/local/mmseg/lib/
make && make install
安装完毕后 注意 coreseek 中的配置文件也是csft.conf 而不是 sphinx.conf
cd /usr/local/coreseek/etc
cp sphinx.conf.dist csft.conf
vim csft.conf

有些配置改动如下其他配置内容如上文的sphinx.conf
在索引源中注释掉txt
index main{
#stopwords = G:\data\stopwords.txt
#wordforms =G:\data\wordforms.txt
#exceptions =/data/exceptions.txt
#charset_type = sbcs
#添加下面2行 意思是把中文分词加入到配置文件中
char_type = zh_cn.utf-8
charset_dictpath =/usr/local/mmseg/etc/ #你安装mmseg的目录
}

保存配置
建立索引
cd /usr/local/coreseek/bin
./indexer --all
./search 中文词缀

如何用php去使用sphinx
Sphinx集成到php程序中有两种方式
1.Sphinx php 模块(这次我们选择使用的方式)
2.Sphinx api 类(位于coreseek源码包里的csft里的api文件夹里有一个sphinxapi.php,使用的时候包含这个php文件即可)

我们要使用sphinx需要做以下几件事:
1.首先要有数据
2.建立sphinx配置文件
3.生成索引
4,启动searchd 服务进程,并开启端口9312
5.用php客户程序去链接sphinx服务

/usr/local/coreseek/bin/searchd
启动进程命令 searchd
-c 指定配置文件
--stop 停止服务
--pidfile 显示指定pid文件
-p 指定端口(默认9312)
注意:这里启动的服务是searchd

使用php sphinx的模块
下载 sphinx-1.1.0.tgz

tar zvxf sphinx-1.1.0.tgz
cd sphinx-1.1.0
/usr/local/php/bin/phpize #用于生成一个configure的脚本
进入coreseek源码包的csft/api/libsphinxclent 目录下执行configure
./configure
make && make install
进入sphinx-1.1.0.gzt的源码包目录下 执行configure用于生成so 的shpinx模块
./configure --with-php-config=/usr/local/webserver/php/bin/php-config --with-sphinx
make && make install
安装成功后会有提示
Build complete.
Don't forget to run 'make test'.

Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/ (这个目录是sphinx.so所在的目录)

然后我们编辑php的ini文件 在extension中加入
extension=sphinx.so

然后重启nginx和php的服务
最后打印一下phpinfo 查看sphinx是否开启成功
如果成功开启 就可以使用php手册里的sphinx的方法和代码了

coreseek安装过程的更多相关文章

  1. sphinx的再创造coreseek的安装过程

    CoreSeek详细安装过程:coreseek-3.2.14.tar.gz下载链接: http://pan.baidu.com/s/1o6DNesE 解压缩安装mmseg分词程序: .tar.gz c ...

  2. coreseek 安装及使用方法详解

    coreseek 安装及使用 一般站点都需要搜索功能,如果是php+mysql站点,建议选择coreseek,如果是java站点建议使用lucene,coreseek 是一款很好的中文全文检索/搜索软 ...

  3. Coreseek安装测试配置指南(转)

    Sphinx--强大的开源全文检索引擎,Coreseek--免费开源的中文全文检索引擎 软件版本:coreseek-4.1 mmseg-3.2.14 autoconf-2.64 老版本的coresee ...

  4. webstorm下载&&安装过程&&打开项目

    一.webstorm下载 WebStorm 是jetbrains公司旗下一款JavaScript 开发工具.被广大中国JS开发者誉为"Web前端开发神器"."最强大的HT ...

  5. vagrant 1.8.6 安装过程及总结遇到的坑

    下面先总结遇到的问题,这些问题如果你也遇到,可能需要搜索很多次才能找到原因. 如果想看安装过程,可以先直接跳到后面第二部分部分. 1 问题汇总: 1.1 vagrant版本过高问题. vagrant ...

  6. 安装过程错误[INS-30131]

    问题:Oracle Database 安装过程错误[INS-30131]   原因:安装用户没有对临时文件夹的读写权限   解决方案:   1.以管理员身份运行cmd.exe 2.输入命令(需启动Se ...

  7. TortoiseGIT的安装过程详解

    TortoiseGIT简介 TortoiseGIT 是Git版本控制系统的一个免费开源客户端,它是git版本控制的 Windows 扩展.可以使你避免使用枯燥而且不方便的命令行.它完全嵌入 Windo ...

  8. RabbitMQ 集群安装过程详解

    一.安装Erlang 1.rabbitMQ是基于erlang的,所以首先必须配置erlang环境. 从erlang官网下载 otp 18.3.下载链接:http://erlang.org/downlo ...

  9. JAVA安装过程中出现的“javac不是内部或外部指令”的解决方法

    近来重新安装了JAVA,安装过程中出现问题,网上找到解决办法,汇总发布. 解决流程: 1.确定自己的环境变量设置没问题,没有出现遗漏 : . 等情况 (具体环境变量设置百度) 2.环境变量设置后 ,d ...

随机推荐

  1. iOS开发:创建真机调试证书及描述文件

    iOS开发:创建真机调试证书及描述文件 关于苹果iOS开发,笔者也是从小白过来的,经历过各种困难和坑,其中就有关于开发证书,生产证书,in_house证书,add_Hoc证书申请过程中的问题,以及上架 ...

  2. 最便捷的免费 SSL 证书解决方案(实测start SSL、Let's Encrypt、七牛免费SSL证书)

    谷歌从 2017 年起,Chrome 浏览器将也会把采用 HTTP 协议的网站标记为「不安全」网站:苹果从 2017 年 iOS App 将强制使用 HTTPS:在国内热火朝天的小程序也要求必须使用 ...

  3. Windows环境搭建Red5流媒体服务器指南

    Windows环境搭建Red5流媒体服务器指南 测试环境:Windows 7 一.   下载安装程序 red5-server 下载地址 https://github.com/Red5/red5-ser ...

  4. Centos7安装完毕后无法联网的解决方法

    /etc/sysconfig/network-scripts/ 用ls命令查看网卡配置文件名(在CENTOS7中网卡名貌似变成了随机数字,所以每台机器的网卡名并不相同)

  5. Jdk与Tomcat配置与安装

    一.jdk的安装与配置 先下载Tomcat与jdk的压缩包:在usr/local/src目录下下载,下载方法:wget+链接 (tar.gz) 解压tomcat与jdk的压缩包: tar –zvxf ...

  6. 每天5分钟 玩转OpenStack 目录列表

    最近在学习 OpenStack 的相关知识,一直苦于 OpenStack 的体系庞大以及复杂程度,学习没有进度,停滞不前.偶然机会在 51CTO 上发现了一个热点的专题关于 OpenStack 的,题 ...

  7. kubernetes部署Fluentd+Elasticsearch+kibana 日志收集系统

    一.介绍 1. Fluentd 是一个开源收集事件和日志系统,用与各node节点日志数据的收集.处理等等.详细介绍移步-->官方地址:http://fluentd.org/ 2. Elastic ...

  8. bootstrap自学总结不间断更新

    2.栅格系统 container-fluid 自适应宽度100% container 固定宽度(适应响应式) 屏幕宽度=x     x>=1200            1170 992< ...

  9. 移动端web开发,click touch tap区别

    转自: http://blog.csdn.net/sly94/article/details/51701188 移动端用tap时会有穿透问题 一:click与tap比较 click与tap都会触发点击 ...

  10. [LeetCode] Two Sum II - Input array is sorted 两数之和之二 - 输入数组有序

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...