首页

基于Solr的Dataimport开放的full-import的Url接口功能实现自动更新同步数据并发布索引更新

标签:solr,索引,搜索引擎,apache,同步,dataimport,full-import,定时     发布时间:2016-11-19   

本站搜索功能基于apachesolr搜索引擎实现内容的搜索和展现,之前每次发布索引都是手动点击(可参考配置发布相关页),通过系统绑定发布事务调用接口方法(可以在浏览器执行地址如“http://www.xwood.net/solr/subject/dataimport?command=full-import&commit=true&clean=true”),实例代码如下

import java.io.BufferedReader;@b@import java.io.IOException;@b@import java.io.InputStreamReader;@b@import java.net.HttpURLConnection;@b@import java.net.MalformedURLException;@b@import java.net.URL;@b@import org.apache.log4j.Logger;@b@@b@public class SolrIndexUpdater {@b@	@b@	private static final Logger log = Logger.getLogger(SolrIndexUpdater.class);@b@	@b@	private static String indexUrl = "http://www.xwood.net/solr/subject/dataimport?command=full-import&commit=true&clean=true";@b@	@b@	 @b@	public static void subject(){@b@		HttpURLConnection conn = null;@b@		try {@b@			conn = (HttpURLConnection) new URL(indexUrl).openConnection();@b@			conn.setRequestMethod("GET");@b@			conn.setRequestProperty("Content-type", "text/xml");@b@			conn.setDoOutput(true);@b@			BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));@b@			String lines="";@b@			String ss="";@b@			while((lines = in.readLine()) != null){@b@				ss+=lines;@b@			}@b@			System.out.println("subject index add success-------------------------------------:"+ss);@b@			conn.disconnect();@b@		} catch (MalformedURLException e) {@b@			e.printStackTrace();@b@		} catch (IOException e) {@b@			e.printStackTrace();@b@		} finally {@b@			if(conn != null) conn.disconnect();@b@		}@b@	}@b@		@b@	//测试@b@	public static void main(String[] arg){@b@		subject();@b@	}@b@@b@}

控制台执行结果如下

subject index add success-------------------------------------:@b@<?xml version="1.0" encoding="UTF-8"?>@b@    <response>@b@    <lst name="responseHeader">@b@        <int name="status">0</int>@b@        <int name="QTime">0</int>@b@    </lst>@b@    <lst name="initArgs">@b@        <lst name="defaults">@b@            <str name="config"> mysql-dsmconfig.xml</str>@b@        </lst>@b@    </lst>@b@    <str name="command">full-import</str>@b@    <str name="status">idle</str>@b@    <str name="importResponse"/><lst name="statusMessages">@b@    <str name="Total Requests made to DataSource">1</str>@b@    <str name="Total Rows Fetched">754</str>@b@    <str name="Total Documents Skipped">0</str>@b@    <str name="Full Dump Started">2016-11-18 23:52:56</str>@b@    <str name="">Indexing completed. Added/Updated: 754 documents. Deleted 0 documents.</str>@b@    <str name="Committed">2016-11-18 23:53:07</str>@b@    <str name="Total Documents Processed">754</str>@b@    <str name="Time taken">0:0:10.883</str>@b@    </lst>@b@    <str name="WARNING">This response format is experimental.  It is likely to change in the future.</str>@b@</response>