1、solr使用Solr 1.4.1配置和SolrJ的用法一、 Solr基本安装和配置 imageLens samples a href=http:/www.script- to original article page 1, 使用slorj的清除原有的索引.或者手动删除。2, 编写代码,将lucene建立的索引(1G左右,874400条记录),按照比例通过solrj分发到三台solr(shard)服务器上,代码如下:public class IndexCreate private static CommonsHttpSolrServer server; public CommonsHttpSo
2、lrServer getServer(String hostUrl) CommonsHttpSolrServer server = null; try server = new CommonsHttpSolrServer(hostUrl); server.setConnectionTimeout(100); server.setDefaultMaxConnectionsPerHost(100); server.setMaxTotalConnections(100); catch (IOException e) System.out.println(请检查tomcat服务器或端口是否开启!);
3、return server; SuppressWarnings(deprecation) public void readerHostCreate(String hosts) throws CorruptIndexException, IOException IndexReader reader = IndexReader.open(c:index); System.out.println(总记录数: +reader.numDocs(); int hostNum = hosts.length; int lengh = reader.numDocs()/hostNum; /根据主机数平分索引长度
4、 int j = reader.numDocs()%hostNum; /取余 for(int i = 0;ihosts.length;i+) long startTime = new Date().getTime(); String url = hostsi.substring(hostsi.indexOf(/)+2,hostsi.lastIndexOf(/); System.out.println(第+(i+1)+次,在主机:+url+ 上创建索引,创建时间+new Date(); if(i=(hosts.length-1) hostlist(reader,lengh*i,lengh*(i+
5、1)+j,hostsi); else hostlist(reader,lengh*i,lengh*(i+1),hostsi); System.out.println(结束时间+new Date(); long endTime = new Date().getTime(); long ms = (endTime-startTime)%60000-(endTime-startTime)%60000)/1000)*1000; System.out.println(本次索引创建完毕,一共用了+(endTime-startTime)/60000+分 + +(endTime-startTime)%6000
6、0)/1000+秒+ms+毫秒); System.out.println(*); reader.close(); public void createIndex(List beans,String hostUrl,CommonsHttpSolrServer server) try server.addBeans(beans); mit(); catch (SolrServerException e) e.printStackTrace(); catch (IOException e) e.printStackTrace(); public static void main(String arg
7、s) throws CorruptIndexException, IOException IndexCreate as = new IndexCreate(); String hosts = new String http:/192.168.169.121:9888/solr,http:/192.168.169.121:9088/solr,http:/192.168.169.48:9888/solr; long startTime = new Date().getTime(); as.readerHostCreate(hosts); long endTime = new Date().getT
8、ime(); System.out.println(-); long ms = (endTime-startTime)%60000-(endTime-startTime)%60000)/1000)*1000; System.out.println(全部索引创建完毕,一共用了+(endTime-startTime)/60000+分 + +(endTime-startTime)%60000)/1000+秒+ms+毫秒); JavaBean类BookIndex.java代码如下:说明变量名与sechma.xml中的配置要相同.注意:不能使用score这个变量或者字段,与slor配置冲突,报excep
9、tion。import org.apache.solr.client.solrj.beans.Field;public class BookIndex Field private String zjid ; Field private String title; Field private String ssid; Field private String dxid; Field private String bookname; Field private String author; Field private String publisher; Field private String p
10、ubdate; Field private String year; Field private String fenlei; Field private String score1; Field private String isbn; Field private String fenleiurl; Field private String mulu; Field private String isp; Field private String iep; public getters();/get方法 public setters();/set方法3, 同时开启四台服务器,运行上面代码:4,
11、 打开IE查询打开:http:/localhost/solr打开:http:/localhost:9888/solr打开http:/localhost:9008/solr打开http:/192.168.168.48:9888/solr四、Solr的Multicore(分片)配置bodybackground:#eee;margin:0;padding:0.examplebackground:#FFF;width:800px;border:1px #000 solid;margin:20px auto;padding:15px;-moz-border-radius: 3px;-webkit-bor
12、der-radius: 3px.example img margin:22px 六、一个简单的web引用: 首先说明的是索引来源,是根据已有的lucene索引上开发的,又因为lucene的索引直接用solrJ应用效果不好,会出现很多问题,找不到类似的解决办法,比如全字段查询,和高亮分组等,和多级索引目录。但用solrJ创建的索引不存在类似的问题. 大致的思路是,读取已有的lucene索引 ,再用solrJ创建索引并分发到多台机器上,最后再做开发. 第一步:读取lucene的多级索引目录,用solrJ创建和分发索引;需注意的是:要加大虚拟机的内存,因为采用的map做为缓存,理论上虚拟机的内存大,
13、map的存储的索引文档数也就多.主要是针对内存溢出. 代码 :package org.readerIndex;import org.apache.solr.client.solrj.beans.Field;public class BookIndex2 Field private String zjid; Field private String zhangjie; Field private String ssid; Field private String qwpos; Field private String publishDate; Field private String mulu;
14、 Field private String fenleiurl; Field private String fenlei; Field private String dxid; Field private String author; Field private String address; Field private String bookname; public String getZjid() return zjid; package org.readerIndex;import java.io.File;import java.io.IOException;import java.t
15、ext.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;import org.apache.lucene.document.Document;import org.apache.lucene.index.CorruptIndexException;import org.apache.lucene.index.IndexReader;import org.apache.lucene.store.LockObtainFailedException;import org.a
16、pache.solr.client.solrj.SolrServerException;import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;public class ReaderIndex public CommonsHttpSolrServer getServer(String hostUrl) CommonsHttpSolrServer server = null; try server = new CommonsHttpSolrServer(hostUrl); server.setConnectionTimeout
17、(100); server.setDefaultMaxConnectionsPerHost(100); server.setMaxTotalConnections(100); catch (IOException e) System.out.println(请检查tomcat服务器或端口是否开启!); return server; public void indexDocuements(String path,String hostUrls) throws CorruptIndexException, LockObtainFailedException, IOException File pa
18、reFile = new File(path); List list = new ArrayList(); getFile(pareFile,list); /递归方法得到路径保存到list中 System.out.println(*程序一共递归到+list.size()+个索引目录*); int arevageSize = list.size()/hostUrls.length;/ 根据主机数平分目录 int remainSize = list.size()%hostUrls.length;/取余 SimpleDateFormat sdf = new SimpleDateFormat(yyyy
19、-MM-dd HH:mm:ss); for(int i=0;ihostUrls.length;i+) Date startDate = new Date(); String url = hostUrlsi.substring(hostUrlsi.indexOf(/)+2,hostUrlsi.lastIndexOf(/); System.out.println(第+(i+1)+次,在主机:+url+ 上创建索引,创建时间 +sdf.format(startDate); if(i=(hostUrls.length-1) list(list,arevageSize*i,arevageSize*(i+
20、1)+remainSize,hostUrlsi); /* */(function ($) $.fn.imageLens = function (options) var defaults = lensSize: 100, borderSize: 4, borderColor: #888 ; var options = $.extend(defaults, options); var lensStyle = background-position: 0px 0px;width: + String(options.lensSize) + px;height: + String(options.le
21、nsSize) + px;float: left;display: none;border-radius: + String(options.lensSize / 2 + options.borderSize) + px;border: + String(options.borderSize) + px solid + options.borderColor + ;background-repeat: no-repeat;position: absolute; return this.each(function () obj = $(this); var offset = $(this).of
22、fset(); / Creating lens var target = $( ).appendTo($(this).parent(); var targetSize = target.size(); / Calculating actual size of image var imageSrc = options.imageSrc ? options.imageSrc : $(this).attr(src); var imageTag = ; var widthRatio = 0; var heightRatio = 0; $(imageTag).load(function ()
23、widthRatio = $(this).width() / obj.width(); heightRatio = $(this).height() / obj.height(); ).appendTo($(this).parent(); target.css( backgroundImage: url( + imageSrc + ) ); target.mousemove(setPosition); $(this).mousemove(setPosition); function setPosition(e) var leftPos = parseInt(e.pageX - offset.l
24、eft); var topPos = parseInt(e.pageY - offset.top); if (leftPos 0 | topPos 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: leftPos + px, top: topPos + px ); ); ;)(jQuery);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1