当前位置: 首页 > 工具软件 > PHP Sphinx > 使用案例 >

sphinx 分词搜索 php,php sphinx搜寻分词 windows7

卢勇
2023-12-01

本地开发环境

1.下载coreseek词库,下载地址:http://www.coreseek.cn/products-install/install_on_windows/coreseek3.2.14通用版php 5.4.3

?

2.下载好后,再环境变量里配置:打开cmd#->CD c:\usr\local\coreseek-3.2.14-win32#->SET PATH=%CD%\bin;%PATH%

?

3.在项目录目下添加一个sphinx.conf文件(名字随自己定),关联查询product与product_type表#MySQL数据源配置,详情请查看:http://www.coreseek.cn/products-install/mysql/#请先将var/test/documents.sql导入数据库,并配置好以下的MySQL用户密码数据库#源定义#sql_attr_uint#sql_attr_bool #sql_attr_bigint #sql_attr_timestamp 时间行#sql_attr_str2ordinal 字符串行#sql_attr_float 浮点型#sql_attr_multi source mysql{ type = mysql sql_host = 192.168.8.229 sql_user = feiyang sql_pass = 123456 sql_db = feiyang_prd sql_port = 3306 sql_query_pre = SET NAMES utf8 sql_query = SELECT SQL_NO_CACHE `product`.`ID` * CAST(1 AS SIGNED) + 0 AS `ID`, `product`.`MinPrice` AS `MinPrice`, `product`.`ProductTypeid` AS `product_type_id`, UNIX_TIMESTAMP(`product`.`ShowStartTime`) AS date_added , `product`.`WebTitle` AS `WebTitle`, `product`.`SubTitle` AS `SubTitle`, `product`.`AddressNames` AS `AddressNames`, `product_type`.`Title` AS `product_type_title` FROM `product` LEFT OUTER JOIN `product_type` ON `product_type`.`ID` = `product`.`ProductTypeid` where `product`.`ShowStatus` = 1 GROUP BY `product`.`ID` ORDER BY NULL #sql_query第一列ID需为整数 #WebTitle、SubTitle, ProductType表中title作为字符串/文本字段,被全文索引 sql_attr_uint = sphinx_internal_id #从SQL读取到的值必须为整数 sql_attr_uint = class_crc sql_attr_uint = Sort sql_attr_uint = ShowStatus sql_attr_float = MinPrice sql_attr_timestamp = date_added #从SQL读取到的值必须为整数,作为时间属性 sql_attr_str2ordinal = WebTitle_sort sql_attr_str2ordinal = SubTitle_sort sql_attr_str2ordinal = AddressNames_sort sql_attr_str2ordinal = StartCity_sort sql_attr_str2ordinal = ToTraffic_sort sql_attr_str2ordinal = BackTraffic_sort sql_attr_str2ordinal = Notes_sort sql_query_info_pre = SET NAMES utf8 #命令行查询时,设置正确的字符集 sql_query_info = SELECT * FROM product WHERE `ID` = (($id - 0) / 1) #命令行查询时,从数据库读取原始数据信息}#index定义index mysql{ source = mysql #对应的source名称 path = E:/projects/test_php/data/product #索引路径,请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/... docinfo = extern mlock = 0 morphology = none min_word_len = 1 ngram_len = 1 #1为搜索中文 html_strip = 0 #charset_dictpath = /usr/local/mmseg3/etc/ #BSD、Linux环境下设置,/符号结尾 charset_dictpath = D:/coreseek/etc/ #Windows环境下设置,/符号结尾,最好给出绝对路径,例如:C:/usr/local/coreseek/etc/... charset_type = utf-8 charset_table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F ngram_chars = U+3000..U+2FA1F}#全局index定义indexer{ mem_limit = 128M}#searchd服务定义searchd{ listen = 9312 read_timeout = 5 max_children = 30 max_matches = 1000 # seamless_rotate = 0 #windows下启动必须注释掉这行 preopen_indexes = 0 unlink_old = 1 pid_file = ../log/searchd_mysql.pid #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/... log = ../log/searchd_mysql.log #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/... query_log = ../log/query_mysql.log #请修改为实际使用的绝对路径,例如:/usr/local/coreseek/var/...}

?

4.生成索引,启动搜索cmd下面运行下面代码:#-> indexer --config config/sphinx.conf --all #生成索引#-> searchd --config config/sphinx.conf 启动搜索

?

?

5.php里的配置$cl = new SphinxClient ();$cl->SetServer ( '127.0.0.1', 9312);$cl->SetMatchMode ( SPH_MATCH_ANY ); //SPH_MATCH_ALL, 匹配所有查询词(默认模式); SPH_MATCH_ANY, 匹配查询词中的任意一个; SPH_MATCH_EXTENDED2, 支持特殊运算符查询 $cl->SetConnectTimeout ( 3 );$cl->SetArrayResult ( true );$weights = array('WebTitle'=>10, 'SubTitle'=>9, 'AddressNames'=>8, 'product_type_title'=>7);$cl->SetFieldWeights($weights);$cl->SetMaxQueryTime(10); //最大搜索时间$res = $cl->Query ( $_GET['keyword'], "*" ); //"*"全部索引, 在conf文件中,会配置多个索引文件// 从名称为index的sphinx索引查询“电影票”$sp->SetGroupBy('item_id',SPH_GROUP_ATTR,'s_order desc');$sp->SetFilter('city_id','1');$sp->SetFilter('cat_id',array(1));$sp->SetLimit(0,10,1000);$sp->AddQuery('电影票','index');$sp->ResetFilters();//重置筛选条件$sp->SetGroupBy('item_id', SPH_GROUPBY_ATTR, 's_order desc');$sp->setFilter('city_id', '2');$sp->setFilter('cat_id', array(2));$sp->setLimits(0, 20, 1000);$sp->AddQuery('温泉', 'index');$sp->ResetFilters();// 重置筛选条件$sp->ResetGroupBy();//重置分组$results = $sp->RunQuries();

?

相关文章

相关视频

 类似资料: