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(); } }
相关文章推荐
- Solr 5.3.0集成mmseg4j、tomcat部署、Solrj 5.3.0使用
- solrj操作solr
- 利用SolrJ操作solr API,完成index操作
- lucene教程 之建立,查找,删除索引
- SQL,数据库建表
- Oracle 索引
- T-SQL学习之索引
- Solr开发文档【推荐】
- solr 从零学习开始
- SolrJ的使用
- solr安装部署、solr测试创建core、用solrj 访问solr(索引和搜索)
- solrJ
- solr4.9.0本地数据提交异常分析
- solrj 对solr 的操作
- Solr/Solrj初探
- SOLR企业搜索平台 三 (schema.xml配置和solrj的使用)
- linux/window 下 solr5.1 tomcat7.x 环境搭建即简单功能测试
- 【MongoDB学习笔记22】MongoDB的索引管理
- MongoDB索引管理——创建索引,查看索引,删除索引,重建索引
- 单机版的solrj(查询)