package com.yihu.base; import org.apache.commons.collections.map.HashedMap; import org.apache.solr.client.solrj.SolrClient; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.client.solrj.response.UpdateResponse; import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocumentList; import org.apache.solr.common.SolrInputDocument; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; import java.util.Map; /** * Solr底层操作类 * * @author hzp * @version 1.0 * @created 2017.05.06 */ @Service @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) public class SolrAdmin { @Autowired SolrPool pool; /************************* 基础操作 **************************************************/ /** * 新建单条索引 */ public Boolean create(String core,Map map) throws Exception { SolrClient client = pool.getConnection(core); SolrInputDocument doc = new SolrInputDocument(); //注意date的格式,要进行适当的转化 for(String key:map.keySet()) { doc.addField(key, map.get(key)); } UpdateResponse re = client.add(doc); client.commit(); pool.close(core); //释放连接 if(re.getStatus()!=0) { System.out.print("create index cost " + re.getQTime()); return true; } else{ System.out.print("create index faild!"); return false; } } /** * 修改单条索引单字段 */ public Boolean update(String core,String uniqueKey,String uniqueKeyValue,String key,Object value) throws Exception { Map map = new HashedMap(); map.put(key,value); return update(core,uniqueKey+":"+uniqueKeyValue,map); } /** * 修改索引多字段 */ public Boolean update(String core,String keyQuery,Map map) throws Exception { SolrClient client = pool.getConnection(core); QueryResponse qr = client.query(new SolrQuery(keyQuery)); SolrDocumentList docs = qr.getResults(); if(docs!=null && docs.size()>0) { List solrList = new ArrayList<>(); for(int i=0;i