一、前言
基于solrj(solr-solrj依赖包)的HttpSolrServer方式连接远程solr服务器,通过SolrInputDocument索引文档模型,提交新增或更新索引,这样通过该方式可以创建多个分布式并发任务去执行索引发布更新,相比于solr-dataimport的单线程依次执行创建,性能又了本质的提高。
二、代码示例
import java.util.Map; @b@import org.apache.solr.client.solrj.impl.HttpSolrServer;@b@import org.apache.solr.client.solrj.impl.XMLResponseParser;@b@import org.apache.solr.common.SolrInputDocument;@b@import com.xwood.engine.common.config.ExtConstants;@b@@b@public class SolrJIndexGenerator {@b@ @b@ private static String coreServerUrl=ExtConstants.getString("processer.index.subject.index.url");@b@ @b@ private static HttpSolrServer coreServer = new HttpSolrServer(coreServerUrl); @b@ @b@ static{@b@ @b@ coreServer.setMaxRetries(1); // defaults to 0. > 1 not recommended. @b@ coreServer.setConnectionTimeout(5000000); // 5 seconds to establish TCP @b@ @b@ coreServer.setParser(new XMLResponseParser()); @b@ @b@ coreServer.setSoTimeout(100000000); // socket read timeout @b@ coreServer.setDefaultMaxConnectionsPerHost(100); @b@ coreServer.setMaxTotalConnections(500); @b@ coreServer.setFollowRedirects(false); // defaults to false @b@ coreServer.setAllowCompression(true); @b@ }@b@ @b@ @b@ public static void submit(String[] values) throws Exception{@b@ @b@ SolrInputDocument doc1 = new SolrInputDocument(); @b@ @b@ doc1.addField( "id", values[0], 1.0f ); @b@ doc1.addField( "title",values[1], 1.0f ); @b@ doc1.addField( "create_by", values[2], 1.0f ); @b@ doc1.addField( "create_time", values[3], 1.0f ); @b@ doc1.addField( "title", values[4], 1.0f ); @b@ doc1.addField( "keywords", values[5], 1.0f ); @b@ @b@ coreServer.add(doc1); @b@ coreServer.commit(); @b@ @b@ logger.info(" 【indexDoc】 -----------------------------id:"+values[0]+"@title:"+values[4]);@b@ @b@ }@b@ @b@ public static void submit(Map<String,String> docs) throws Exception{@b@ @b@ SolrInputDocument doc1 = new SolrInputDocument(); @b@ @b@ for(String dk:docs.keySet()){@b@ doc1.addField( dk, docs.get(dk), 1.0f ); @b@ }@b@ coreServer.add(doc1); @b@ coreServer.commit(); @b@ logger.info(" 【indexDoc】 -----------------------------id:"+docs.get("id")+"@title:"+docs.get("title"));@b@ }@b@ @b@@b@}