solr使用Word文档下载推荐.docx
《solr使用Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《solr使用Word文档下载推荐.docx(10页珍藏版)》请在冰豆网上搜索。
img_03"
data_images/img3.jpg"
img_04"
data_images/img4.jpg"
/div>
divstyle="
bottom:
0;
position:
fixed;
"
hrstyle="
clear:
both;
h4>
http:
//www.script-tooriginalarticlepage<
/h4>
1,使用slorj的清除原有的索引.或者手动删除。
2,编写代码,将lucene建立的索引(1G左右,874400条记录),按照比例通过solrj分发到三台solr(shard)服务器上,代码如下:
publicclassIndexCreate{
privatestaticCommonsHttpSolrServerserver;
publicCommonsHttpSolrServergetServer(StringhostUrl){
CommonsHttpSolrServerserver=null;
try{
server=newCommonsHttpSolrServer(hostUrl);
server.setConnectionTimeout(100);
server.setDefaultMaxConnectionsPerHost(100);
server.setMaxTotalConnections(100);
}catch(IOExceptione){
System.out.println("
请检查tomcat服务器或端口是否开启!
);
}
returnserver;
}
@SuppressWarnings("
deprecation"
)
publicvoidreaderHostCreate(String[]hosts)throwsCorruptIndexException,IOException{
IndexReaderreader=IndexReader.open("
c:
\\index"
System.out.println("
总记录数:
"
+reader.numDocs());
inthostNum=hosts.length;
intlengh=reader.numDocs()/hostNum;
//根据主机数平分索引长度
intj=reader.numDocs()%hostNum;
//取余
for(inti=0;
i<
hosts.length;
i++){
longstartTime=newDate().getTime();
Stringurl=hosts[i].substring(hosts[i].indexOf("
//"
)+2,hosts[i].lastIndexOf("
/"
));
第"
+(i+1)+"
次,在主机:
+url+"
上创建索引,创建时间"
+newDate());
if(i==(hosts.length-1)){
hostlist(reader,lengh*i,lengh*(i+1)+j,hosts[i]);
}else{
hostlist(reader,lengh*i,lengh*(i+1),hosts[i]);
}
结束时间"
longendTime=newDate().getTime();
longms=(endTime-startTime)%60000-(((endTime-startTime)%60000)/1000)*1000;
本次索引创建完毕,一共用了"
+(endTime-startTime)/60000+"
分"
+
"
+((endTime-startTime)%60000)/1000+"
秒"
+ms+"
毫秒"
****************************"
reader.close();
publicvoidcreateIndex(List<
BookIndex>
beans,StringhostUrl,CommonsHttpSolrServerserver){
server.addBeans(beans);
mit();
}catch(SolrServerExceptione){
e.printStackTrace();
publicstaticvoidmain(String[]args)throwsCorruptIndexException,IOException{
IndexCreateas=newIndexCreate();
String[]hosts=newString[]{"
//192.168.169.121:
9888/solr"
"
9088/solr"
//192.168.169.48:
};
longstartTime=newDate().getTime();
as.readerHostCreate(hosts);
longendTime=newDate().getTime();
-------------------"
longms=(endTime-startTime)%60000-(((endTime-startTime)%60000)/1000)*1000;
全部索引创建完毕,一共用了"
"
}
JavaBean类BookIndex.java代码如下:
说明变量名与sechma.xml中的配置要相同.注意:
不能使用score这个变量或者字段,与slor配置冲突,报exception。
importorg.apache.solr.client.solrj.beans.Field;
publicclassBookIndex{
@Field
privateStringzjid;
privateStringtitle;
privateStringssid;
privateStringdxid;
privateStringbookname;
privateStringauthor;
privateStringpublisher;
privateStringpubdate;
privateStringyear;
privateStringfenlei;
privateStringscore1;
privateStringisbn;
privateStringfenleiurl;
privateStringmulu;
privateStringisp;
privateStringiep;
publicgetters();
//get方法
publicsetters();
//set方法
3,同时开启四台服务器,运行上面代码:
4,打开IE查询
打开:
//localhost/solr
//localhost:
9888/solr
打开http:
9008/solr
//192.168.168.48:
四、Solr的Multicore(分片)配置
body{background:
#eee;
margin:
padding:
0}
.example{background:
#FFF;
width:
800px;
border:
1px#000solid;
20pxauto;
15px;
-moz-border-radius:
3px;
-webkit-border-radius:
3px}
.exampleimg{margin:
22px}
}
六、一个简单的web引用:
首先说明的是索引来源,是根据已有的lucene索引上开发的,又因为lucene的索引直接用solrJ应用效果不好,会出现很多问题,找不到类似的解决办法,比如全字段查询,和高亮分组等,和多级索引目录…。
但用solrJ创建的索引不存在类似的问题.
大致的思路是,读取已有的lucene索引,再用solrJ创建索引并分发到多台机器上,最后再做开发.
第一步:
读取lucene的多级索引目录,用solrJ创建和分发索引;
需注意的是:
要加大虚拟机的内存,因为采用的map做为缓存,理论上虚拟机的内存大,map的存储的索引文档数也就多.主要是针对内存溢出.
代码:
packageorg.readerIndex;
publicclassBookIndex2{
@Field
privateStringzjid;
privateStringzhangjie;
privateStringqwpos;
privateStringpublishDate;
privateStringaddress;
publicStringgetZjid(){
returnzjid;
…………………………………………
importjava.io.File;
importjava.io.IOException;
importjava.text.SimpleDateFormat;
importjava.util.ArrayList;
importjava.util.Date;
importjava.util.List;
importorg.apache.lucene.document.Document;
importorg.apache.lucene.index.CorruptIndexException;
importorg.apache.lucene.index.IndexReader;
importorg.apache.lucene.store.LockObtainFailedException;
importorg.apache.solr.client.solrj.SolrServerException;
importorg.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
publicclassReaderIndex{
publicvoidindexDocuements(Stringpath,String[]hostUrls)throwsCorruptIndexException,LockObtainFailedException,IOException{
FilepareFile=newFile(path);
List<
String>
list=newArrayList<
();
getFile(pareFile,list);
//递归方法得到路径保存到list中
***程序一共递归到"
+list.size()+"
个索引目录***"
intarevageSize=list.size()/hostUrls.length;
//根据主机数平分目录
intremainSize=list.size()%hostUrls.length;
//取余
SimpleDateFormatsdf=newSimpleDateFormat("
yyyy-MM-ddHH:
mm:
ss"
for(inti=0;
hostUrls.length;
DatestartDate=newDate();
Stringurl=hostUrls[i].substring(hostUrls[i].indexOf("
)+2,hostUrls[i].lastIndexOf("
上创建索引,创建时间"
+sdf.format(startDate));
if(i==(hostUrls.length-1)){
list(list,arevageSize*i,arevageSize*(i+1)+remainSize,hostUrls[i]);
/*
*/
(function($){
$.fn.imageLens=function(options){
vardefaults={
lensSize:
100,
borderSize:
4,
borderColor:
#888"
};
varoptions=$.extend(defaults,options);
varlensStyle="
background-position:
0px0px;
+String(options.lensSize)+"
px;
height:
+String(options.lensSize)
+"
float:
left;
display:
none;
border-radius:
+String(options.lensSize/2+options.borderSize)
+String(options.borderSize)+"
pxsolid"
+options.borderColor
;
background-repeat:
no-repeat;
absolute;
returnthis.each(function(){
obj=$(this);
varoffset=$(this).offset();
//Creatinglens
vartarget=$("
divstyle='
+lensStyle+"
'
class='
+options.lensCss+"
&
nbsp;
).appendTo($(this).parent());
vartargetSize=target.size();
//Calculatingactualsizeofimage
varimageSrc=options.imageSrc?
options.imageSrc:
$(this).attr("
src"
varimageTag="
imgstyle='
none;
src='
+imageSrc+"
varwidthRatio=0;
varheightRatio=0;
$(imageTag).load(function(){
widthRatio=$(this).width()/obj.width();
heightRatio=$(this).height()/obj.height();
}).appendTo($(this).parent());
target.css({backgroundImage:
url('
)"
});
target.mousemove(setPosition);
$(this).mousemove(setPosition);
functionsetPosition(e){
varleftPos=parseInt(e.pageX-offset.left);
vartopPos=parseInt(e.pageY-offset.top);
if(leftPos<
0||topPos<
0||leftPos>
obj.width()||topPos>
obj.height()){
target.hide();
else{
target.show();
leftPos=String(((e.pageX-offset.left)*widthRatio-target.width()/2)*(-1));
topPos=String(((e.pageY-offset.top)*heightRatio-target.height()/2)*(-1));
target.css({backgroundPosition:
leftPos+'
px'
+topPos+'
px'
leftPos=String(e.pageX-target.width()/2);
topPos=String(e.pageY-target.height()/2);
target.css({left:
top:
topPos+'
})(jQuery);