|
package com.urwork.tools.solr;
import com.urwork.tools.page.Page;
import org.apache.commons.collections.CollectionUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.CloudSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
/** * Created by xxx on xxxx/xx/xx.
*/
public> private CloudSolrClient server=null;
/**
* 构造函数
* @param zkAddr zk地址:192.168.2.233:2181,192.168.2.234:2181,192.168.2.235:2181
* @param collection collection名字: company
*/
private SolrCloudClient(String zkAddr,String collection) {
server = new CloudSolrClient.Builder().withZkHost(zkAddr).build();
server.setDefaultCollection(collection);
}
/**
* 删除集合中的所有数据
* @throws Exception
*/
public void deleteAll() throws Exception {
server.deleteByQuery("*:*");
server.commit();
}
/**
* 根据id删除集合中的数据
* @param> * @throws Exception
*/
public void deleteById(String> server.deleteById(id);
server.commit();
}
/**
* 根据ids删除集合中的数据
* @param> * @throws Exception
*/
public void deleteByIds(List<String>> server.deleteById(ids);
server.commit();
}
/**
* 批量更新索引
* @param docs
* @param <T>
* @throws Exception
*/
public <T> void addList(List<T> docs) throws Exception {
if (CollectionUtils.isEmpty(docs)){
return;
}
List<SolrInputDocument> list = new ArrayList<>();
for (T doc:docs){
Field[] declaredFields = doc.getClass().getDeclaredFields();
SolrInputDocument sid = new SolrInputDocument();
for (Field field : declaredFields){
field.setAccessible(true);
String name = field.getName();
Object value = field.get(doc);
if (value!=null){
sid.addField(name,value);
}
list.add(sid);
}
}
server.add(list);
server.commit();
}
/**
* 添加单条索引
* @param doc
* @param <T>
* @throws Exception
*/
public <T> void add(T doc) throws Exception {
List<T> docs = new ArrayList<>();
docs.add(doc);
addList(docs);
}
/**
* 更新一条索引
* @param doc
* @param <T>
* @throws Exception
*/
public <T> void update(T doc)throws Exception{
Field> if (idField == null){
throw new RuntimeException("your document doesn't have> }
idField.setAccessible(true);
Object> deleteById(id+"");
add(doc);
}
/**
* 批量更新索引
* @param docs
* @param <T>
* @throws Exception
*/
public <T> void updateList(List<T> docs) throws Exception {
if (CollectionUtils.isEmpty(docs)){
return;
}
List<String>> for (T doc : docs){
Field> if (idField!=null){
idField.setAccessible(true);
Object> ids.add(id+"");
}
}
deleteByIds(ids);
addList(docs);
}
/**
* 执行查询
* @param clazz 对应的class
* @param page Page分页
* @param query 查询条件
* @param <T>
* @return
* @throws Exception
*/
public <T> Page<T> search(Class<T> clazz,Page<T> page, SolrQuery query) throws Exception {
query.setStart((page.getCurrentPageNo()-1)*page.getPageSize());
query.setRows(page.getPageSize());
QueryResponse response = server.query(query);
SolrDocumentList results = response.getResults();
List<T> rtnList = new ArrayList<>();
for (SolrDocument doc : results){
T instance = clazz.newInstance();
Field[] declaredFields = instance.getClass().getDeclaredFields();
for (Field filed : declaredFields){
filed.setAccessible(true);
String name = filed.getName();
Object fieldValue =doc.getFieldValue(name);
filed.set(instance,fieldValue);
}
rtnList.add(instance);
}
page.setResult(rtnList);
page.setTotalRecord((int)results.getNumFound());
return page;
}
public void close() throws IOException {
server.close();
}
} |
|
|