前言
年轻的时候总以为很多app或者网站的搜索功能是基于cache+sql的模式进行查询的,也未曾想过数据是亿万级别,用户也是亿万级别时候,cache和sql的入门级模式是否能应对。答案是肯定不能hold住的,现在年长了些,随着项目的发展有幸接触到相关解决方案,所以想记下来,备忘。
那么老规则本文主要解决三个问题:
1.如何解决与设计数据和用户都是亿万级别的搜索的思路。
2.sphinx的简介与特性
3.sphinx的安装与运行
4.sphinx在亿级项目中的使用场景
正文开始
1.如何解决与设计数据和用户都是亿万级别的搜索的思路。
首先当数据量和用户基数很大时候,意味着三个问题需要解决:
1.查询的次数会很多并且需要快速返回;
2.查询并发数会很高,如何正确的分流分压;
3.数据的增长会很快,这部分增长的数据如何有效的处理才能实时搜索到;
MySQL自身的全文索引搜索慢,定制化程度低,自然无法满足解决上述问题,那么就需要更高性能的自定义的搜索,sphinx出现了,它提供了针对上述三大问题都有相应的解决方案。sphinx是以以俄国全文检索引擎,提供了高速、低空间占用、高结果相关度的全文搜索功能。主要方式是提供符合条件的数据源给sphinx,sphinx生成索引,依赖索引对外提供服务。更重要的是sphinx内置mysql数据库数据源的支持,使用起来非常简单,和使用mysql很大程度相似。
2.sphinx的简介与特性
我的理解中的sphinx
1.sphinx的机制两部分构成:生成索引+search索引
2.sphinx索引类型:普通索引+rt实时索引+分布式索引
特性(最新版sphinx性能某些方面更高于下面描述)
1.高速的建立索引(在当代CPU上,峰值性能可达到10 MB/秒);
2.高性能的搜索(在2 – 4GB 的文本数据上,平均每次检索响应时间小于0.1秒);
3.可处理海量数据(目前已知可以处理超过100 GB的文本数据, 在单一CPU的系统上可
处理100 M 文档);
4.提供了优秀的相关度算法,基于短语相似度和统计(BM25)的复合Ranking方法;
5.支持分布式搜索;
6.可作为MySQL的存储引擎提供搜索服务;
7.支持布尔、短语、词语相似度等多种检索模式;
8.文档支持多个全文检索字段(最大不超过32个);
9.文档支持多个额外的属性信息(例如:分组信息,时间戳等);
10.支持单一字节编码和UTF-8编码;
11.原生的MySQL支持(同时支持MyISAM 和InnoDB );
12.原生的PostgreSQL 支持.
反正就是很牛逼就是了。
3.sphinx的安装与运行(此部分转载的)
1.需要安装的软件
coreseek的mmseg包
mysql安装包
sphinx-0.9.8版
sphinx中文分词补丁1
sphinx中文分词补丁2
2.安装libmmseg
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
有问题尝试执行下面命令
- 1
- 2
- 3
- 1
- 2
- 3
3.重新编译mysql
安装sphinx之前先装两个补丁。
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
4.安装sphinx
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 1
- 2
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 1
- 2
- 3
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
5.配置sphinx
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
6.索引建立
装好sphinx后在sphinx的目录中有三个目录 分别为bin etc var
bin中 存有sphinx用到的一些执行文件 包括 indexer 索引建立 search 查询工具 searchd 查询服务器。备注:最新版已经没有search 查询工具了
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
7.查询服务器
/usr/local/sphinx/bin/searchd –config /usr/local/sphinx/etc/sphinx.conf 为开启
/usr/local/sphinx/bin/searchd –config /usr/local/sphinx/etc/sphinx.conf –stop 为关闭
sphinx的查询 可以大致分为三种
7.1 数据库引擎中的查询
7.2 通过search工具查询(最新版已不提供这个工具)
/usr/local/sphinx/bin/search --config
/usr/local/sphinx/etc/sphinx.conf test
7.3 通过php的接口查询 详见sphinxapi.php
8.创建sphinx启动脚本与配置
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
4.sphinx在亿级项目中的使用场景
不管是网站还是app很多产品的设计思路和产品功能多多少少都有相似之处,那么这边主要讲以下几个场景
描述、话题的搜索
主要的实现思路是全量索引+增量索引方式,可设定时任务定点跑索引
用户昵称的搜索
主要是实现思路是实时索引+分布式索引的方式,用户由于过多,故使用实时索引的方法进行增加,旧数据通过跑脚本重新读取后再写入。
搜索框联想词的提示
主要实现思路是分布式索引的方式,自动联想其他人曾经输入过的词语。
tip: morphology = stem_en会启用英文单词的提取。搜索英文时候就不会一个一个字母搜了,会提高sphinx搜索英文单词的时候的效率。