七、通过java代码实现增量、全量索引

      前期对solr全文检索已经做了绝大部分的工作,现在剩下的就是如何让索引自动加载,包括全量加载及增量加载。

1.首先要实现自动的全量加载及增量加载就要通过定时任务来实现,这里定时任务的编写不在这里描述。

2.如下是我在项目中的全量、增量加载的代码:

package com.ultrapower.bpm.interfaces;

import java.io.IOException;
import java.util.Properties;

import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.log4j.Logger;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import com.ultrapower.bpm.process.action.ProcessAction;
import com.ultrapower.bpm.sheet.manage.PreemptiveAuthInterceptor;

public class AddSolrOperator {
	//======================================读取配置文件中solr的URL
	public static String SOLR_URL = "";
	public static String SOLR_IP = "";
	public static String SOLR_PORT = "";
	private static Logger log = Logger.getLogger(ProcessAction.class);
	
	static{
		try {
			Properties properties = PropertiesLoaderUtils.loadAllProperties("solrURL.properties");
			SOLR_URL = properties.getProperty("solr.url");
			SOLR_IP = properties.getProperty("solr.ip");
			SOLR_PORT = properties.getProperty("solr.port");
		} catch (IOException e) {
			log.error("读取solrURL.properties出现异常!!", e);
		}
	}
	
	//======================================
	
	
	
	// private static final String DEFAULT_URL = "http://localhost:8088/solr/";
	 
	// http://localhost:8088/solr/collection1/dataimport?command=delta-import&clean=false&commit=true&entity=up_knowledge&optimize=false
	 // http://localhost:8088/solr/collection1/dataimport?command=delta-import&clean=false&commit=true&optimize=false
	 public static HttpSolrServer solr=null ;
	 
	 @SuppressWarnings("unchecked")
	public synchronized void scanInterface() throws IOException{
		 solr= new HttpSolrServer(SOLR_URL);
			//====进行用户验证,因为tomcat中设定了用户验证,所以为了能连接服务器必需要验证用户,否则不能连通服务器
			 DefaultHttpClient m_client =(DefaultHttpClient)solr.getHttpClient(); 
			  UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("admin","admin");
			  m_client.addRequestInterceptor(new PreemptiveAuthInterceptor(),0);
			  (((DefaultHttpClient)m_client).getCredentialsProvider()).setCredentials(new AuthScope(SOLR_IP,Integer.parseInt(SOLR_PORT)), credentials);
	        //======
//			  AddSolrOperator add=new AddSolrOperator();
			  buildIndex(false);
	     SolrInputDocument doc = new SolrInputDocument();
	     try {
		  } catch (Exception e) {
			  e.printStackTrace();
		  }
	}
	 
	 
	 /**
	     * 增量/全量建立索引 。
	     * 
	     * @param delta false,增量建立索引;true,重建所有索引
	 * @throws IOException 
	     */
	    public static void buildIndex(boolean delta) throws IOException {
	        SolrQuery query = new SolrQuery();
	        // 指定RequestHandler,默认使用/select
	        query.setRequestHandler("/dataimport");

	        String command = delta ? "full-import" : "delta-import"; 
	        String clean = delta ? "true" : "false";
	        String optimize = delta ? "true" : "false";
	        
	        query.setParam("command", command)
	             .setParam("clean", false)
	             .setParam("commit", "true")
//	             .setParam("entity", "")
	             .setParam("optimize", optimize);
	        try {
	        	solr.query(query);
	        	solr.commit();
	        	System.out.println(" ///"+command);
	        } catch (SolrServerException e) {
	           e.printStackTrace();
	        }
	    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值