安装(Windows)

1.官方下载

Sphinx下载地址: 下载

2.解压并重命名

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

3.文件夹目录介绍

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

4.设置配置文件

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

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

(2)设置配置项

主要是以下为配置函数:

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

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

  • source src1throttled : src1{}

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

  • index dist1{}

  • indexer{}

  • common{}

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

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

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

6.生成索引文件

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

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

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

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

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

PHP开启sphinx扩展

1.下载php_sphinx扩展: 前往

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

  1. Architecture ==》x86/x64
  2. PHP Extension Build ==》NTS/NS

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

2.修改php.ini配置文件

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

修改后重启apache服务

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

  1. sphinx
  2. sphinx support enabled
  3. Version 1.3.2
  4. Revision $Revision$

代码实现

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

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

2.PHP代码实现

一般实现

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

thinkphp5使用介绍

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

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

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

3.url访问:

http://localhost/mypro/api/index/test?key=test

4.输出数据

  1. D:\web\COD\api\application\api\controller\Index.php:21:
  2. array (size=10)
  3. 'error' => string '' (length=0)
  4. 'warning' => string '' (length=0)
  5. 'status' => int 0
  6. 'fields' =>
  7. // 查询显示的字段名
  8. array (size=2)
  9. 0 => string 'title' (length=5)
  10. 1 => string 'content' (length=7)
  11. 'attrs' =>
  12. array (size=2)
  13. 'group_id' => string '1' (length=1)
  14. 'date_added' => string '2' (length=1)
  15. 'matches' =>
  16. // 匹配的结果,返回匹配记录的id和权重(权重越大,匹配条件越多)
  17. array (size=3)
  18. 0 =>
  19. array (size=3)
  20. 'id' => string '1' (length=1)
  21. 'weight' => int 2421
  22. 'attrs' =>
  23. array (size=2)
  24. ...
  25. 1 =>
  26. array (size=3)
  27. 'id' => string '2' (length=1)
  28. 'weight' => int 2421
  29. 'attrs' =>
  30. array (size=2)
  31. ...
  32. 2 =>
  33. array (size=3)
  34. 'id' => string '4' (length=1)
  35. 'weight' => int 1442
  36. 'attrs' =>
  37. array (size=2)
  38. ...
  39. 'total' => int 3
  40. 'total_found' => int 3
  41. 'time' => float 0
  42. 'words' =>
  43. array (size=1)
  44. 'test' =>
  45. array (size=2)
  46. 'docs' => int 6
  47. 'hits' => int 10

在ThinkPHP5项目中应用

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

  1. source src1{
  2. # 省略其他配置
  3. sql_user = root
  4. sql_pass = 123123
  5. sql_db = shopMall
  6. sql_query = \
  7. SELECT id,offerid, name, ename, setmeal, category, country, traffic, os, body, inventory_title, shop \
  8. FROM i_offer
  9. sql_attr_uint = offerid
  10. # 省略其他配置
  11. }
  1. index rt
  2. {
  3. type = rt
  4. path = D:\Service\sphinx\data\rt
  5. rt_field = name
  6. rt_field = ename
  7. rt_field = setmeal
  8. rt_field = category
  9. rt_field = country
  10. rt_field = traffic
  11. rt_field = body
  12. rt_attr_uint = offerid
  13. }

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

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

3.程序实现

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

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

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

4.测试实现

访问url:

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

返回结果:


  1. D:\web\COD\api\application\api\controller\Index.php:22:
  2. array (size=10)
  3. 'error' => string '' (length=0)
  4. 'warning' => string '' (length=0)
  5. 'status' => int 0
  6. 'fields' =>
  7. array (size=10)
  8. 0 => string 'name' (length=4)
  9. 1 => string 'ename' (length=5)
  10. 2 => string 'setmeal' (length=7)
  11. 3 => string 'category' (length=8)
  12. 4 => string 'country' (length=7)
  13. 5 => string 'traffic' (length=7)
  14. 6 => string 'os' (length=2)
  15. 7 => string 'body' (length=4)
  16. 8 => string 'inventory_title' (length=15)
  17. 9 => string 'shop' (length=4)
  18. 'attrs' =>
  19. array (size=1)
  20. 'offerid' => string '1' (length=1)
  21. 'matches' =>
  22. array (size=6)
  23. 0 =>
  24. array (size=3)
  25. 'id' => string '36' (length=2)
  26. 'weight' => int 4667
  27. 'attrs' =>
  28. array (size=1)
  29. ...
  30. 1 =>
  31. array (size=3)
  32. 'id' => string '19' (length=2)
  33. 'weight' => int 2611
  34. 'attrs' =>
  35. array (size=1)
  36. ...
  37. // 此处省略部分数据
  38. 'total' => int 6
  39. 'total_found' => int 6
  40. 'time' => float 0
  41. 'words' =>
  42. array (size=2)
  43. '官' =>
  44. array (size=2)
  45. 'docs' => int 14
  46. 'hits' => int 16
  47. '方' =>
  48. array (size=2)
  49. 'docs' => int 70
  50. 'hits' => int 94

对结果进行处理

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

返回结果

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

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

暂且实现如此。

参考连接:

  • 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

windows7使用Sphinx+PHP+MySQL详细介绍的更多相关文章

  1. Spotlight on Mysql详细介绍

    Spotlight on Mysql详细介绍   by:授客 QQ:1033553122     1. 版本 2. 使用介绍 1) 主页 会话面板 MySQL面板 INNODB面板 存储面板 主机面板 ...

  2. windows7配置Nginx+php+mysql的详细教程

    windows7配置Nginx+php+mysql的详细教程 作者:Vincent.李 字体:[增加 减小] 类型:转载 时间:2016-09-04我要评论 这篇文章主要介绍了windows7配置Ng ...

  3. 推荐收藏 —— MySQL视图详细介绍

    前言:  在MySQL中,视图可能是我们最常用的数据库对象之一了.那么你知道视图和表的区别吗?你知道创建及使用视图要注意哪些点吗?可能很多人对视图只是一知半解,想详细了解视图的同学看过来哟,本篇文章会 ...

  4. 多图文,详细介绍mysql各个集群方案

    目录 多图文,详细介绍mysql各个集群方案 一,mysql原厂出品 二,mysql第三方优化 三,依托硬件配合 四,其它 多图文,详细介绍mysql各个集群方案 集群的好处 高可用性:故障检测及迁移 ...

  5. MySQL视图详细介绍

    前言: 在MySQL中,视图可能是我们最常用的数据库对象之一了.那么你知道视图和表的区别吗?你知道创建及使用视图要注意哪些点吗?可能很多人对视图只是一知半解,想详细了解视图的同学看过来哟,本篇文章会详 ...

  6. MySQL 分区介绍总结

    200 ? "200px" : this.width)!important;} --> 介绍 分区是指根据一定的规则将一个大表分解成多个更小的部分,这里的规则一般就是利用分区 ...

  7. sphinx配置文件sphinx.conf参数详细说明

    sphinx配置文件sphinx.conf参数详细说明 sphinx.conf各个参数详细说明 # # Sphinx configuration file sample # # WARNING! Wh ...

  8. WDCP是什么 关于WDCP的详细介绍

    WDCP是WDlinux Control Panel的简称,是一套用PHP开发的Linux服务器管理系统以及虚拟主机管理系统,,旨在易于使用Linux系统做为我们的网站服务器,以及平时对Linux服务 ...

  9. mysql数据类型介绍

    一.int.bigint.smallint 和 tinyint的区别详细介绍 bigint 从 -2^63 (-9223372036854775808) 到 2^63-1 (9223372036854 ...

随机推荐

  1. 重构一段基于原生JavaScript的表格绘制代码

    为了在CardSimulate项目中方便的显示技能和效果列表,决定重构以前编写的一段JavaScript代码——att表格绘制库,这段代码的作用是将特定的JavaScript数据对象转化为表格,支持精 ...

  2. Attack Top Chunk之 bcloud

    前言 这是 bctf 2016 的题,链接 https://github.com/ctfs/write-ups-2016/tree/master/bctf-2016/exploit/bcloud-20 ...

  3. h5调用微信分享

    https://blog.csdn.net/qq_39562787/article/details/79217386

  4. 第四章 数据库和SQL 4-3 数据的更新(UPDATE语句的使用方法)

    一.UPDATE语句的基本语法. 二.指定条件的UPDATE语句(搜索型UPDATE) 三.使用NULL进行更新 NULL清空:使用UPDATE可以将列更新为NULL,俗称NULL清空. 四.多列更新 ...

  5. MYSQL 5.7 sqlmode 行为

    最近碰到了sql_mode 的一些问题,故进行了研究,根据实际情况研究其行为. sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ER ...

  6. linux smem 查看各进程使用memory情况

    SMEM(8) SMEM(8) NAME smem - Report memory usage with shared memory divided proportionally. SYNOPSIS ...

  7. 利用jTessBoxEditor工具进行Tesseract3.02.02样本训练,提高验证码识别率

    1.背景 前文已经简要介绍tesseract ocr引擎的安装及基本使用,其中提到使用-l eng参数来限定语言库,可以提高识别准确率及识别效率. 本文将针对某个网站的验证码进行样本训练,形成自己的语 ...

  8. 将亚马逊aws的ec2服务器的登陆方式改为密码登陆

    1.在用密钥登陆ec2后,为root用户创建密码: sudo passwd root 系统会让你输入两次密码 2.切换为root用户,并且编辑sshd_config文件,PasswordAuthent ...

  9. python_web应用雏型

    python_web应用雏型 Web应用程序顾名思义,就是一种可以通过Web访问的应用程序, Web应用的最大特点是用户只需要有网络和浏览器,不需要再安装其他软件就可顺利通过web访问到程序. WEB ...

  10. tomcat7换端口号调试

    1.C:\tomcat\conf\server.xml中修改端口号 2.C:\tomcat\bin\startup.bat批处理文件启动tomcat 3.用ctrl+c结束批处理文件 4.调试结束