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);

}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值