coreseek sphinx 创建表和索引

张映 发表于 2014-08-05

分类目录: mysql

标签:, , , , ,

前面说了,coreseek sphinx mmseg mysql等的安装,下面说一下怎么使用。

一,coreseek sphinx启动后,会多出一个端口,并且可以像mysql一样登录,但不是登录mysql

  1. [root@localhost tank]# mysql -h 127.0.0.1 -P 9306      //不是真的连接mysql,而连接了sphinx index  
  2. Welcome to the MySQL monitor.  Commands end with ; or \g.  
  3. Your MySQL connection id is 1  
  4. Server version: 1.11-id64-dev (r2540)  
  5.   
  6. Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.  
  7.   
  8. Oracle is a registered trademark of Oracle Corporation and/or its  
  9. affiliates. Other names may be trademarks of their respective  
  10. owners.  
  11.   
  12. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.  
  13.   
  14. mysql> select * from tank_test where match('坦克') ;   //这种写法,根原装的sphinx不一样  
  15. +------+--------+------------+------+  
  16. | id   | weight | user_id    | u_id |  
  17. +------+--------+------------+------+  
  18. |    3 |   2230 | 1311895260 |   62 |  
  19. |    5 |   2230 | 1311895260 |   33 |  
  20. |    4 |   1304 | 1311895262 |    0 |  
  21. |    6 |   1304 | 1311895262 |   34 |  
  22. +------+--------+------------+------+  
  23. 4 rows in set (0.00 sec)  
  24.   
  25. mysql> show META;     //上次检索的信息  
  26. +---------------+-------+  
  27. | Variable_name | Value |  
  28. +---------------+-------+  
  29. | total         | 3     |  
  30. | total_found   | 3     |  
  31. | time          | 0.000 |  
  32. | keyword[0]    | test  |  
  33. | docs[0]       | 3     |  
  34. | hits[0]       | 5     |  
  35. +---------------+-------+  
  36. 6 rows in set (0.00 sec)  
  37.   
  38. mysql> show tables;    //这里的表其实不是真表,也不是create table创建出来的,是sphinx索引  
  39. +--------------+-------------+  
  40. | Index        | Type        |  
  41. +--------------+-------------+  
  42. | dist1        | distributed |  
  43. | myorder      | local       |  
  44. | rt           | rt          |  
  45. | tank_test    | rt          |  
  46. | test1        | local       |  
  47. | test1stemmed | local       |  
  48. +--------------+-------------+  
  49. 6 rows in set (0.00 sec)  

二,创建sphinx索引

1,修改/usr/local/sphinx/etc/sphinx.conf

  1. # vim /usr/local/sphinx/etc/sphinx.conf   //添加以下内容  
  2.   
  3. index tank_test  
  4. {  
  5.  type            = rt  
  6.  path            = /usr/local/sphinx/var/data/rt  
  7.  charset_dictpath     = /usr/local/mmseg3/etc/  
  8.  charset_type         = zh_cn.utf-8  
  9.  ngram_len            = 0  
  10.  rt_field        = name  
  11.  rt_field        = title  
  12.  rt_field        = sub_title  
  13.  rt_attr_uint        = user_id  
  14.  rt_attr_uint        = uid  
  15. }  

在这里要注意,rt_field是检索字段,rt_attr_uint是返回字段

2,重启sphinx

  1. # pkill -9 searchd  
  2.   
  3. # /usr/local/sphinx/bin/indexer --config /usr/local/sphinx/etc/sphinx.conf --all  
  4. # /usr/local/sphinx/bin/searchd --config /usr/local/sphinx/etc/sphinx.conf  

3,插入数据,并查看

  1. mysql> show tables;  
  2. +--------------+-------------+  
  3. | Index        | Type        |  
  4. +--------------+-------------+  
  5. | dist1        | distributed |  
  6. | rt           | rt          |  
  7. | tank_test    | rt          |      //新增加的索引  
  8. | test1        | local       |  
  9. | test1stemmed | local       |  
  10. +--------------+-------------+  
  11. 5 rows in set (0.00 sec)  
  12.   
  13. mysql> desc tank_test;  
  14. +-----------+---------+  
  15. | Field     | Type    |  
  16. +-----------+---------+  
  17. | id        | bigint  |  
  18. | name      | field   |  
  19. | title     | field   |  
  20. | sub_title | field   |  
  21. | user_id   | integer |  
  22. | u_id      | integer |  
  23. +-----------+---------+  
  24. 6 rows in set (0.00 sec)  
  25.   
  26. mysql> insert into tank_test values (3,'坦克','tank is 坦克','技术总监',1311895260,33);  
  27.   
  28. mysql> insert into tank_test values (4,'tank张','tank is 坦克','技术总监',1311895262,34);  
  29.   
  30. mysql> select * from tank_test where match('坦克');    //匹配搜索的字段是rt_field  
  31. +------+--------+------------+------+  
  32. | id   | weight | user_id    | u_id |                 //返回的字段是rt_attr_uint  
  33. +------+--------+------------+------+  
  34. |    3 |   2230 | 1311895260 |   33 |  
  35. |    4 |   1304 | 1311895262 |   34 |  
  36. +------+--------+------------+------+  
  37. 2 rows in set (0.00 sec)  

id和weight是系统自带的返回字段

到这儿索引就创建好了,show tables的时候是可以看新建的tank_test,用phpmyadmin或者其他mysql数据库连接工具根本看不到,原因是他根本不是真实的表。sphinx到底能不能用真实的表呢?

三,创建表,并添加索引

1,创建真实的表,插入数据

  1. CREATE TABLE IF NOT EXISTS `orders` (  
  2.  `id` int(11) NOT NULL AUTO_INCREMENT,  
  3.  `user_id` int(11) NOT NULL ,  
  4.  `username` varchar(20) NOT NULL,  
  5.  `create_time` datetime NOT NULL,  
  6.  `product_name` varchar(20) NOT NULL,  
  7.  `summary` text NOT NULL,  
  8.  PRIMARY KEY (`id`)  
  9. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;  
  10.   
  11. INSERT INTO  `orders` (`user_id` ,`username` ,`create_time` ,`product_name` ,`summary`) VALUES  
  12. ('1311895262','张三','2014-08-01 00:24:54','tank is 坦克','技术总监'),  
  13. ('1311895263','tank张二','2014-08-01 00:24:54','tank is 坦克','技术经理'),  
  14. ('1311895264','tank张一','2014-08-01 00:24:54','tank is 坦克','DNB经理'),  
  15. ('1311895265','tank张','2014-08-01 00:24:54','tank is 坦克','运维总监');  

在这里要注意,是连接mysql的3306端口,不是连接coreseek sphinx的9306

2,修改/usr/local/sphinx/etc/sphinx.conf,添加以下内容

  1. source order  
  2. {  
  3.  type            = mysql  
  4.  sql_host        = localhost  
  5.  sql_user        = root  
  6.  sql_pass        =  
  7.  sql_db            = test  
  8.  sql_query_pre        = SET NAMES utf8  
  9.  sql_query        = \  
  10.  SELECT id, user_id, username, UNIX_TIMESTAMP(create_time) AS create_time, product_name, summary  \  
  11.  FROM orders  
  12.  sql_attr_uint        = user_id  
  13.  sql_attr_timestamp    = create_time  
  14.  sql_ranged_throttle    = 0  
  15.  sql_query_info    = SELECT * FROM orders WHERE id=$id  
  16. }  
  17.   
  18. index myorder  
  19. {  
  20.  source            = order  
  21.  path            = /usr/local/sphinx/var/data/myorder  
  22.  docinfo        = extern  
  23.  mlock            = 0  
  24.  morphology        = none  
  25.  min_word_len        = 1  
  26.  charset_dictpath    = /usr/local/mmseg3/etc/  
  27.  charset_type        = zh_cn.utf-8  
  28.  ngram_len            = 0  
  29.  html_strip        = 0  
  30. }  

3,重启sphinx

  1. # pkill -9 searchd  
  2.   
  3. # /usr/local/sphinx/bin/indexer --config /usr/local/sphinx/etc/sphinx.conf --all  
  4. # /usr/local/sphinx/bin/searchd --config /usr/local/sphinx/etc/sphinx.conf  

4,切换到9306,检索测试

  1. mysql> show tables;  
  2. +--------------+-------------+  
  3. | Index        | Type        |  
  4. +--------------+-------------+  
  5. | dist1        | distributed |  
  6. | myorder      | local       |  
  7. | rt           | rt          |  
  8. | tank_test    | rt          |  
  9. | test1        | local       |  
  10. | test1stemmed | local       |  
  11. +--------------+-------------+  
  12. 6 rows in set (0.00 sec)  
  13.   
  14. mysql> desc myorder;  
  15. +--------------+-----------+  
  16. | Field        | Type      |  
  17. +--------------+-----------+  
  18. | id           | bigint    |  
  19. | username     | field     |  
  20. | product_name | field     |  
  21. | summary      | field     |  
  22. | user_id      | integer   |  
  23. | create_time  | timestamp |  
  24. +--------------+-----------+  
  25. 6 rows in set (0.00 sec)  
  26.   
  27. mysql> select * from myorder where match('坦克');  
  28. +------+--------+------------+-------------+  
  29. | id   | weight | user_id    | create_time |  
  30. +------+--------+------------+-------------+  
  31. |    5 |   1304 | 1311895262 |  1407081600 |  
  32. |    6 |   1304 | 1311895263 |  1406823894 |  
  33. |    7 |   1304 | 1311895264 |  1406823894 |  
  34. |    8 |   1304 | 1311895265 |  1406823894 |  
  35. +------+--------+------------+-------------+  
  36. 4 rows in set (0.00 sec)  


转载请注明
作者:海底苍鹰
地址:http://blog.51yip.com/mysql/1643.html