前期对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(); } } }
网友评论