我们在做互联网的项目中,经常碰到这样一种情况,发现随着网站的逐渐发展壮大,产品也越来越多,这个时候在产品搜索方面的问题也逐渐暴漏出来,搜索服务越来越慢。碰到这种情况,我们往往在数据库方面采取一些措施,进行索引优化、分库分表等,但是你会发现,这种方式发挥的作用非常有限,远远达不到我们的要求,那么如何使我们的网站的搜索功能像淘宝一样飞起来呢?下面我们就介绍一种工具Solr,我们将会从what、where、how、why这几个方面介绍。
首先我们,来了解一下Solr是什么——What?
Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service的API接口。用户可以通过http请求,向搜索引擎服务器提交一定格式的XML文件,生成索引;也可以通过Http Get操作提出查找请求,并得到XML格式的返回结果。它的主要特性包括:高效、灵活的缓存功能,垂直搜索功能,高亮显示搜索结果,通过索引复制来提高可用性,提供一套强大Data Schema来定义字段,类型和设置文本分析,提供基于Web的管理界面等。
那么,既然这个工具这么好,我们到哪里下载呢——Where?
打开这个官方网站——http://lucene.apache.org/solr/downloads.html,我们会看到如下图的入口,这里面就是所有版本的Solr的镜像地址了,你可以下载任意版本的,截止到博客发布日期最新版本是Solr-5.3.1,作为一个程序员,你懂得,所以我下载的是5.3.1这个最新版本。
现在,让我们开始学习Solr吧——How!
首先,我们要了解自己的开发环境
我自己的开发环境是Window7、JDK1.8、Solr-5.3.1
PS:在这里要注意一下你下载的Solr的版本支持的JDK的最低要求是什么
第二步,我们把下载的Solr解压到安装目录
第三步,启动Solr
打开http://localhost:8983/solr/,如图
这样,Solr成功安装并启动起来了。
第三步,让我们运行一个Solr自带的实例吧!
Creating Solr home directory D:\solr-5.3.1\example\techproducts\solr
//创建项目的目录文件
Starting up Solr on port 8983 using command:
D:\solr-5.3.1\bin\solr.cmd start -p 8983 -s "D:\solr-5.3.1\example\techproducts\
solr"
//启动这个我们要创建的Solr项目的命令
Waiting up to 30 to see Solr running on port 8983
Setup new core instance directory:
D:\solr-5.3.1\example\techproducts\solr\techproducts
//创建项目核心实例目录
Creating new core 'techproducts' using command://下边这个命令就是创建项目的核心实例的http命令
http://localhost:8983/solr/admin/cores?action=CREATE&name=techproducts&instanceD
ir=techproducts
{
"responseHeader":{
"status":0,//创建成功
"QTime":2793},
"core":"techproducts"}
//以下是添加索引文件信息
Indexing tech product example docs from D:\solr-5.3.1\example\exampledocs
SimplePostTool version 5.0.0
Posting files to [base] url http://localhost:8983/solr/techproducts/update using
content-type application/xml...
POSTing file gb18030-example.xml to [base]
POSTing file hd.xml to [base]
POSTing file ipod_other.xml to [base]
POSTing file ipod_video.xml to [base]
POSTing file manufacturers.xml to [base]
POSTing file mem.xml to [base]
POSTing file money.xml to [base]
POSTing file monitor.xml to [base]
POSTing file monitor2.xml to [base]
POSTing file mp500.xml to [base]
POSTing file sd500.xml to [base]
POSTing file solr.xml to [base]
POSTing file utf8-example.xml to [base]
POSTing file vidcard.xml to [base]
14 files indexed.
COMMITting Solr index changes to http://localhost:8983/solr/techproducts/update.
..
Time spent: 0:00:00.807
Solr techproducts example launched successfully. Direct your Web browser to http
://localhost:8983/solr to visit the Solr Admin UI