设为首页 收藏本站
查看: 1523|回复: 0

[经验分享] 【solr基础教程之二】索引

[复制链接]
累计签到:3 天
连续签到:1 天
发表于 2015-7-19 08:59:51 | 显示全部楼层 |阅读模式
  


  一、向Solr提交索引的方式
  


1、使用post.jar进行索引

(1)创建文档xml文件



test4
testagain
http://www.163.com



(2)使用java -jar post.jar

[iyunv@jediael44 exampledocs]# java -Durl=http://ip:8080/solr/update -jar post.jar test.xml
SimplePostTool version 1.5
Posting files to base url http://ip:8080/solr/update using content-type application/xml..
POSTing file test.xml
1 files indexed.
COMMITting Solr index changes to http://localhost:8080/solr/update..
Time spent: 0:00:00.135

(3)查看post.jar的使用方法

[iyunv@jediael44 exampledocs]# java -jar post.jar --help
SimplePostTool version 1.5
Usage: java [SystemProperties] -jar post.jar [-h|-] [ [...]]
Supported System Properties and their defaults:
-Ddata=files|web|args|stdin (default=files)
-Dtype= (default=application/xml)
-Durl= (default=http://localhost:8983/solr/update)
-Dauto=yes|no (default=no)
-Drecursive=yes|no| (default=0)
-Ddelay= (default=0 for files, 10 for web)
-Dfiletypes=[,,...] (default=xml,json,csv,pdf,doc,docx,ppt,pptx,xls,xlsx,odt,odp,ods,ott,otp,ots,rtf,htm,html,txt,log)
-Dparams="=[&=...]" (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. Data can be read from files specified as commandline args, URLs specified as args, as raw commandline arg strings or via STDIN.
Examples:
java -jar post.jar *.xml
java -Ddata=args -jar post.jar '42'
java -Ddata=stdin -jar post.jar < hd.xml
java -Ddata=web -jar post.jar http://example.com/
java -Dtype=text/csv -jar post.jar *.csv
java -Dtype=application/json -jar post.jar *.json
java -Durl=http://localhost:8983/solr/update/extract -Dparams=literal.id=a -Dtype=application/pdf -jar post.jar a.pdf
java -Dauto -jar post.jar *
java -Dauto -Drecursive -jar post.jar afolder
java -Dauto -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 and url automatically from file name. When posting rich documents the file name will be propagated as &quot;resource.name&quot; and also used as &quot;literal.id&quot;. You may override these or any other request parameter
through the -Dparams property. To do a commit only, use &quot;-&quot; as argument. The web mode is a simple crawler following links within domain, default delay=10s.

(4)默认情况下,使用xml文件作数据源,若使用其它方式,如下  

java -Dtype=application/json -jar post.jar *.json




2、使用管理界面的Document页面进行提交
DSC0000.jpg
  
  3、使用SolrJ进行索引
  (1)使用SolrJ进行简单索引
  

package org.ljh.test.solr;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;
public class BasicSolrJIndexDemo {
public static void main(String[] args) throws Exception {
/*
* 注意,虽然使用地址http://ip:8080/solr/#/collection1来访问页面,但应该通过http:/
* /ip:8080/solr/collection1来进行文档的提交
*/
String serverUrl = (args != null && args.length > 0) ? args[0]
: &quot;http://localhost:8080/solr/collection1&quot;;
SolrServer solrServer = new HttpSolrServer(serverUrl);
SolrInputDocument doc1 = new SolrInputDocument();
doc1.setField(&quot;id&quot;, &quot;solrJTest3&quot;);
doc1.setField(&quot;url&quot;, &quot;http://www.163.com/&quot;);
solrServer.add(doc1);
SolrInputDocument doc2 = new SolrInputDocument();
doc2.setField(&quot;id&quot;, &quot;solrJTest4&quot;);
doc2.setField(&quot;url&quot;, &quot;http://www.sina.com/&quot;);
solrServer.add(doc2);
solrServer.commit(true,true);
}

}


(2)使用SolrJ进行简单查询  
  

package org.ljh.test.solr;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
public class BasicSolrJSearchDemo {
public static void main(String[] args) throws Exception {
String serverUrl = (args != null && args.length > 0) ? args[0]
: &quot;http://localhost:8080/solr/collection1&quot;;
SolrServer solrServer = new HttpSolrServer(serverUrl);
//读取输入参数作为查询关键字,若无关键字,则查询全部内容。
String queryString = (args != null && args.length > 1) ? args[1] : &quot;url:163&quot;;
SolrQuery solrQuery = new SolrQuery(queryString);
solrQuery.setRows(5);
QueryResponse resp = solrServer.query(solrQuery);
SolrDocumentList hits = resp.getResults();
for(SolrDocument doc : hits ){
System.out.println(doc.getFieldValue(&quot;id&quot;).toString() + &quot; : &quot; + doc.getFieldValue(&quot;url&quot;));
}
}
}


4、使用第三方工具  
  (1)DIH
  


  (2)ExtractingRequestHandler, aka Solr Cell
  


  (3)Nutch
  


  二、schema.xml : 定义文档的&#26684;式
  schema.xml定义了被索引的文档应该包括哪些Field、这个Filed的类型,以及其它相关信息。
  1、示例
  Nutch为Solr提供的schema.xml如下:
  



























































   


id
content

以上文档包括5个部分:  
  (1)FiledType:域的类型

(2)Field:哪些域被索引、存储等,以及这个域是什么类型。

(3)uniqueKey:哪个域作为id,即文章的唯一标识。

(4)defaultSearchField:默认的搜索域

(5)solrQueryParser:OR,即使用OR来构建Query。


  


  2、Field元素
  一个或者多个Field元素组成一个Fields元素,Nutch中使用了此结构,但solr的example中没有Fileds元素,而是直接将Fields元素作为schma元素的下一级元素。FieldType与此类&#20284;。
  一个Filed的示例如下:
  

Filed的几个基本属性如下:  
  (1)name属性
  域的名称
  (2)type属性
  域的类型
  (3)stored属性
  是否存储这个域,只有存储了,才能在搜索结果中查看这个域的完整内容。
  (4)indexed属性
  是否索引这个域,索引了就可以用作搜索域,除此之外,即使你不需要对这个域进行搜索,但需要排序、分组、查询提示、facet、function queries等,也需要对这个域进行索引。
  例如,查询一本书时,一般不会通过销售的数量进行搜索,但会根据销售的数量进行排序。
  In addition to enabling searching, you will also need to mark your field as indexed if you need to sort, facet, group by, provide query suggestions for, or execute function queries on values within a field.


  (5)multiValued属性
  若一个域中允许存在多个&#20540;,则设置multiValued为true。
  



  


此时,在被索引的文档中,可以使用多个具有相同name&#20540;的Filed。



............
lucene
solr



若使用SolrJ,则使用addField方法代替setField方法。
doc.addField(&quot;tag&quot;,&quot;lucene&quot;);
doc.addField(&quot;tag&quot;,&quot;solr&quot;);
  
  (6)required属性
  Solr使用required属性来指定每个提交的文档都必须提供这个域。注意uniqueKey元素中指定的域隐含了required=true。
  


  


  3、dynamicField元素


  





(1)一般而言,不要使用动态域,除非是以下三种情况

Dynamic fields help address common problems that occur when building search applications, including

■ Modeling documents with many fields

■ Supporting documents from diverse sources

■ Adding new document sources

具体可见solr in action的5.3.3节。

  
  4、copyField
  copyFiled用于以下2种情形
  copy fields support two use cases that are common in most search applications:

■ Populate a single catch-all field with the contents of multiple fields.

■ Apply different text analysis to the same field content to create a new searchable field.
  即
  (1)将多个域复制到一个单一的域,以方便搜索等。如:
  











则搜索时只对text进行搜索即可。  
  (2)对同一个域进行多次不同的分析处理,如:
  



...
在上述例子中,若对一个域进行索引,则将词汇词干化,但在搜索提示时,就不对词汇进行词干化。  
  


  5、FieldType元素
  (1)FiedlType定义了Filed的类型,它将在Filed中的type属性中被引用。
  (2)Solr内置的FiledType有以下类型:
DSC0001.jpg


  (3)有2大类FieldType:
  一类是要对其进行分析后再索引的非结构化数据,如文章 的正文等,如StrField,TrieLongField等。
  另一类是不需要对其进行分析,而直接索引的的结构批数据,如url,id,人名等,主要是TextField。
  (4)在schema.xml中看到 的solr.*代表的是org.apache.solr.schema.*,如
  

         表示类型为org.apache.solr.schema.StrField。  
  (5)StringField
  StringField中的内容不应该被分析,它包含的是结构化数据。
  StringField,用类org.apache.solr.schema.StrField表示。
  (6)DateField
  DateField一般使用TrieDateField来表示,其中Trie数据可以方便的进行范围搜索。
  DateField的默认&#26684;式:In general, Solr expects your dates to be in the ISO-8601 Date/Time format (yyyy-MMddTHH:mm:ssZ); the date in our tweet (2012-05-22T09:30:22Z) breaks down to

yyyy = 2012

MM = 05

dd = 22

HH = 09 (24-hr clock)

mm = 30

ss = 22

Z = UTC Timezone (Z is for Zulu)


  可以通过以下方式截取其内容:
  2012-05022T09:30:00Z/HOUR
  表示截取到小时的粒度,即其&#20540;为:2012-05022T09:00:00Z
  (7)NumericField
  有多个实现类型,如TrieDoubleField,TrieFloatField,TrieIntField,TrieLongField等。
  (8)type有多个属性,主要包括
  sortMissingFirst:当根据使用这个类型的域进行排序时,若这个域没有&#20540;,则在排序时,将此文档放在最前面。
  sortMissingLast::当根据使用这个类型的域进行排序时,若这个域没有&#20540;,则在排序时,将此文档放在最后面。
  precisionStep:
  positionIncrementGap:见solr in action 5.4.4节。
  


  


  6、UniqueKey元素
  (1)Solr使用元素来标识一个唯一标识符,类&#20284;于一个数据库表的主键。如:
  

id必须选择一个Field作为一个uniqueKey。使用uniqueKey标识的字段,每一个进行索引的文档都必须提供。  
  (2)Solr不要求为每个文档提供一个唯一标识符,但建议为每个文档都提供一个唯一标识符,以用于避免重复等。
  (3)当向solr提交一个文档时,若此文档的id已经存在,则此文档会覆盖原有的文档。
  (4)如果solr被部署在多个服务器中,则必须提供uniqueKey。
  (5)使用基本类&#20284;来作为uniqueKey,不要使用复杂类型。  One thing to note is that it’s best to use a primitive field type, such as string or long, for the field you indicate as being the  as that ensures Solr doesn’t make

any changes to the value during indexing
  


  三、SolrConfig.xml中与索引相关的内容
  以下为一个示例
  





${solr.ulog.dir:}



${solr.autoCommit.maxTime:15000}
false



${solr.autoSoftCommit.maxTime:-1}








  
  


  


  


  


  


  


  


  


  


  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-88137-1-1.html 上篇帖子: solr 之 缓存 下篇帖子: solr dataimport中遇到的问题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表