安装(Windows)

1.官方下载

Sphinx下载地址: 下载

2.解压并重命名

此处下载版本为3.0.3,将 sphinx 文件夹命名为sphinx

3.文件夹目录介绍

sphinx
--api(各语言支持的api)
--bin(二进制程序)
--doc(文档说明)
--etc(配置文件:conf/sql)
--misc
--src
# 手动创建以下两个文件夹
--data
--log

4.设置配置文件

(1)将sphinx/etc/sphinx-min.conf.dist文件复制到sphinx/bin/目录下,并重命名为sphinx.conf

注:sphinx/etc/sphinx.conf.dist为带注释的详细的

(2)设置配置项

主要是以下为配置函数:

  • source src1{} --- 连接数据库的基本配置
# 连接的数据库类型
type = mysql
# 连接的数据库主机
sql_host = localhost
# 数据库连接的用户名,默认为test
sql_user = root
# 数据库连接的密码,默认为空
sql_pass =123123
# 连接的数据库名称,默认为test
sql_db = test
# 连接数据库的端口号,默认为3306
sql_port = 3306
# 操作的数据表执行的查询语句
sql_query       = \SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content \FROM documents
  • index test1{}
# 索引数据存放目录,默认为/var/data/test1
path = D:\Service\sphinx\data\test1
# 设置中文匹配
min_word_len    = 1
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
min_word_len    = 1
min_prefix_len  = 0
min_infix_len   = 1
# 开启中文分词支持
ngram_len       = 1
# 需要分词的字符
ngram_chars     = U+3000..U+2FA1F
  • index test1stemmed : test1{}
# 主要需要修改的配置项,默认为/var/data/test1stemmed
path = D:\Service\sphinx\data\test1stemmed
  • index rt{}
# 主要需要修改的配置项,默认为/var/data/rt
path = D:\Service\sphinx\data\rt
# 指定对哪些字段进行匹配
rt_field = name
rt_field = ename
rt_field = setmeal
rt_field = category
rt_field = country
rt_field = traffic
rt_field = body#
rt_attr_uint    = offerid
  • searchd{}
# 自定义日志文件位置
log = D:\Service\sphinx\log\searchd.log
query_log = D:\Service\sphinx\log\query.log
pid_file = D:\Service\sphinx\log\searchd.pid

以下几项不需要修改默认值,即可直接使用

  • source src1throttled : src1{}

分布式索引的相关配置,没有则可以不修改

  • index dist1{}

  • indexer{}

  • common{}

5.操作数据库,导入样例数据

(1)进入到mysql命令行,执行命令

D:\phpStudy\PHPTutorial\MySQL\bin>mysql -uroot -p
Enter password: *************
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.5.53 MySQL Community Server (GPL)mysql> use test;# 恢复样例数据到数据库
mysql> source /D:\Service\sphinx\etc/eaxmple.sql# 新增两个数据表,documents和tags
mysql> show tables;
documents
tags

6.生成索引文件

cmd命令行进入到sphinx/bin/目录下

# 生成索引文件
> indexer.exe --config sphinx.conf --all
Sphinx 3.0.3-dev (commit facc3fb)
Copyright (c) 2001-2018, Andrew Aksyonoff
Copyright (c) 2008-2016, Sphinx Technologies Inc (http://sphinxsearch.com)using config file 'sphinx.conf'...
WARNING: key 'docinfo' was permanently removed from Sphinx configuration. Refer to documentation for details.
WARNING: key 'dict' was permanently removed from Sphinx configuration. Refer to documentation for details.
WARNING: key 'mva_updates_pool' was permanently removed from Sphinx configuration. Refer to documentation for details.
indexing index 'test1'...
collected 4 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 4 docs, 0.2 Kb
total 1.0 sec, 0.2 Kb/sec, 3 docs/sec
indexing index 'test1stemmed'...
collected 4 docs, 0.0 MB
sorted 0.0 Mhits, 100.0% done
total 4 docs, 0.2 Kb
total 1.0 sec, 0.2 Kb/sec, 3 docs/sec
skipping non-plain index 'dist1'...
skipping non-plain index 'rt'...

【注】新版sphinx的bin目录下已经没有search.exe程序,所以不能直接在命令行执行返回结果,只能使用api接口返回数据。

7.开启搜索服务,保持后台运行

> searchd.exe --pidfile[Tue May 15 09:02:14.690 2018] [7776] using config file './sphinx.conf'...
listening on all interfaces, port=9312
listening on all interfaces, port=9306
Sphinx 3.0.3-dev (commit facc3fb)
Copyright (c) 2001-2018, Andrew Aksyonoff
Copyright (c) 2008-2016, Sphinx Technologies Inc (http://sphinxsearch.com)

PHP开启sphinx扩展

1.下载php_sphinx扩展: 前往

具体需要下载的版本需要查看phpinfo信息:

Architecture         ==》x86/x64
PHP Extension Build  ==》NTS/NS

下载并解压后,将php_sphinx.dll文件放到php/ext目录下

2.修改php.ini配置文件

# 在 Dynamic Extensions 列表中添加php_sphinx扩展
extension=php_sphinx.dll

修改后重启apache服务

3.在phpinfo.php输出的信息中查看sphinx扩展是否安装成功

            sphinx
sphinx support   enabled
Version          1.3.2
Revision     $Revision$

代码实现

1.样例数据表test.documents记录:

 id   group_id   group_id2   date_added             title             content1    1             5        2018-05-14 09:12:25   test one           this is my test document number one. also checking search within phrases.2    1             6        2018-05-14 09:12:25   test two           this is my test document number two3    2             7        2018-05-14 09:12:25   another doc        this is another group4    2             8        2018-05-14 09:12:25   doc number four    this is to test groups 

2.PHP代码实现

一般实现

<?php
require('sphinxapi.php');$sphinx = new SphinxClient();
$sphinx->SetServer('localhost',9312);
$sphinx->SetMatchMode(SPH_MATCH_ANY);
$sphinx->SetArrayResult ( true );
$res = $sphinx->Query($_GET['key'],'*');
var_dump($res);

thinkphp5使用介绍

1.将sphinxapi.php文件放到extend目录下

2.在控制器方法中使用(app/api/index

public function test()
{$sphinx = new \SphinxClient();// sphinx的主机名和端口$sphinx->SetServer('localhost',9312);// 匹配模式$sphinx->SetMatchMode(SPH_MATCH_ANY);// 设置返回结果集为php数组格式$sphinx->SetArrayResult ( true );$res = $sphinx->Query(input('key'),'*');var_dump($res);
}

3.url访问:
http://localhost/mypro/api/index/test?key=test

4.输出数据

D:\web\COD\api\application\api\controller\Index.php:21:
array (size=10)'error' => string '' (length=0)'warning' => string '' (length=0)'status' => int 0'fields' => // 查询显示的字段名array (size=2)0 => string 'title' (length=5)1 => string 'content' (length=7)'attrs' => array (size=2)'group_id' => string '1' (length=1)'date_added' => string '2' (length=1)'matches' => // 匹配的结果,返回匹配记录的id和权重(权重越大,匹配条件越多)array (size=3)0 => array (size=3)'id' => string '1' (length=1)'weight' => int 2421'attrs' => array (size=2)...1 => array (size=3)'id' => string '2' (length=1)'weight' => int 2421'attrs' => array (size=2)...2 => array (size=3)'id' => string '4' (length=1)'weight' => int 1442'attrs' => array (size=2)...'total' => int 3'total_found' => int 3'time' => float 0'words' => array (size=1)'test' => array (size=2)'docs' => int 6'hits' => int 10

在ThinkPHP5项目中应用

1.修改配置信息sphinx/bin/sphinx.conf

source src1{
# 省略其他配置
sql_user  = root
sql_pass  = 123123
sql_db    = shopMall
sql_query = \SELECT id,offerid, name, ename, setmeal, category, country, traffic, os, body, inventory_title, shop \FROM i_offer
sql_attr_uint = offerid
# 省略其他配置
}
index rt
{type  = rtpath  = D:\Service\sphinx\data\rtrt_field  = namert_field  = enamert_field  = setmealrt_field  = categoryrt_field  = countryrt_field  = trafficrt_field  = bodyrt_attr_uint  = offerid
}

2.生成索引,并开启searchd服务

# 生成项目索引
sphinx/bin/indexer.exe --config sphinx.conf --all# 开启服务 &表示后台开启,不用保持窗口执行状态
sphinx/bin/searchd.exe &

3.程序实现

sphinx查询返回的结果并不是我们需要的显示结果,所以还需要对结果进行处理,从而获取到我们需要的结果。

默认sphinx返回的数据中包含id信息是和数据记录的信息是相关的,所以我们需要通过id到数据库中查询相关信息。

public function test()
{$s = new \SphinxClient;$s->setServer("localhost", 9312);// 作为数组返回$s->SetArrayResult(true);// 匹配格式  任意匹配$s->setMatchMode(SPH_MATCH_ANY);$s->setMaxQueryTime(3);// input()表示接收用户传过来的数据$result = $s->query(input('key'),'*');return json($result);
}

4.测试实现

访问url:
http://localhost/mypro/api/index/test?key=官方

返回结果:


D:\web\COD\api\application\api\controller\Index.php:22:
array (size=10)'error' => string '' (length=0)'warning' => string '' (length=0)'status' => int 0'fields' => array (size=10)0 => string 'name' (length=4)1 => string 'ename' (length=5)2 => string 'setmeal' (length=7)3 => string 'category' (length=8)4 => string 'country' (length=7)5 => string 'traffic' (length=7)6 => string 'os' (length=2)7 => string 'body' (length=4)8 => string 'inventory_title' (length=15)9 => string 'shop' (length=4)'attrs' => array (size=1)'offerid' => string '1' (length=1)'matches' => array (size=6)0 => array (size=3)'id' => string '36' (length=2)'weight' => int 4667'attrs' => array (size=1)...1 => array (size=3)'id' => string '19' (length=2)'weight' => int 2611'attrs' => array (size=1)...// 此处省略部分数据'total' => int 6'total_found' => int 6'time' => float 0'words' => array (size=2)'官' => array (size=2)'docs' => int 14'hits' => int 16'方' => array (size=2)'docs' => int 70'hits' => int 94

对结果进行处理

public function test()
{$s = new \SphinxClient;$s->setServer("localhost", 9312);// 作为数组返回$s->SetArrayResult(true);// 匹配格式  任意匹配$s->setMatchMode(SPH_MATCH_ANY);$s->setMaxQueryTime(3);// input()表示接收用户传过来的数据$result = $s->query(input('key'),'*');// 避免没有匹配记录时报错if(empty($result['matches'])) {return null;}$result = $result['matches'];// 返回数组中指定的id列, 返回结果为单列数组$result = array_column($result, 'id');$list = model('offer')->field('offerid, name, ename, setmeal, category, country, traffic, os, body, inventory_title, shop')->where(array('id' => array('in', $result)))->select();return json($list);
}

返回结果

[{offerid: 2332302,name: "【官方站】減震隱形增高鞋墊(安全有效~秒增高5公分~)",ename: "zenggaoxiedian",setmeal: "日韓超夯,氣墊隱形增高墊,輕鬆增高5公分,透氣減震,抗菌防臭,藝人最愛!【可拆分,自由裁剪,均碼35-44可用】【超殺998三雙】",category: "[{"id":6,"name":"其他"},{"id":7,"name":"商城"},{"id":8,"name":"家庭用品\n"}]",country: "[{"id":11,"name":"American Samoa"},{"id":1,"name":"Andorra"},{"id":8,"name":"Angola"},{"id":5,"name":"Anguilla"},{"id":10,"name":"Argentina"},{"id":7,"name":"Armenia"},{"id":12,"name":"Austria"}]",traffic: "[{"id":2,"name":"16+"},{"id":3,"name":"3G\/4G"},{"id":4,"name":"Adult"}]",os: "[{"id":1,"name":"3DS System Software"},{"id":2,"name":"Android"},{"id":13,"name":"BeOS"},{"id":16,"name":"CentOS"}]",body: "123123123",inventory_title: "隱形增高鞋墊B",shop: "[{"userid":77912776,"name":"myShop"}]"},{offerid: 3308032,name: "【官方站】電熱造型梳",ename: "zaoxingshu",setmeal: "長/短髮都適用!好梳好上手!亂翹髮尾一秒聽話!【人氣爆红款美髮救星】限時特價,加NT$300再得1件!!!",category: "[{"id":2,"name":"美容"},{"id":4,"name":"日用品"},{"id":6,"name":"其他"},{"id":8,"name":"家庭用品\n"}]",country: "[{"id":6,"name":"Albania"},{"id":4,"name":"Antigua And Barbuda"}]",traffic: "[{"id":3,"name":"3G\/4G"},{"id":5,"name":"Adult Content"},{"id":6,"name":"App Discovery Traffic"}]",os: "[{"id":3,"name":"Android with AOKP"},{"id":5,"name":"Android with Cyanogen Mod"},{"id":6,"name":"Android with LiquidSmooth"},{"id":7,"name":"Android with MIUI"}]",body: "123123123"inventory_title: "NOVA多功能卷髮棒B",shop: "[{"userid":77912776,"name":"myShop"}]"}
]

支持简体中文、繁体中文和英文的检索。

暂且实现如此。

参考连接:

  • PHP官方手册使用Sphinx介绍:

http://www.php.net/manual/zh/book.sphinx.php

  • sphinx安装:

https://blog.csdn.net/huang2017/article/details/69665057

https://blog.csdn.net/huang2017/article/details/69666154

  • 将sphinx服务添加到windows服务:

./searchd.exe --install -c sphinx.conf --servicename s

https://blog.csdn.net/design321/article/details/8895712

  • sphinx使用:

https://blog.csdn.net/u010837612/article/details/70827481

  • 中文支持(linux系统)

http://www.xuejiehome.com/blread-1283.html

  • 中文支持(windows系统)

http://www.phpernote.com/php-template-framework/284.html

  • 其他

https://my.oschina.net/guyson/blog/283576

转载于:https://www.cnblogs.com/zqunor/p/9045267.html

windows7使用Sphinx+PHP+MySQL详细介绍相关推荐

  1. php sphinx mysql_windows7使用Sphinx+PHP+MySQL详细介绍

    安装(Windows) 1.官方下载 Sphinx下载地址: 下载 2.解压并重命名 此处下载版本为3.0.3,将 sphinx 文件夹命名为sphinx 3.文件夹目录介绍 sphinx --api ...

  2. MySQL · 引擎介绍 · Sphinx源码剖析(三)

    在本节中我会介绍Sphinx在构建索引之前做的一些事情,主要是从mysql拉取数据保存,然后分词排序保存到内存等等一系列的操作.下面是几个相关指令 sql_query = \SELECT id, gr ...

  3. mysql为什么要压测_mysql集群压测的详细介绍

    本篇文章给大家带来的内容是关于mysql集群压测的详细介绍,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. mysql压测 mysql自带就有一个叫mysqlslap的压力测试工具,通 ...

  4. mysql查询哪天,Mysql日期查询的详细介绍

    查询当前日期 SELECT CURRENT_DATE(); SELECT CURDATE(); 查询当前日期和时间 SELECT NOW(); 查询今天的数据 SELECT * FROM `表名` W ...

  5. php mysql explain_Mysql分析-explain的详细介绍

    为什么要了解explain: 想了解select查询的内部情况,查询优化器是如何工作的,是否使用到了索引,explain可以做到. MySQL查询优化器是如何工作的 : MySQL查询优化器有几个目标 ...

  6. mysql drop库_Mysql 删除数据库drop database详细介绍

    Mysql 删除数据库drop database 方法-:使用cmd模式下载删除 mysql> drop database school; 此命名删除了school数据库.但如果school数据 ...

  7. mysql事务传播属性_Spring事务传播属性和隔离级别详细介绍

    1 事务的传播属性(Propagation) 1) REQUIRED ,这个是默认的属性 Support a current transaction, create a new one if none ...

  8. mysql如何设置多节点_详细介绍Mysql5.7从节点设置多线程主从复制的办法

    软件安装:装机软件必备包 SQL是Structured Query Language(结构化查询语言)的缩写.SQL是专为数据库而建立的操作命令集,是一种功能齐全的数据库语言.在使用它时,只需要发出& ...

  9. apache php mysql配置详解,详细介绍:Apache+PHP+MySQL配置攻略_php基础

    一.系统要求: 本系统在REDHAT7.2版本测试通过 二.服务器端软件要求: 1:到APACHE的网络站下载APACHE WEB SERVER http://www.apache.org/ 2. 到 ...

最新文章

  1. Codeforces Beta Round #4 (Div. 2)【完结】
  2. SAP Spartacus里使用Observable访问Component数据
  3. 推荐一个可交互的 Attention 可视化工具!我的Transformer可解释性有救啦?
  4. Centos下lnmp编译安装详细过程
  5. python三方库之paramiko
  6. SpringMVC_04 拦截器 【拦截器的编程步骤】【session复习?】
  7. 网站解析 选择 主记录www 为主站 记录类型都选A 泛解析 选*就好了
  8. 【参数辨识】永磁同步电机的参数辨识
  9. C#之json序列化与反序列化
  10. beautifulsoup 基本语法
  11. RH413-GRUB加密
  12. CSS3 transform 旋转、偏移、缩放属性,CSS3 filter 滤镜,实现图像特效
  13. Visio软件方案UML选择
  14. 计算机网络(自顶向下方法)-应用层
  15. MineCraft mod开发环境搭建
  16. 视频直播技术之iOS端推流 1
  17. 3D建模教学 | 卡通石头高模制作技巧
  18. QQ2012如何恢复“合并会话窗口”为多个聊天窗口?
  19. 2019山东省赛记事
  20. Silverlight是什么

热门文章

  1. mysql 位操作支持
  2. 什么情况不能办理房产抵押贷款 房产抵押贷能贷多少?
  3. iOS之由身份证号返回性别
  4. 李开复:年轻人该比谁更拼命吗?
  5. 新书《编写可测试的JavaScript代码 》出版,感谢支持
  6. 简单的Excel导出(两种方式)
  7. 封装绑定以BaseDataList , ListControl 为基类的函数库
  8. @Async join
  9. Intent.ACTION_MAIN
  10. 阿里巴巴旗下平台口碑推出无人收银技术,改造便利店市场;重庆法院运用 AI 探索“智能判案”...