# tar zxvf sphinx-2.2.5-release.tar.gz # cd sphinx-2.2.5-release # ./configure --prefix=/usr/local/sphinx2 --with-mysql=/usr/local/mysql # make && make install 2,安装sphinx客户端,代码如下:
# cd api/libsphinxclient //sphinx-2.2.5-release目录下 # ./configure --prefix=/usr/local/sphinx2/libsphinxclient # make && make install 3,安装sphinx php扩展,代码如下:
# tar zxvf sphinx-1.3.1.tgz # cd sphinx-1.3.1 # phpize # ./configure --with-sphinx=/usr/local/sphinx2/libsphinxclient --with-php-config=/usr/bin/php-config # make && make install 4,安装scws,代码如下:
# tar xvjf scws-1.2.2.tar.bz2 # mkdir /usr/local/scws # cd scws-1.2.2 # ./configure --prefix=/usr/local/scws/ # make && make install 5,安装scws php扩展,代码如下:
# cd ./phpext/ # phpize # ./configure --with-php-config=/usr/bin/php-config # make && make install 三,配置sphinx,scws,php等.
1,创建测试表和数据,代码如下:
mysql> desc users; +----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+----------------+ | user_id | int(11) | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | | NULL | | +----------+-------------+------+-----+---------+----------------+ 2 rows in set (0.00 sec)
mysql> select * from users; +------------+------------+ | user_id | username | +------------+------------+ | 1311895262 | 张三 | | 1311895263 | tank张二 | | 1311895264 | tank张一 | | 1311895265 | tank张 | +------------+------------+ 4 rows in set (0.00 sec) mysql> desc orders; +--------------+-------------+------+-----+---------+----------------+ | 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 | | +--------------+-------------+------+-----+---------+----------------+ 5 rows in set (0.00 sec)
mysql> select * from orders; +----+------------+---------------------+----------------+--------------+ | id | user_id | create_time | product_name | summary | +----+------------+---------------------+----------------+--------------+ | 9 | 1311895262 | 2014-08-01 00:24:54 | tank is 坦克 | 技术总监 | | 10 | 1311895263 | 2014-08-01 00:24:54 | tank is 坦克 | 技术经理 | | 11 | 1311895264 | 2014-08-01 00:24:54 | tank is 坦克 | DNB经理 | | 12 | 1311895265 | 2014-08-01 00:24:54 | tank is 坦克 | 运维总监 | +----+------------+---------------------+----------------+--------------+ 4 rows in set (0.00 sec) 上面二张表,都是真实的mysql表.
2,配置sphinx.conf,加上以下内容,代码如下:
source myorder { type = mysql sql_host = localhost sql_user = root sql_pass = sql_db = test sql_query_pre = SET NAMES utf8 sql_query_pre = SET SESSION query_cache_type=OFF sql_query = SELECT a.id, a.user_id,b.username, UNIX_TIMESTAMP(a.create_time) AS create_time, a.product_name, a.summary FROM orders a left join users b on a.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 * FROM orders WHERE id=$id }
WARNING: key 'sql_query_info' was permanently removed from Sphinx configuration. Refer to documentation for details. WARNING: key 'charset_type' was permanently removed from Sphinx configuration. Refer to documentation for details. 3,安装scws词库,代码如下:
# tar xvjf scws-dict-chs-utf8.tar.bz2 -C /usr/local/scws/etc/
[root@localhost phpext]# mysql -h 127.0.0.1 -P 9306 Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 1 Server version: 2.2.5-id64-release (r4825)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql> select * from myorder where match('张'); +------+------------+------------+-------------+----------------+ | id | user_id | username | create_time | product_name | +------+------------+------------+-------------+----------------+ | 9 | 1311895262 | 张三 | 1406823894 | tank is 坦克 | | 10 | 1311895263 | tank张二 | 1406823894 | tank is 坦克 | | 11 | 1311895264 | tank张一 | 1406823894 | tank is 坦克 | | 12 | 1311895265 | tank张 | 1406823894 | tank is 坦克 | +------+------------+------------+-------------+----------------+ 4 rows in set (0.00 sec) 2,利用php 扩展,代码如下: