sphinx实现联合查询几个例子
2019/10/10/17:32:11 阅读:1900 来源:谷歌SEO算法 标签:
1号店SEO
sphinx也就是一个像mysql数据库的工具了,我们可以在linux中使用sphinx来替换mysql了,下面小编整理了几个sphinx联合查询的语句,记得以前sphinx是不支持联合查询的,第一次接触sphinx,好像2010年初的时候,当时写过一篇关于sphinx安装的文章,sphinx mmseg mysql 中文分词,下面举例说明,sphinx的联合查询用法.
一,添加二张测试表和数据,代码如下:
1,users表和数据
- mysql>descusers;
- +----------+-------------+------+-----+---------+----------------+
- |Field|Type|Null|Key|Default|Extra|
- +----------+-------------+------+-----+---------+----------------+
- |user_id|int(11)|NO|PRI|NULL|auto_increment|
- |username|varchar(20)|NO||NULL||
- +----------+-------------+------+-----+---------+----------------+
- 2rowsinset(0.00sec)
- --phpfensi.com
- mysql>select*fromusers;
- +------------+------------+
- |user_id|username|
- +------------+------------+
- |1311895262|张三|
- |1311895263|tank张二|
- |1311895264|tank张一|
- |1311895265|tank张|
- +------------+------------+
- 4rowsinset(0.00sec)
2,orders表和数据
- mysql>descorders;
- +--------------+-------------+------+-----+---------+----------------+
- |Field|Type|Null|Key|Default|Extra|
- +--------------+-------------+------+-----+---------+----------------+
- |id|int(11)|NO|PRI|NULL|auto_increment|
- |user_id|int(11)|NO||NULL||
- |create_time|datetime|NO||NULL||
- |product_name|varchar(20)|NO||NULL||
- |summary|text|NO||NULL||
- +--------------+-------------+------+-----+---------+----------------+
- 5rowsinset(0.00sec)
- mysql>select*fromorders;
- +----+------------+---------------------+----------------+--------------+
- |id|user_id|create_time|product_name|summary|
- +----+------------+---------------------+----------------+--------------+
- |9|1311895262|2014-08-0100:24:54|tankis坦克|技术总监|
- |10|1311895263|2014-08-0100:24:54|tankis坦克|技术经理|
- |11|1311895264|2014-08-0100:24:54|tankis坦克|DNB经理|
- |12|1311895265|2014-08-0100:24:54|tankis坦克|运维总监|
- +----+------------+---------------------+----------------+--------------+
- 4rowsinset(0.00sec)
二,配置sphinx.conf,代码如下:
- sourceorder
- {
- type=mysql
- sql_host=localhost
- sql_user=root
- sql_pass=
- sql_db=test
- sql_query_pre=SETNAMESutf8
- sql_query=\
- SELECTa.id,a.user_id,b.username,UNIX_TIMESTAMP(a.create_time)AScreate_time,a.product_name,a.summary\
- FROMordersaleftjoinusersbona.user_id=b.user_id
- sql_attr_uint=user_id
- sql_field_string=username
- sql_field_string=product_name
- sql_attr_timestamp=create_time
- sql_ranged_throttle=0
- sql_query_info=SELECT*FROMordersWHEREid=$id
- }
- indexmyorder
- {
- source=order
- path=/usr/local/sphinx/var/data/myorder
- docinfo=extern
- mlock=0
- morphology=none
- min_word_len=1
- charset_dictpath=/usr/local/mmseg3/etc/
- charset_type=zh_cn.utf-8
- ngram_len=0
- html_strip=0
- }
注意:在这里a.user_id = b.user_id,等号二边一定要有空格,不然就会报错.
三,重启sphinx,代码如下:
- #pkillsearchd
- #/usr/local/sphinx/bin/indexer--config/usr/local/sphinx/etc/sphinx.conf--all
- #/usr/local/sphinx/bin/searchd--config/usr/local/sphinx/etc/sphinx.conf
四,测试sphinx,代码如下:
- [root@localhostetc]#mysql-h127.0.0.1-P9306//登录sphinx,9306端口,不是真实的mysql
- WelcometotheMySQLmonitor.Commandsendwith;or\g.
- YourMySQLconnectionidis1
- Serverversion:1.11-id64-dev(r2540)
- Copyright(c)2000,2013,Oracleand/oritsaffiliates.Allrightsreserved.
- OracleisaregisteredtrademarkofOracleCorporationand/orits
- affiliates.Othernamesmaybetrademarksoftheirrespective
- owners.
- Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement.
- mysql>select*frommyorderwherematch('张');
- +------+--------+------------+------------+-------------+----------------+
- |id|weight|user_id|username|create_time|product_name|
- +------+--------+------------+------------+-------------+----------------+
- |9|1304|1311895262|张三|1406823894|tankis坦克|
- |10|1304|1311895263|tank张二|1406823894|tankis坦克|
- |11|1304|1311895264|tank张一|1406823894|tankis坦克|
- |12|1304|1311895265|tank张|1406823894|tankis坦克|
- +------+--------+------------+------------+-------------+----------------+
- 4rowsinset(0.01sec)
- mysql>select*frommyorderwherematch('张三');
- +------+--------+------------+----------+-------------+----------------+
- |id|weight|user_id|username|create_time|product_name|
- +------+--------+------------+----------+-------------+----------------+
- |9|2500|1311895262|张三|1406823894|tankis坦克|
- +------+--------+------------+----------+-------------+----------------+
- 1rowinset(0.00sec)
热门评论