您的位置:首页 > 其它

Solr之简单测试

2015-07-02 21:38 344 查看
服务启动后,目前你看到的界面没有任何数据,为了方便用户往solr中添加索引,Solr为用户提供了一个post.jar工具,用户只需要在命令行下运行post.jar并传入一些参数就可以完成索引的增删改操作,它仅仅是一个供用户进行Solr测试的工具而已,有关post.jar的使用说明如下:

[slim@localhost exampledocs]$ java -jar post.jar -h
SimplePostTool version 5.0.0
Usage: java [SystemProperties] -jar post.jar [-h|-] [<file|folder|url|arg> [<file|folder|url|arg>...]]

Supported System Properties and their defaults:
-Dc=<core/collection>
-Durl=<base Solr update URL> (overrides -Dc option if specified)
-Ddata=files|web|args|stdin (default=files)
-Dtype=<content-type> (default=application/xml)
-Dhost=<host> (default: localhost)
-Dport=<port> (default: 8983)
-Dauto=yes|no (default=no)
-Drecursive=yes|no|<depth> (default=0)
-Ddelay=<seconds> (default=0 for files, 10 for web)
-Dfiletypes=<type>[,<type>,...] (default=xml,json,csv,pdf,doc,docx,ppt,pptx,xls,xlsx,odt,odp,ods,ott,otp,ots,rtf,htm,html,txt,log)
-Dparams="<key>=<value>[&<key>=<value>...]" (values must be URL-encoded)
-Dcommit=yes|no (default=yes)
-Doptimize=yes|no (default=no)
-Dout=yes|no (default=no)

This is a simple command line tool for POSTing raw data to a Solr port.
NOTE: Specifying the url/core/collection name is mandatory.
Data can be read from files specified as commandline args,
URLs specified as args, as raw commandline arg strings or via STDIN.
Examples:
java -Dc=gettingstarted -jar post.jar *.xml
java -Ddata=args -Dc=gettingstarted -jar post.jar '<delete><id>42</id></delete>'
java -Ddata=stdin -Dc=gettingstarted -jar post.jar < hd.xml
java -Ddata=web -Dc=gettingstarted -jar post.jar http://example.com/ java -Dtype=text/csv -Dc=gettingstarted -jar post.jar *.csv
java -Dtype=application/json -Dc=gettingstarted -jar post.jar *.json
java -Durl=http://localhost:8983/solr/techproducts/update/extract -Dparams=literal.id=pdf1 -jar post.jar solr-word.pdf
java -Dauto -Dc=gettingstarted -jar post.jar *
java -Dauto -Dc=gettingstarted -Drecursive -jar post.jar afolder
java -Dauto -Dc=gettingstarted -Dfiletypes=ppt,html -jar post.jar afolder
The options controlled by System Properties include the Solr
URL to POST to, the Content-Type of the data, whether a commit
or optimize should be executed, and whether the response should
be written to STDOUT. If auto=yes the tool will try to set type
automatically from file name. When posting rich documents the
file name will be propagated as "resource.name" and also used
as "literal.id". You may override these or any other request parameter
through the -Dparams property. To do a commit only, use "-" as argument.
The web mode is a simple crawler following links within domain, default delay=10s.
使用post.jar前需要配置solr的core,添加一个core你可以通过Solr Admin的web UI来创建,如图:



name : core名称,

instanceDir:core根目录,相对于你的solr-home目录,可以在SOLR_HOME下创建多个core目录

dataDir :core的数据目录,当前core的索引数据会存放在dataDir下的data\index目录下,上述所有文件夹需要你手动创建(除了data\index这里的index目录,solr会自动创建)

config :Solr配置文件,solrconfig.xml配置文件是每个core必须的一个配置文件,只对当前core有效

schema :Solr中用户定义字段类型及字段的配置文件,sechma.xml配置文件是用来定义索引的每个域的,比如域的名称啊,域的类型,域是否索引,是否存储,是否分词,是否存储项向量,使用什么分词器,指定同义词字典文件在哪儿,指定停用词字典文件在哪儿等

将solr_home/configsets/basic_configs/conf复制到instanceDir目录下。solr会根据配置目录加载配置文件。

Core创建成功后,你会在solr admin 后台看到这样的界面:



如上创建core完成,除此之外,我们可以通过Http管理Solr Core,实现索引的分类,参考:http://my.oschina.net/cloudcoder/blog/305033

修改schema.xml配置,索引字段:增加如下配置

<field name="name" type="text_general" indexed="true" stored="true" />
<field name="age" type="int" indexed="false" stored="true" />
<field name="desc" type="text_general" indexed="true" stored="true" />
<!--拷贝字段 多值-->
<field name="context" type="text_general" indexed="true" stored="false" multiValued="true"/>

<copyField source="name" dest="context"/>
<copyField source="desc" dest="context"/>
编辑doc文档

<?xml version="1.0" encoding="UTF-8" ?>
<add>
<!-- 设置权重 -->
<doc boost="5.5">
<field name="id">abc121</field>
<field name="name" boost="2.0">zhang shan</field>
<field name="age">22</field>
<field name="desc">zhang shan  is student.</field>
</doc>
<doc>
<field name="id">abc122</field>
<field name="name" boost="2.0">li shan</field>
<field name="age">18</field>
<field name="desc">li shan  is student.</field>
</doc>
</add>
使用post.jar工具上传doc文件新增索引
[slim@localhost exampledocs]$ java -Dc=test -Dhost:localhost -Dport=9080 -jar post.jar test.xml
SimplePostTool version 5.0.0
Posting files to [base] url http://localhost:9080/solr/test/update using content-type application/xml...
POSTing file test.xml to [base]
1 files indexed.
COMMITting Solr index changes to http://localhost:9080/solr/test/update... Time spent: 0:00:00.747
通过控制台查询:



Solr的检索运算符:

“:” 指定字段查指定值,如返回所有值*:*

“?” 表示单个任意字符的通配

“*” 表示多个任意字符的通配(不能在检索的项开始使用*或者?符号)

“~” 表示模糊检索,如检索拼写类似于”roam”的项这样写:roam~将找到形如foam和roams的单词;roam~0.8,检索返回相似度在0.8以上的记录。邻近检索,如检索相隔10个单词的”apache”和”jakarta”,”jakarta apache”~10

“^” 控制相关度检索,如检索jakarta apache,同时希望去让”jakarta”的相关度更加好,那么在其后加上”^”符号和增量值,即jakarta^4 apache

布尔操作符AND、||

布尔操作符OR、&&

布尔操作符NOT、!、- (排除操作符不能单独与项使用构成查询)

“+” 存在操作符,要求符号”+”后的项必须在文档相应的域中存在

( ) 用于构成子查询

[] 包含范围检索,如检索某时间段记录,包含头尾,date:[200707 TO 200710]

{} 不包含范围检索,如检索某时间段记录,不包含头尾date:{200707 TO 200710}

/ 转义操作符,特殊字符包括+ - && || ! ( ) { } [ ] ^ ” ~ * ? : /

参考文章:

1.跟益达学Solr5之玩转post.jar

2.Apache Solr solrconfig.xml中文说明

3.solr schema.xml Field属性详解

4.solr schema.xml FieldType属性详解

5.Solr配置文件schema.xml和solrconfig.xml分析
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  全文检索 lucene Solr