您的位置:首页 > 其它

solrJ对索引的删除操作

2016-03-10 16:11 639 查看
public class SolrUtils {

/**
* 创建solr服务器连接
* @param coreUrl
*            建立连接的solr服务器
* @return
*/
public static SolrServer createSolrServer(String coreUrl) {
HttpSolrServer solr = null;
try {
solr = new HttpSolrServer(coreUrl);
solr.setConnectionTimeout(100);
solr.setDefaultMaxConnectionsPerHost(100);
solr.setMaxTotalConnections(100);
} catch (Exception e) {
System.out.println("请检查tomcat服务器或端口是否开启!");
e.printStackTrace();
}
return solr;
}

/**
* 根据id从索引中删除记录
*
* @param server
* @param idName
*            主键名
* @param id
*            主键值
*/
public static void deleteById(SolrServer server, String idName, Object id) {
try {
server.deleteByQuery(idName + ":" + id.toString());
server.commit(false, false);
// LOG.info("Delete from index by id" + id +
// " finished . operate param is:" + idName + ":" + id.toString());
} catch (Exception e) {
// LOG.error("Delete from index by id" + id + " error, " +
// e.getMessage(), e);
}
}

/**
* 根据id集合从索引中删除记录
*
* @param server
* @param ids
*/
public static <T> void deleteByIds(SolrServer server, String idName,
List<T> ids) {
try {
if (ids.size() > 0) {
StringBuffer query = new StringBuffer(idName + ":" + ids.get(0));
for (int i = 1; i < ids.size(); i++) {
if (null != ids.get(i)) {
query.append(" OR " + idName + ":"
+ ids.get(i).toString());
}
}
server.deleteByQuery(query.toString());
server.commit(false, false);
// LOG.info("Delete from index by id list" + ids +
// " finished .");
} else {
// LOG.info("Delete ids list is null.");
}
} catch (Exception e) {
// LOG.error("Delete from index by id list" + ids + " error, " +
// e.getMessage(), e);
e.printStackTrace();
}
}

/**
* 根据查询从索引中删除
*
* @param server
* @param queryString
*/
public static void deleteByQuery(SolrServer server, String query) {
try {
server.deleteByQuery(query);
server.commit(false, false);
// LOG.info("Delete from index by query string " + query +
// "finished .");
} catch (Exception e) {
// LOG.error("Delete from index by query Strng " + query + "error, "
// + e.getMessage(), e);
e.printStackTrace();
}
}

/**
* 删除所有索引
*
* @param server
*/
public static void deleteAllIndex(SolrServer server) {
try {
server.deleteByQuery("*:*");
server.commit(false, false);
// LOG.info("All index delete finished.");
} catch (Exception e) {
// LOG.error("Delete all index error " + e.getMessage(), e);
e.printStackTrace();
}
}

/**
* 更新部分索引字段
*
* @param server
* @param id
*            待更新的文档id
* @param fieldValue
*            待更新的字段名
* @param fieldValue
*            要更新的字段值
* @throws IOException
* @throws SolrServerException
*/
public static void update(SolrServer server, String fieldName, String id,Object fieldValue) throws IOException, SolrServerException {
List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
HashMap<String, Object> oper = new HashMap<String, Object>();
oper.put("set", fieldValue);
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", id);
doc.addField(fieldName, oper);
docs.add(doc);
server.add(docs);
server.commit();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  solrJ 删除索引