热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

coreseeksphinx创建表和索引

前面说了,coreseeksphinxmmsegmysql等的安装,下面说一下怎么使用。一,coreseeksphinx启动后,会多出一个端口,并且可以像mysql一样登录,但不是登录mysql[root@localhosttank]#mysql-h127.0.0.1-P9306不是真的连接mysql,而连接了sphinxin

前面说了,coreseek sphinx mmseg mysql等的安装,下面说一下怎么使用。 一,coreseek sphinx启动后,会多出一个端口,并且可以像mysql一样登录,但不是登录mysql [root@localhost tank]# mysql -h 127.0.0.1 -P 9306 //不是真的连接mysql,而连接了sphinx in

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

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

[root@localhost tank]# mysql -h 127.0.0.1 -P 9306      //不是真的连接mysql,而连接了sphinx index
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 1.11-id64-dev (r2540)
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 tank_test where match('坦克') ;   //这种写法,根原装的sphinx不一样
+------+--------+------------+------+
| id   | weight | user_id    | u_id |
+------+--------+------------+------+
|    3 |   2230 | 1311895260 |   62 |
|    5 |   2230 | 1311895260 |   33 |
|    4 |   1304 | 1311895262 |    0 |
|    6 |   1304 | 1311895262 |   34 |
+------+--------+------------+------+
4 rows in set (0.00 sec)
mysql> show META;     //上次检索的信息
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| total         | 3     |
| total_found   | 3     |
| time          | 0.000 |
| keyword[0]    | test  |
| docs[0]       | 3     |
| hits[0]       | 5     |
+---------------+-------+
6 rows in set (0.00 sec)
mysql> show tables;    //这里的表其实不是真表,也不是create table创建出来的,是sphinx索引
+--------------+-------------+
| Index        | Type        |
+--------------+-------------+
| dist1        | distributed |
| myorder      | local       |
| rt           | rt          |
| tank_test    | rt          |
| test1        | local       |
| test1stemmed | local       |
+--------------+-------------+
6 rows in set (0.00 sec)

二,创建sphinx索引

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

# vim /usr/local/sphinx/etc/sphinx.conf   //添加以下内容
index tank_test
{
 type            = rt
 path            = /usr/local/sphinx/var/data/rt
 charset_dictpath     = /usr/local/mmseg3/etc/
 charset_type         = zh_cn.utf-8
 ngram_len            = 0
 rt_field        = name
 rt_field        = title
 rt_field        = sub_title
 rt_attr_uint        = user_id
 rt_attr_uint        = uid
}

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

2,重启sphinx

# pkill -9 searchd
# /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

3,插入数据,并查看

mysql> show tables;
+--------------+-------------+
| Index        | Type        |
+--------------+-------------+
| dist1        | distributed |
| rt           | rt          |
| tank_test    | rt          |      //新增加的索引
| test1        | local       |
| test1stemmed | local       |
+--------------+-------------+
5 rows in set (0.00 sec)
mysql> desc tank_test;
+-----------+---------+
| Field     | Type    |
+-----------+---------+
| id        | bigint  |
| name      | field   |
| title     | field   |
| sub_title | field   |
| user_id   | integer |
| u_id      | integer |
+-----------+---------+
6 rows in set (0.00 sec)
mysql> insert into tank_test values (3,'坦克','tank is 坦克','技术总监',1311895260,33);
mysql> insert into tank_test values (4,'tank张','tank is 坦克','技术总监',1311895262,34);
mysql> select * from tank_test where match('坦克');    //匹配搜索的字段是rt_field
+------+--------+------------+------+
| id   | weight | user_id    | u_id |                 //返回的字段是rt_attr_uint
+------+--------+------------+------+
|    3 |   2230 | 1311895260 |   33 |
|    4 |   1304 | 1311895262 |   34 |
+------+--------+------------+------+
2 rows in set (0.00 sec)

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

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

三,创建表,并添加索引

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

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

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

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

source order
{
 type            = mysql
 sql_host        = localhost
 sql_user        = root
 sql_pass        =
 sql_db            = test
 sql_query_pre        = SET NAMES utf8
 sql_query        = \
 SELECT id, user_id, username, UNIX_TIMESTAMP(create_time) AS create_time, product_name, summary  \
 FROM orders
 sql_attr_uint        = user_id
 sql_attr_timestamp    = create_time
 sql_ranged_throttle    = 0
 sql_query_info    = SELECT * FROM orders WHERE id=$id
}
index myorder
{
 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
}

3,重启sphinx

# pkill -9 searchd
# /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

4,切换到9306,检索测试

mysql> show tables;
+--------------+-------------+
| Index        | Type        |
+--------------+-------------+
| dist1        | distributed |
| myorder      | local       |
| rt           | rt          |
| tank_test    | rt          |
| test1        | local       |
| test1stemmed | local       |
+--------------+-------------+
6 rows in set (0.00 sec)
mysql> desc myorder;
+--------------+-----------+
| Field        | Type      |
+--------------+-----------+
| id           | bigint    |
| username     | field     |
| product_name | field     |
| summary      | field     |
| user_id      | integer   |
| create_time  | timestamp |
+--------------+-----------+
6 rows in set (0.00 sec)
mysql> select * from myorder where match('坦克');
+------+--------+------------+-------------+
| id   | weight | user_id    | create_time |
+------+--------+------------+-------------+
|    5 |   1304 | 1311895262 |  1407081600 |
|    6 |   1304 | 1311895263 |  1406823894 |
|    7 |   1304 | 1311895264 |  1406823894 |
|    8 |   1304 | 1311895265 |  1406823894 |
+------+--------+------------+-------------+
4 rows in set (0.00 sec)
前面说了,coreseek sphinx mmseg mysql等的安装,下面说一下怎么使用。 一,coreseek sphinx启动后,会多出一个端口,并且可以像mysql一样登录,但不是登录mysql [root@localhost tank]# mysql -h 127.0.0.1 -P 9306 //不是真的连接mysql,而连接了sphinx index Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 1.11-id64-dev (r2540) Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or [...]
推荐阅读
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • Oracle Database 10g许可授予信息及高级功能详解
    本文介绍了Oracle Database 10g许可授予信息及其中的高级功能,包括数据库优化数据包、SQL访问指导、SQL优化指导、SQL优化集和重组对象。同时提供了详细说明,指导用户在Oracle Database 10g中如何使用这些功能。 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 本文由编程笔记小编整理,介绍了PHP中的MySQL函数库及其常用函数,包括mysql_connect、mysql_error、mysql_select_db、mysql_query、mysql_affected_row、mysql_close等。希望对读者有一定的参考价值。 ... [详细]
  • 本文介绍了在Mac上搭建php环境后无法使用localhost连接mysql的问题,并通过将localhost替换为127.0.0.1或本机IP解决了该问题。文章解释了localhost和127.0.0.1的区别,指出了使用socket方式连接导致连接失败的原因。此外,还提供了相关链接供读者深入了解。 ... [详细]
  • 解决VS写C#项目导入MySQL数据源报错“You have a usable connection already”问题的正确方法
    本文介绍了在VS写C#项目导入MySQL数据源时出现报错“You have a usable connection already”的问题,并给出了正确的解决方法。详细描述了问题的出现情况和报错信息,并提供了解决该问题的步骤和注意事项。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • 如何在php中将mysql查询结果赋值给变量
    本文介绍了在php中将mysql查询结果赋值给变量的方法,包括从mysql表中查询count(学号)并赋值给一个变量,以及如何将sql中查询单条结果赋值给php页面的一个变量。同时还讨论了php调用mysql查询结果到变量的方法,并提供了示例代码。 ... [详细]
  • 本文介绍了在MacOS系统上安装MySQL的步骤,并详细说明了如何设置MySQL服务的开机启动和如何修改MySQL的密码。通过下载MySQL的macos版本并按照提示一步一步安装,在系统偏好设置中可以找到MySQL的图标进行设置。同时,还介绍了通过终端命令来修改MySQL的密码的具体操作步骤。 ... [详细]
  • Python项目实战10.2:MySQL读写分离性能优化
    本文介绍了在Python项目实战中进行MySQL读写分离的性能优化,包括主从同步的配置和Django实现,以及在两台centos 7系统上安装和配置MySQL的步骤。同时还介绍了创建从数据库的用户和权限的方法。摘要长度为176字。 ... [详细]
  • 本文详细介绍了SQL日志收缩的方法,包括截断日志和删除不需要的旧日志记录。通过备份日志和使用DBCC SHRINKFILE命令可以实现日志的收缩。同时,还介绍了截断日志的原理和注意事项,包括不能截断事务日志的活动部分和MinLSN的确定方法。通过本文的方法,可以有效减小逻辑日志的大小,提高数据库的性能。 ... [详细]
  • Java实战之电影在线观看系统的实现
    本文介绍了Java实战之电影在线观看系统的实现过程。首先对项目进行了简述,然后展示了系统的效果图。接着介绍了系统的核心代码,包括后台用户管理控制器、电影管理控制器和前台电影控制器。最后对项目的环境配置和使用的技术进行了说明,包括JSP、Spring、SpringMVC、MyBatis、html、css、JavaScript、JQuery、Ajax、layui和maven等。 ... [详细]
  • 在使用dedecms过程中,添加自定义字段变量很有用,但删除并不容易。本文介绍了两种常用的删除方法:执行SQL语句和手动SQL删除。 ... [详细]
author-avatar
两枚小瓶盖
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有