Solr使用入门指南
本文转自http://chuanliang2007.spaces.live.com/blog/cns!E5B7AB2851A4C9D2!499.entry?wa=wsignin1.0
由于搜索引擎功能在门户社区中对提高用户体验有着重在门户社区中涉及大量需要搜索引擎的功能需求,目前在实现搜索引擎的方案上有集中方案可供选择:
1. 基于Lucene自己进行封装实现站内搜索。工作量及扩展性都较大,不采用。
2. 调用Google、Baidu的API实现站内搜索。同第三方搜索引擎绑定太死,无法满足后期业务扩展需要,暂时不采用。
3. 基于Compass+Lucene实现站内搜索。适合于对数据库驱动的应用数据进行索引,尤其是替代传统的like ‘%expression%’来实现对varchar或clob等字段的索引,对于实现站内搜索是一种值得采纳的方案。但在分布式处理、接口封装上尚需要自己进行一定程度的封装,暂时不采用。
4. 基于Solr实现站内搜索。封装及扩展性较好,提供了较为完备的解决方案,因此在门户社区中采用此方案,后期加入Compass方案。
1、 Solr简介
Solr是一个基于Lucene的Java搜索引擎服务器。Solr 提供了层面搜索、命中醒目显示并且支持多种输出格式(包括 XML/XSLT 和 JSON 格式)。它易于安装和配置,而且附带了一个基于 HTTP 的管理界面。Solr已经在众多大型的网站中使用,较为成熟和稳定。Solr 包装并扩展了 Lucene,所以Solr的基本上沿用了Lucene的相关术语。更重要的是,Solr 创建的索引与 Lucene 搜索引擎库完全兼容。通过对Solr 进行适当的配置,某些情况下可能需要进行编码,Solr 可以阅读和使用构建到其他 Lucene 应用程序中的索引。此外,很多 Lucene 工具(如Nutch、 Luke)也可以使用Solr 创建的索引。
2、 Tomcat下Solr安装配置
由于Solr基于java开发,因此Solr在windows及Linux都能较好部署使用,但由于Solr提供了一些用于测试及管理、维护较为方便的shell脚本,因此在生产部署时候建议安装在Linux上,测试时候可以在windows使用。
下面以Linux下安装配置Solr进行说明,windows与此类似。
wget http://apache.mirror.phpchina.com/tomcat/tomcat-6/v6.0.16/bin/apache-tomcat-6.0.16.zip
unzip apache-tomcat-6.0.16.zip
mv apache-tomcat-6.0.16 /opt/tomcat
chmod 755 /opt/tomcat/bin/*
wget http://apache.mirror.phpchina.com/lucene/solr/1.2/apache-solr-1.2.0.tgz
tar zxvf apache-solr-1.2.0.tgz
Solr的安装配置最为麻烦的是对solr.solr.home的理解和配置,主要有三种
基于当前路径的方式
cp apache-solr-1.2.0/dist/apache-solr-1.2.0.war /opt/tomcat/webapps/solr.war
mkdir /opt/solr-tomcat
cp -r apache-solr-1.2.0/example/solr/ /opt/solr-tomcat/
cd /opt/solr-tomcat
/opt/tomcat/bin/startup.sh
由于在此种情况下(没有设定solr.solr.home环境变量或JNDI的情况下),Solr查找./solr,因此在启动时候需要切换到/opt/solr-tomcat
基于环境变量solr.solr.home
在当前用户的环境变量中(.bash_profile)或在/opt/tomcat/catalina.sh中添加如下环境变量
export JAVA_OPTS="$JAVA_OPTS -Dsolr.solr.home=/opt/solr-tomcat/solr"
基于JNDI配置
mkdir –p /opt/tomcat/conf/Catalina/localhost
touch /opt/tomcat/conf/Catalina/localhost/solr.xml ,内容如下:
- <Context docBase="/opt/tomcat/webapps/solr.war" debug="0" crossContext="true" >
- <Environment name="solr/home" type="java.lang.String" value="/opt/solr-tomcat/solr" override="true" />
- </Context>
访问solr管理界面 http://ip:port/solr
3、 Solr原理
Solr对外提供标准的http接口来实现对数据的索引的增加、删除、修改、查询。在 Solr 中,用户通过向部署在servlet 容器中的 Solr Web 应用程序发送 HTTP 请求来启动索引和搜索。Solr 接受请求,确定要使用的适当SolrRequestHandler,然后处理请求。通过 HTTP 以同样的方式返回响应。默认配置返回Solr 的标准 XML 响应,也可以配置Solr 的备用响应格式。
可以向 Solr 索引 servlet 传递四个不同的索引请求:
add/update 允许向 Solr 添加文档或更新文档。直到提交后才能搜索到这些添加和更新。
commit 告诉 Solr,应该使上次提交以来所做的所有更改都可以搜索到。
optimize 重构 Lucene 的文件以改进搜索性能。索引完成后执行一下优化通常比较好。如果更新比较频繁,则应该在使用率较低的时候安排优化。一个索引无需优化也可以正常地运行。优化是一个耗时较多的过程。
delete 可以通过 id 或查询来指定。按 id 删除将删除具有指定 id 的文档;按查询删除将删除查询返回的所有文档。
一个典型的Add请求报文
- <add>
- <doc>
- <field name="id">TWINX2048-3200PRO</field>
- <field name="name">CORSAIR XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail</field>
- <field name="manu">Corsair Microsystems Inc.</field>
- <field name="cat">electronics</field>
- <field name="cat">memory</field>
- <field name="features">CAS latency 2, 2-3-3-6 timing, 2.75v, unbuffered, heat-spreader</field>
- <field name="price">185</field>
- <field name="popularity">5</field>
- <field name="inStock">true</field>
- </doc>
- <doc>
- <field name="id">VS1GB400C3</field>
- <field name="name">CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - Retail</field>
- <field name="manu">Corsair Microsystems Inc.</field>
- <field name="cat">electronics</field>
- <field name="cat">memory</field>
- <field name="price">74.99</field>
- <field name="popularity">7</field>
- <field name="inStock">true</field>
- </doc>
- </add>
一个典型的搜索结果报文:
- <response>
- <lst name="responseHeader">
- <int name="status">0</int>
- <int name="QTime">6</int>
- <lst name="params">
- <str name="rows">10</str>
- <str name="start">0</str>
- <str name="fl">*,score</str>
- <str name="hl">true</str>
- <str name="q">content:"faceted browsing"</str>
- </lst>
- </lst>
- <result name="response" numFound="1" start="0" maxScore="1.058217">
- <doc>
- <float name="score">1.058217</float>
- <arr name="all">
- <str>http://localhost/myBlog/solr-rocks-again.html</str>
- <str>Solr is Great</str>
- <str>solr,lucene,enterprise,search,greatness</str>
- <str>Solr has some really great features, like faceted browsing and replication</str>
- </arr>
- <arr name="content">
- <str>Solr has some really great features, like faceted browsing and replication</str>
- </arr>
- <date name="creationDate">2007-01-07T05:04:00.000Z</date>
- <arr name="keywords">
- <str>solr,lucene,enterprise,search,greatness</str>
- </arr>
- <int name="rating">8</int>
- <str name="title">Solr is Great</str>
- <str name="url">http://localhost/myBlog/solr-rocks-again.html</str>
- </doc>
- </result>
- <lst name="highlighting">
- <lst name="http://localhost/myBlog/solr-rocks-again.html">
- <arr name="content">
- <str>Solr has some really great features, like <em>faceted</em>
- <em>browsing</em> and replication</str>
- </arr>
- </lst>
- </lst>
- </response>
关于solr的详细使用说明,请参考
http://wiki.apache.org/solr/FrontPage
4、 Solr测试使用
Solr的安装包包含了相关的测试样例,路径在apache-solr-1.2.0/example/exampledocs
1. 使用shell脚本(curl)测试Solr的操作:
cd apache-solr-1.2.0/example/exampledocs
vi post.sh,根据tomcat的ip、port修改URL变量的值URL=http://localhost:8080/solr/update
./post.sh *.xml #
2. 使用Solr的java 包测试Solr的操作:
查看帮助:java -jar post.jar –help
提交测试数据:
java -Durl=http://localhost:8080/solr/update -Ddata=files -jar post.jar *.xml
下面以增加索引字段liangchuan、url为例,说明一下Solr中索引命令的使用
1) 修改solr的schema,配置需要索引字段的说明:
vi /opt/solr-tomcat/solr/conf/schema.xml ,在<fields>中增加如下内容
- <field name="liangchuan" type="string" indexed="true" stored="true"/>
- <field name="url" type="string" indexed="true" stored="true"/>
- <field name="liangchuan" type="string" indexed="true" stored="true"/>
- <field name="url" type="string" indexed="true" stored="true"/>
2) 创建增加索引请求的xml测试文件
touch /root/apache-solr-1.2.0/example/exampledocs/liangchuan.xml,内容如下:
- <add>
- <doc>
- <field name="id">liangchuan000</field>
- <field name="name">Solr, the Enterprise Search Server</field>
- <field name="manu">Apache Software Foundation</field>
- <field name="liangchuan">liangchuan's solr "hello,world" test</field>
- <field name="url">http://www.google.com</field>
- </doc>
- </add>
3) 提交索引请求
cd apache-solr-1.2.0/example/exampledocs
./post.sh liangchuan.xml
4) 查询
通过solr的管理员界面http://localhost:8080/solr/admin查询
或通过curl 测试:
export URL="http://localhost:8080/solr/select/"
curl "$URL?indent=on&q=liangchuan&fl=*,score"
5、Solr查询条件参数说明
参数 描述 示例
q
Solr 中用来搜索的查询。可以通过追加一个分号和已索引且未进行断词的字段的名称来包含排序信息。默认的排序是 score desc,指按记分降序排序。
q=myField:JavaAND otherField:developerWorks; date asc
此查询搜索指定的两个字段并根据一个日期字段对结果进行排序。
start 将初始偏移量指定到结果集中。可用于对结果进行分页。默认值为 0。
start=15
返回从第 15 个结果开始的结果。
rows 返回文档的最大数目。默认值为 10。 rows=25
fq 提供一个可选的筛选器查询。查询结果被限制为仅搜索筛选器查询返回的结果。筛选过的查询由 Solr 进行缓存。它们对提高复杂查询的速度非常有用。
任何可以用 q 参数传递的有效查询,排序信息除外。
hl 当 hl=true 时,在查询响应中醒目显示片段。默认为 false。参看醒目显示参数上的 Solr Wiki 部分可以查看更多选项 hl=true
fl 作为逗号分隔的列表指定文档结果中应返回的 Field 集。默认为 “*”,指所有的字段。“score” 指还应返回记分。
*,score
其中关于Solr查询相关的参数详细的信息请参看:
http://wiki.apache.org/solr/CommonQueryParameters
Solr的查询条件参数q的格式与Lucene相同,具体参看:
http://lucene.apache.org/java/docs/queryparsersyntax.html
支持一下吆 请收藏一下:很好
6、 在门户社区中solr使用模式
在门户社区中需要使用solr,可采用如下模式:
对原有系统已有的数据或需要索引的数据量较大的情况
直接采用通过http方式调用solr的接口方式,效率较差,采用solr本身对csv 的支持(http://wiki.apache.org/solr/UpdateCSV
),将数据导出为csv格式,然后调用solr的csv接口http://localhost:8080/solr/update/csv
对系统新增的数据
先将需要索引查询的数据组装成xml格式,然后使用httpclient 将数据提交到solr 的http接口,例如
http://localhost:8080/solr/update
也可以参考post.jar中的SimplePostTool的实现。
http://svn.apache.org/viewvc/lucene/solr/trunk/src/java/org/apache/solr/util/SimplePostTool.java?view=co
中文分词
采用庖丁解牛作为solr(Lucene)缺省的中文分词方案
项目库:http://code.google.com/p/paoding/
Google groups:http://groups.google.com/group/paoding
Javaeye的groups:http://analysis.group.javaeye.com/
与nutch的集成使用
http://blog.foofactory.fi/2007/02/online-indexing-integrating-nutch-with.html
嵌入式Solr
http://wiki.apache.org/solr/Solrj#EmbeddedSolrServer
分布式索引
http://wiki.apache.org/solr/CollectionDistribution
7、参考资料
http://wiki.apache.org/solr/
http://www.ibm.com/developerworks/cn/java/j-solr1/
http://www.ibm.com/developerworks/cn/java/j-solr2/
http://www.xml.com/pub/a/2006/08/09/solr-indexing-xml-with-lucene-andrest.html?page=1
http://lucene.apache.org/java/docs/queryparsersyntax.html
http://www.blogjava.NET/RongHao/archive/2007/11/06/158621.html
Solr使用入门指南的更多相关文章
- 企业级搜索引擎Solr使用入门指南
由于搜索引擎功能在门户社区中对提高用户体验有着重在门户社区中涉及大量需要搜索引擎的功能需求,目前在实现搜索引擎的方案上有集中方案可供选择: 基于Lucene自己进行封装实现站内搜索. 工作量及扩展性都 ...
- [转载] Solr使用入门指南
转载自http://blog.csdn.net/liuzhenwen/article/details/4060922 由于搜索引擎功能在门户社区中对提高用户体验有着重要的作用,在门户社区中涉及大量需要 ...
- Solr快速入门(一)
概述 本文档介绍了如何获取和运行Solr,将各种数据源收集到多个集合中,以及了解Solr管理和搜索界面. 首先解压缩Solr版本并将工作目录更改为安装Solr的子目录.请注意,基本目录名称可能随Sol ...
- Web API 入门指南 - 闲话安全
Web API入门指南有些朋友回复问了些安全方面的问题,安全方面可以写的东西实在太多了,这里尽量围绕着Web API的安全性来展开,介绍一些安全的基本概念,常见安全隐患.相关的防御技巧以及Web AP ...
- Vue.js 入门指南之“前传”(含sublime text 3 配置)
题记:关注Vue.js 很久了,但就是没有动手写过一行代码,今天准备入手,却发现自己比菜鸟还菜,于是四方寻找大牛指点,才终于找到了入门的“入门”,就算是“入门指南”的“前传”吧.此文献给跟我一样“白痴 ...
- yii2实战教程之新手入门指南-简单博客管理系统
作者:白狼 出处:http://www.manks.top/document/easy_blog_manage_system.html 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文 ...
- 【翻译】Fluent NHibernate介绍和入门指南
英文原文地址:https://github.com/jagregory/fluent-nhibernate/wiki/Getting-started 翻译原文地址:http://www.cnblogs ...
- ASP.NET MVC 5 入门指南汇总
经过前一段时间的翻译和编辑,我们陆续发出12篇ASP.NET MVC 5的入门文章.其中大部分翻译自ASP.NET MVC 5 官方教程,由于本系列文章言简意赅,篇幅适中,从一个web网站示例开始讲解 ...
- 一起学微软Power BI系列-官方文档-入门指南(1)Power BI初步介绍
我们在前一篇文章微软新神器-Power BI,一个简单易用,还用得起的BI产品中,我们初步介绍了Power BI的基本知识.由于Power BI是去年开始微软新发布的一个产品,虽然已经可以企业级应用, ...
随机推荐
- CF719E(线段树+矩阵快速幂)
题意:给你一个数列a,a[i]表示斐波那契数列的下标为a[i],求区间对应斐波那契数列数字的和,还要求能够维护对区间内所有下标加d的操作 分析:线段树 线段树的每个节点表示(f[i],f[i-1])这 ...
- jquery中attr和prop的区别
在高版本的jquery引入prop方法后,什么时候该用prop?什么时候用attr?它们两个之间有什么区别?这些问题就出现了. 关于它们两个的区别,网上的答案很多.这里谈谈我的心得,我的心得很简单: ...
- maven+springmvc+dubbo+zookeeper
为什么要用dubbo? 还是让官方来解释吧: http://dubbo.io/User+Guide-zh.htm http://dubbo.io/ 一般 nginx+tomcat ...
- shell-for循环
sheel语言for循环格式 for var in item1 item2 ... itemN do command1 command2 ... commandN done 案例1 #!/bin/ba ...
- jquery $.extend()扩展插件获取焦点或失去焦点事件
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Git 耍不起来啊
1. $ git clone https://******.git Cloning into 'dt-engine-server'...fatal: unable to access 'https: ...
- 【BZOJ-4698】Sandy的卡片 后缀数组
4698: Sdoi2008 Sandy的卡片 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 140 Solved: 55[Submit][Stat ...
- flex学习小结
接触到flex一个多月了,今天做一个学习小结.如果有知识错误或者意见不同的地方.欢迎交流指教. 画外音:先说一下,我是怎么接触到flex布局的.对于正在学习的童鞋们,我建议大家没事可以逛逛网站,看看人 ...
- django文件上传和序列化
django实现文件上传 使用form表单上传文件 html页面 <html lang="en"> <head> <meta charset=&quo ...
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
传送门 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46727 Acce ...