手机版
你好,游客 登录 注册
背景:
阅读新闻

Solr使用入门教程

[日期:2013-10-06] 来源:Linux社区  作者:Linux [字体: ]

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. 使用SolrJava 包测试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>中增加如下内容

  1. <fieldname="liangchuan"type="string"indexed="true"stored="true"/>
  2. <fieldname="url"type="string"indexed="true"stored="true"/>

2) 创建增加索引请求的xml测试文件
touch /root/apache-solr-1.2.0/example/exampledocs/liangchuan.xml,内容如下:

  1. <add>
  2. <doc>
  3. <fieldname="id">liangchuan000</field>
  4. <fieldname="name">Solr, the Enterprise Search Server</field>
  5. <fieldname="manu">Apache Software Foundation</field>
  6. <fieldname="liangchuan">liangchuan's solr "hello,world" test</field>
  7. <fieldname="url">http://www.google.com</field>
  8. </doc>
  9. </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:Java AND 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

linux
相关资讯       Solr  Solr教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款