backup-s-server02

import java.util.HashMap;

import java.util.List;

import java.util.Map;

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.common.SolrInputDocument;

import org.springframework.stereotype.Service;

@Service("solrServerService")

public class SolrServerServiceImpl implements SolrServerService{

/**

* 添加索引

* @param coreName

* @param obj

* @throws Exception

*/

public void add(String coreName, Object obj) throws Exception

{

if(obj == null)

{

return ;

}

SolrClient solrClient = SolrClientFactory.getSolrClient(coreName);

solrClient.addBean(obj);

solrClient.commit();

}

/**

* 根据id删除索引

* @param coreName

* @param id

* @throws Exception

*/

public void delete(String coreName, String id) throws Exception {

SolrClient solrClient = SolrClientFactory.getSolrClient(coreName);

solrClient.deleteById(id);

solrClient.commit();

}

/**

* 根据ID更新字段

* @param coreName

* @param list

*/

public void update(String coreName, List<UpdateData> list) throws Exception{

SolrClient solrClient = SolrClientFactory.getSolrClient(coreName);

for(UpdateData ud : list)

{

SolrInputDocument sd = new SolrInputDocument();

for(Data data: ud.datas){

Map<String, Object> map = new HashMap<String, Object>();

if(data.isIncrease)

{

map.put("inc", data.getValue());

}

else

{

map.put("set", data.getValue());

}

sd.addField(data.getFieldName(), map);

}

sd.addField("id", ud.getIdValue());

solrClient.add(sd);

solrClient.commit();

}

}

/**

* Atomic update index

* @param coreName

* @param list

* @throws Exception

*/

public void updateValues(String coreName, List<SolrInputDocument> list) throws Exception{

SolrClient solrClient = SolrClientFactory.getSolrClient(coreName);

for(SolrInputDocument model:list)

{

solrClient.add(model);

}

solrClient.commit();

}

/**

* 批量添加

* @param coreName

* @param list

* @throws Exception

*/

@SuppressWarnings("rawtypes")

public void batchAdd(String coreName, List list) throws Exception {

SolrClient solrClient = SolrClientFactory.getSolrClient(coreName);

solrClient.addBeans(list);

solrClient.commit();

}

/**

* 批量删除

* @param coreName

* @param idList

* @throws Exception

*/

public void batchDelete(String coreName, List<String> idList) throws Exception {

SolrClient solrClient = SolrClientFactory.getSolrClient(coreName);

solrClient.deleteById(idList);

solrClient.commit();

}

/**

* Solr search API

* @param coreName

* @param query

* @return

* @throws Exception

*/

public QueryResponse search(String coreName, SolrQuery query) throws Exception {

SolrClient solrClient = SolrClientFactory.getSolrClient(coreName);

return solrClient.query(query);

}

}

猜你喜欢

转载自wingoal.iteye.com/blog/2289295