/**
* @author zhipeng
* @date 创建时间:2015-10-10 下午12:15:35
* @parameter
* @return
*/
public class SolrServer {
private static SolrServer solrServer = null;
private static HttpSolrServer server=null;
private static Map<Object, Object> proMap = new HashMap<Object, Object>();
private static Logger log = Logger.getLogger(SolrServer.class);
public static synchronized SolrServer getInstance() {
if (solrServer==null){
solrServer=new SolrServer();
}
return solrServer;
}
static{
Properties properties = new Properties();
try {
properties.load(SolrServer.class
.getResource("/solrparams.properties").openStream());
} catch (IOException e) {
log.error("IOException has been cactched SolrServer solrparams.properties读取配置错误", e);
}
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
Object key = entry.getKey();
Object value = entry.getValue();
//log.debug("---> key :" +key +" : value" +value);
proMap.put(key, value);
}
}
public static HttpSolrServer getServer(String domain){
try {
if(server != null && server.getBaseURL() != null && server.getBaseURL().indexOf(domain)>0){
log.debug("加载solr的URL:"+server.getBaseURL());
}else{
String solrurl=(String) proMap.get("solrurl")+domain;
server = new HttpSolrServer(solrurl);
server.setSoTimeout(1000*60); // socket read timeout
server.setConnectionTimeout(1000*60); //1分钟的提交时间 防止提交超时
server.setDefaultMaxConnectionsPerHost(100);
server.setMaxTotalConnections(100);
server.setFollowRedirects(false); // defaults to false
//allowCompression defaults to false.
//Server side must support gzip or deflate for this to have any effect.
server.setAllowCompression(true);
server.setMaxRetries(1); // defaults to 0. > 1 not recommended.
}
} catch (Exception e) {
log.debug("SolrServer getServer 获取solr服务错误"+e);
}
return server;
}
}
java 的工具类 solrUtil
springMVC 在bean 启动完后执行方法
达人 expert
@Service
public class ExpertServiceImpl implements IExpertService,
ApplicationListener<ContextRefreshedEvent> {
// 获取连接服务
@SuppressWarnings("static-access")
private static HttpSolrServer solrServer = SolrServer.getInstance()
.getServer();
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
Pagination page = new Pagination();
Map<String, Object> query = new HashMap<String, Object>();
//查询标识 expertId:* solr数据是否为空
query.put("expertId", "*");
List<Expert> expertList = searchExpertSolr(page,query);
if(expertList.size() <= 0){
//如果容器第一次启动
if (event.getApplicationContext().getParent() == null) {
List<ExpertPO> expertsList = interStorage.getExpertList();
for (ExpertPO experts : expertsList) {
SolrInputDocument doc1 = new SolrInputDocument();
Expert expert = ExpertModelUtil.expertPO2BO(experts);
writeExpertSolr(doc1,expert);
ExpertResume expertResume = ExpertModelUtil
.expertPO2ResumeBO(experts);
writeExpertResumeSolr(doc1,expertResume);
ExpertStore expertStore = ExpertModelUtil
.expertPO2StoreBO(experts);
doc1 = writeExpertStoreSolr(doc1,expertStore);
commitSolr(doc1);
}
}
}
}
/**
* 达人信息 写入到solr writerSolr()
*
* @param
* @return
* @author zhipeng
* @Time 2015-10-12 上午11:50:55
*/
private static SolrInputDocument writeExpertSolr(SolrInputDocument doc1,Expert expert) {
doc1.addField("id", expert.getUserId());
doc1.addField("userId", expert.getUserId());
doc1.addField("expertId", expert.getUserId());
doc1.addField("realName", expert.getRealName());
doc1.addField("mobile", expert.getMobile());
doc1.addField("showable", expert.getShowable());
doc1.addField("expertMeetCount", expert.getExpertMeetCount());
doc1.addField("email", expert.getEmail());
doc1.addField("expertGrade", expert.getExpertGrade());
doc1.addField("activityArea", expert.getActivityArea());
doc1.addField("institutions", expert.getInstitutions());
doc1.addField("relatedLinks", expert.getRelatedLinks());
doc1.addField("residentCity", expert.getResidentCity());
doc1.addField("position", expert.getPosition());
doc1.addField("publicAccount", expert.getPublicAccount());
doc1.addField("workingYears", expert.getWorkingYears());
doc1.addField("portrait", expert.getPortrait());
doc1.addField("auditFailReason", expert.getAuditFailReason());
doc1.addField("usableStatus", expert.getUsableStatus());
doc1.addField("auditStatus", expert.getAuditStatus());
doc1.addField("name", expert.getName());
doc1.addField("payType", expert.getPayType());
doc1.addField("payAccount", expert.getPayAccount());
doc1.addField("expertIdNumber", expert.getExpertIdNumber());
doc1.addField("expertCharge", expert.getExpertCharge());
return doc1;
}
/**
* 达人简介 写入到 solr writeExpertResumeSolr()
*
* @param
* @return
* @author zhipeng
* @Time 2015-10-12 下午2:17:14
*/
private static SolrInputDocument writeExpertResumeSolr(SolrInputDocument doc1,ExpertResume expertResume) {
doc1.addField("expertprofile", expertResume.getExpertprofile());
doc1.addField("topicDescription", expertResume.getTopicDescription());
return doc1;
}
/**
* 达人店铺 写入到solr writeExpertStoreSolr()
*
* @param
* @return
* @author zhipeng
* @Time 2015-10-12 下午2:21:45
*/
private static SolrInputDocument writeExpertStoreSolr(SolrInputDocument doc1,ExpertStore expertStore) {
doc1.addField("webBackground", expertStore.getWebBackground());
doc1.addField("appBackground", expertStore.getAppBackground());
doc1.addField("PersonalBackground", expertStore.getPersonalBackground());
doc1.addField("workTime", expertStore.getWorkTime());
doc1.addField("responseFrequency", expertStore.getResponseFrequency());
return doc1;
}
/**
* 提交solr commitSolr()
*
* @param
* @return
* @author zhipeng
* @Time 2015-10-12 下午6:15:34
*/
private static void commitSolr(SolrInputDocument doc1) {
try {
solrServer.add(doc1);
solrServer.commit();
} catch (SolrServerException | IOException e) {
log.debug(ExpertServiceImpl.class + "commitSolr()"+e);
}
}
/**
* 返回solr 查询的 结果集 getResponse()
* @param
* @return
* @author zhipeng
* @Time 2015-10-12 上午11:45:55
*/
private static QueryResponse getResponse(Pagination page, Map<String, Object> query) {
SolrQuery sQuery = new SolrQuery();
sQuery.setQuery(makeSearchQuery(query));
// 设置分页 PageNum=0就是从0开始,,size=5当前返回5条记录,第二页就是变化PageNum这个值为5就可以了。
sQuery.setStart((page.getPageNum() - 1) * page.getPageSize());
sQuery.setRows(page.getPageSize());
QueryResponse response = null;
try {
response = solrServer.query(sQuery);
} catch (SolrServerException e) {
log.debug(ExpertServiceImpl.class + "getResponse()" +e);
}
return response;
}
/**
* 构造solr 查询的条件参数
* makeSearchQuery()
* @param
* @return
* @author zhipeng
* @Time 2015-10-13 下午4:28:00
*/
private static String makeSearchQuery(Map<String, Object> query){
StringBuffer searchQuery = new StringBuffer();
if (query.containsKey("userId")) {
searchQuery.append("userId:" + query.get("userId"));
}
if (query.containsKey("realName")) {
if (StringUtils.isNotBlank(searchQuery.toString())) {
searchQuery.append(" AND realName:" + query.get("realName"));
} else {
searchQuery.append("realName:" + query.get("realName"));
}
}
if (query.containsKey("mobile")) {
if (StringUtils.isNotBlank(searchQuery.toString())) {
searchQuery.append(" AND mobile:" + query.get("mobile"));
} else {
searchQuery.append("mobile:" + query.get("mobile"));
}
}
if (query.containsKey("auditStatus")) {
if (StringUtils.isNotBlank(searchQuery.toString())) {
searchQuery.append(" AND auditStatus:"+ query.get("auditStatus"));
} else {
searchQuery.append("auditStatus:" + query.get("auditStatus"));
}
}
if (query.containsKey("usableStatus")) {
if (StringUtils.isNotBlank(searchQuery.toString())) {
searchQuery.append(" AND usableStatus:"+ query.get("usableStatus"));
} else {
searchQuery.append("usableStatus:" + query.get("usableStatus"));
}
}
if (query.containsKey("expertId")) {
if (StringUtils.isNotBlank(searchQuery.toString())) {
searchQuery.append(" AND expertId:*");
} else {
searchQuery.append("expertId:*");
}
}
if (query.size() <= 0) {
searchQuery.append("*:*");
}
return searchQuery.toString();
}
@Override
public List<Expert> searchExpertSolr(Pagination page,
Map<String, Object> query) {
List<Expert> expertList = new ArrayList<Expert>();
QueryResponse response = getResponse(page, query);
SolrDocumentList list = response.getResults();
for (SolrDocument solrDocument : list) {
Expert expert = new Expert(solrDocument);
expertList.add(expert);
}
return expertList;
}
@Override
public List<ExpertResume> searchExpertResumeSolr(Pagination page,
Map<String, Object> query) {
QueryResponse response = getResponse(page, query);
List<ExpertResume> expertResumeList = new ArrayList<ExpertResume>();
SolrDocumentList list = response.getResults();
for (SolrDocument solrDocument : list) {
ExpertResume expertResume = new ExpertResume(solrDocument);
expertResumeList.add(expertResume);
}
return expertResumeList;
}
@Override
public List<ExpertStore> searchExpertStoreSolr(Pagination page,
Map<String, Object> query) {
QueryResponse response = getResponse(page, query);
List<ExpertStore> expertStoreList = new ArrayList<ExpertStore>();
SolrDocumentList list = response.getResults();
for (SolrDocument solrDocument : list) {
ExpertStore expertStore = new ExpertStore(solrDocument);
expertStoreList.add(expertStore);
}
return expertStoreList;
}
}
查询 topicId 的 solr 各种条件拼接
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
Pagination page = new Pagination();
Map<String, Object> query = new HashMap<String, Object>();
query.put("topicId","*");
List<Topic> topicList = searchTopicSolr(page, query);
if (topicList.size() <= 0) {
if (event.getApplicationContext().getParent() == null) {
List<TopicActivity> topicActivityList = topicDBStorage
.getTopicActivityList();
for (TopicActivity topicActivity : topicActivityList) {
SolrInputDocument doc1 = new SolrInputDocument();
Topic topic = TopicModelUtil
.topicActivity2Topic(topicActivity);
writeTopicSolr(doc1, topic);
ExpertActivity activity = TopicModelUtil.topicActivity2Activity(topicActivity);
writeActivitySolr(doc1, activity);
commitSolr(doc1);
}
}
}
}
/**
* 提交solr commitSolr()
*
* @param
* @return
* @author zhipeng
* @Time 2015-10-12 下午6:15:34
*/
public static void commitSolr(SolrInputDocument doc1) {
try {
solrServer.add(doc1);
solrServer.commit();
} catch (SolrServerException | IOException e) {
log.debug(TopicServiceImpl.class + "commitSolr()"+e);
}
}
/**
* 话题 写入 sol
* writeTopicSolr()
* @param
* @return
* @author zhipeng
* @Time 2015-10-14 上午10:09:48
*/
public static SolrInputDocument writeTopicSolr(SolrInputDocument doc1,Topic topic){
doc1.addField("id", topic.getId());
doc1.addField("topicId",topic.getId());
doc1.addField("title", topic.getTitle());
doc1.addField("content", topic.getContent());
doc1.addField("publisherId", topic.getPublisherId());
doc1.addField("publisher", topic.getPublisher());
doc1.addField("publishTime", topic.getPublishTime());
doc1.addField("price", topic.getPrice());
doc1.addField("keywords", topic.getKeywords());
doc1.addField("auditStatus", topic.getAuditStatus());
doc1.addField("activestatus", topic.getActivestatus());
doc1.addField("topicType", topic.getTopicType());
doc1.addField("requirement", topic.getRequirement());
doc1.addField("duration", topic.getDuration());
//doc1.addField("categoryIds", topic.getCategoryIds());
doc1.addField("sort", topic.getSort());
doc1.addField("collectionCount", topic.getCollectionCount());
doc1.addField("description", topic.getDescription());
doc1.addField("categoryIds",topic.getTopicCategoryIds());
return doc1;
}
public static SolrInputDocument writeActivitySolr(SolrInputDocument doc1,ExpertActivity activity){
doc1.addField("contant", activity.getContant());
doc1.addField("userCountUpper", activity.getUserCountUpper());
doc1.addField("userCountLower", activity.getUserCountLower());
doc1.addField("beginTime", activity.getBeginTime());
doc1.addField("endTime", activity.getEndTime());
doc1.addField("address", activity.getAddress());
doc1.addField("activityDescription", activity.getDescription());
doc1.addField("applyBeginTime", activity.getApplyBeginTime());
doc1.addField("applyEndTime", activity.getApplyEndTime());
return doc1;
}
/**
* 构造solr 查询的条件参数
* makeSearchQuery()
* @param
* @return
* @author zhipeng
* @Time 2015-10-13 下午4:28:00
*/
public static String makeSearchQuery(Map<String, Object> query){
StringBuffer searchQuery = new StringBuffer();
if(query.containsKey("id")){
searchQuery.append("id:" + query.get("id"));
}
if (query.containsKey("auditStatus")) {
if(StringUtils.isNotBlank(searchQuery.toString())){
searchQuery.append(" AND auditStatus:" + query.get("auditStatus"));
}else{
searchQuery.append("auditStatus:" + query.get("auditStatus"));
}
}
if(query.containsKey("activityStatus")){
if(StringUtils.isNotBlank(searchQuery.toString())){
searchQuery.append(" AND activityStatus:" + query.get("activityStatus"));
}else{
searchQuery.append("activityStatus:" + query.get("activityStatus"));
}
}
if (query.containsKey("title")) {
if (StringUtils.isNotBlank(searchQuery.toString())) {
searchQuery.append(" AND title:"+"*" + query.get("title")+"*");
} else {
searchQuery.append("title:" +"*"+ query.get("title")+"*");
}
}
if (query.containsKey("publisher")) {
if (StringUtils.isNotBlank(searchQuery.toString())) {
searchQuery.append(" AND publisher:"+"*"+query.get("publisher")+"*");
} else {
searchQuery.append("publisher:" +"*"+ query.get("publisher")+"*");
}
}
if (query.containsKey("publisherId")) {
if (StringUtils.isNotBlank(searchQuery.toString())) {
searchQuery.append(" AND publisherId:" + query.get("publisherId"));
} else {
searchQuery.append("publisherId:" + query.get("publisherId"));
}
}
if (query.containsKey("topicType")) {
if (StringUtils.isNotBlank(searchQuery.toString())) {
searchQuery.append(" AND topicType:" + query.get("topicType"));
} else {
searchQuery.append("topicType:" + query.get("topicType"));
}
}
if (query.containsKey("category")) {
if (StringUtils.isNotBlank(searchQuery.toString())) {
searchQuery.append(" AND categoryIds:" + query.get("categoryIds"));
} else {
searchQuery.append("categoryIds:" + query.get("categoryIds"));
}
}
if (query.containsKey("keyword")) {
if (StringUtils.isNotBlank(searchQuery.toString())) {
searchQuery.append(" AND ( title:*" + query.get("keyword")+"*");
searchQuery.append(" OR publisher:*" + query.get("keyword")+"*)");
} else {
searchQuery.append("title:*" + query.get("keyword")+"*");
searchQuery.append(" OR publisher:*" + query.get("keyword")+"*");
}
}
if (query.containsKey("topicId")) {
if (StringUtils.isNotBlank(searchQuery.toString())) {
searchQuery.append(" AND topicId:*");
} else {
searchQuery.append("topicId:*");
}
}
if(query.containsKey("publisherIdList")){
@SuppressWarnings("unchecked")
List<Long> userIdList = (List<Long>) query.get("publisherIdList");
for (int i = 0; i < userIdList.size(); i++) {
if(i != 0){
if (StringUtils.isNotBlank(searchQuery.toString())) {
searchQuery.append(" OR publisherId:"+userIdList.get(i));
}
}else{
if (StringUtils.isNotBlank(searchQuery.toString())) {
searchQuery.append(" AND ( publisherId:"+userIdList.get(i));
}else{
searchQuery.append(" ( publisherId:"+userIdList.get(i));
}
}
}
searchQuery.append(" )");
}
if (query.size() <= 0) {
searchQuery.append("*:*");
}
return searchQuery.toString();
}
/**
* 返回solr 查询的 结果集 getResponse() find 排序
* @param
* @return
* @author zhipeng
* @Time 2015-10-12 上午11:45:55
*/
public static QueryResponse getFindResponse(Pagination page,Map<String,Object> query) {
SolrQuery sQuery = new SolrQuery();
sQuery.setQuery(makeSearchQuery(page.getQuery()));
// 设置分页 PageNum=0就是从0开始,,size=5当前返回5条记录,第二页就是变化PageNum这个值为5就可以了。
sQuery.setStart((page.getPageNum() - 1) * page.getPageSize());
sQuery.setRows(page.getPageSize());
if(query!=null){
if(query.containsKey("sort")){
sQuery.addSort(new SortClause("publishTime",ORDER.desc));
}else{
sQuery.addSort(new SortClause("sort",ORDER.asc));
sQuery.addSort(new SortClause("publishTime",ORDER.desc));
sQuery.addSort(new SortClause("auditStatus",ORDER.asc));
}
}
QueryResponse response = null;
try {
response = solrServer.query(sQuery);
} catch (SolrServerException e) {
log.debug(TopicServiceImpl.class + "getResponse()" +e);
}
return response;
}
/**
* 返回solr 查询的 结果集 getResponse() findByCategory 排序
* @param
* @return
* @author zhipeng
* @Time 2015-10-12 上午11:45:55
*/
public static QueryResponse getResponse(Pagination page,Map<String,Object> query) {
SolrQuery sQuery = new SolrQuery();
Map<String,Object> pageQuery=page.getQuery();
pageQuery.put("topicId", query.get("topicId"));
sQuery.setQuery(makeSearchQuery(page.getQuery()));
// 设置分页 PageNum=0就是从0开始,,size=5当前返回5条记录,第二页就是变化PageNum这个值为5就可以了。
sQuery.setStart((page.getPageNum() - 1) * page.getPageSize());
sQuery.setRows(page.getPageSize());
if(query!=null){
if(query.containsKey("sortBy")){
int sortIndex =Integer.parseInt(query.get("sortBy").toString());
String sortType="";
if(query.containsKey("sort")){
sortType =query.get("sort").toString();
}
//默认排序
if(sortIndex==1){
sQuery.addSort(new SortClause("sort",ORDER.asc));
sQuery.addSort(new SortClause("publishtime",ORDER.desc));
}
//收藏数量排序
else if(sortIndex == 2){
if(StringUtils.isBlank(sortType)){
sortType ="asc";
}
if("asc".equals(sortType)){
sQuery.addSort(new SortClause("collectioncount",ORDER.asc));
}else{
sQuery.addSort(new SortClause("collectioncount",ORDER.desc));
}
}
//根据创建时间
else if(sortIndex ==3){
if(StringUtils.isBlank(sortType)){
sortType ="desc";
}
if("desc".equals(sortType)){
sQuery.addSort(new SortClause("publishtime",ORDER.desc));
}else{
sQuery.addSort(new SortClause("publishtime",ORDER.asc));
}
}
//价格排序
else if(sortIndex==4){
if(StringUtils.isBlank(sortType)){
sortType ="asc";
}
if("asc".equals(sortType)){
sQuery.addSort(new SortClause("price",ORDER.asc));
}else{
sQuery.addSort(new SortClause("price",ORDER.desc));
}
}
}
}
QueryResponse response = null;
try {
response = solrServer.query(sQuery);
} catch (SolrServerException e) {
log.debug(TopicServiceImpl.class + "getResponse()" +e);
}
return response;
}
public static List<Topic> searchTopicSolr(Pagination page,Map<String,Object> query){
QueryResponse response = getResponse(page,query);
List<Topic> topicList = new ArrayList<Topic>();
SolrDocumentList list = response.getResults();
for (SolrDocument solrDocument : list) {
Topic topic = new Topic(solrDocument);
ExpertActivity activity = new ExpertActivity(solrDocument);
topic.setExpertActivity(activity);
topic.setUserCount(activity.getUserCountLower()+"--"+activity.getUserCountUpper());
topicList.add(topic);
}
return topicList;
}
public static List<Topic> searchTopicFindSolr(Pagination page,Map<String,Object> query){
QueryResponse response = getFindResponse(page,query);
List<Topic> topicList = new ArrayList<Topic>();
SolrDocumentList list = response.getResults();
for (SolrDocument solrDocument : list) {
Topic topic = new Topic(solrDocument);
ExpertActivity activity = new ExpertActivity(solrDocument);
topic.setExpertActivity(activity);
topicList.add(topic);
}
return topicList;
}
public static List<ExpertActivity> searchActivitySolr(Pagination page,Map<String,Object> query){
QueryResponse response = getResponse(page,query);
List<ExpertActivity> activityList = new ArrayList<ExpertActivity>();
SolrDocumentList list = response.getResults();
for (SolrDocument solrDocument : list) {
ExpertActivity activity = new ExpertActivity(solrDocument);
activityList.add(activity);
}
return activityList;
}
转载于:https://blog.51cto.com/hackerxian/1704614