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{

 

/**

* add index

* @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();

}

 

/**

* delete index by 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();

}

 

/**

* Update field based on 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();

 

}

 

/**

* Bulk add

* @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();

}

 

/**

* batch deletion

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

}

}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326989906&siteId=291194637