solrJ项目开发的示例分析
solrJ项目开发的示例分析,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
“专业、务实、高效、创新、把客户的事当成自己的事”是我们每一个人一直以来坚持追求的企业文化。 成都创新互联是您可以信赖的网站建设服务商、专业的互联网服务提供商! 专注于网站制作、网站建设、软件开发、设计服务业务。我们始终坚持以客户需求为导向,结合用户体验与视觉传达,提供有针对性的项目解决方案,提供专业性的建议,创新互联建站将不断地超越自我,追逐市场,引领市场!
solrJ操作类 SolrService
package com.youxuepai.res.assistant.service.solr; import com.youxuepai.res.assistant.dto.solr.BasicQueryParamDTO; import java.io.IOException; 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.SolrServerException; import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.common.SolrInputDocument; /** * Interface SolrService ... solr操作教辅资源 * * @author gaobo * @group * @date 2019/8/22 */ public interface SolrService { /** * solr添加List对象列表 */
void saveOrUpdate(List beans, SolrClient client); /** * solr添加Obj对象 */ void saveOrUpdate(T beans, SolrClient client); /** * 原子更新solr * 只根据ID更新部分字段 * @param bean * @param client * @param */ void atmoUpdate(T bean, SolrClient client); /** * solr根据Id删除对象索引 * @param id * @return */ boolean deleteById(String id,SolrClient client); /** * 转换javabean对象为查询params. * * @param bean 添加入solr的实体类 */ SolrQuery ConvertQueryParam(T bean); SolrInputDocument beanToSolrMap(T bean); /** * 查询solr数据 * * @param client solr实例 * @param queryParam 过滤参数bean * @param bean 基础查询信息bean */ QueryResponse queryByBean( SolrClient client, BasicQueryParamDTO queryParam, T bean) throws IOException, SolrServerException; /** * 查询solr数据 * * @param client solr实例 * @param queryParam 过滤参数bean */ QueryResponse queryByBean( SolrClient client, BasicQueryParamDTO queryParam ) throws IOException, SolrServerException; /** * 查询solr数据 * * @param client solr实例 * @param bean 基础查询信息bean */ QueryResponse queryByBean( SolrClient client, T bean) throws IOException, SolrServerException; } solrJ操作类 SolrServiceImpl
package com.youxuepai.res.assistant.service.solr.impl; import com.youxuepai.res.assistant.dto.solr.BasicQueryParamDTO; import com.youxuepai.res.assistant.service.solr.SolrService; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import org.apache.solr.client.solrj.SolrClient; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrQuery.ORDER; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.beans.DocumentObjectBinder; import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.common.SolrInputDocument; import org.apache.solr.common.SolrInputField; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; /** * 类 SolrServiceImpl 公共添加solr方法 * * @author gaobo * @group * @date 2019/8/22 */ @Service public class SolrServiceImpl implements SolrService { Logger logger = LoggerFactory.getLogger(SolrServiceImpl.class); DocumentObjectBinder binder = new DocumentObjectBinder(); /** * solr添加对象列表 */ @Override public
void saveOrUpdate(List beans, SolrClient client) { for (T bean : beans) { saveOrUpdate(bean, client); } try { client.commit(); } catch (SolrServerException | IOException e) { logger.error("保存文档出错," + beans.size(), e); throw new RuntimeException(e.getMessage()); } } @Override public void saveOrUpdate(T beans, SolrClient client) { SolrInputDocument doc = binder.toSolrInputDocument(beans); try { client.add(doc); client.commit(); } catch (Exception e) { logger.error("保存文档出错," + doc.toString(), e); throw new RuntimeException(e.getMessage()); } } final String PRIMARY_KEY = "assistantResourceId"; /** * 原子更新solr * 只根据ID更新部分字段 * @param bean * @param client * @param */ @Override public void atmoUpdate(T bean, SolrClient client) { SolrInputDocument doc = beanToSolrMap(bean); try { client.add(doc); client.commit(); } catch (Exception e) { logger.error("保存文档出错," + doc.toString(), e); throw new RuntimeException(e.getMessage()); } } /** * solr根据Id删除对象索引 * @param id * @return */ @Override public boolean deleteById(String id,SolrClient client) { try { client.deleteById(id); client.commit(); } catch (Exception e) { e.printStackTrace(); return false; } return true; } /** * 转换javabean对象为查询params. * * @param bean 添加入solr的实体类 */ @Override public SolrQuery ConvertQueryParam(T bean) { Map paramMap = beanToMap(bean); // 添加默认参数 SolrQuery solrQuery = new SolrQuery(); paramMap.entrySet().forEach( filterMap -> solrQuery.addFilterQuery(filterMap.getKey() + ":" + filterMap.getValue()) ); return solrQuery; } private Map beanToMap(T bean){ SolrInputDocument doc = binder.toSolrInputDocument(bean); Collection values = doc.values(); return values.stream() .filter(entity -> entity.getValue() != null && !entity.getValue().equals("")) .collect(Collectors.toMap(map -> map.getName(), map -> map.getValue())); } @Override public SolrInputDocument beanToSolrMap(T bean) { Map paramMap = beanToMap(bean); SolrInputDocument inputDoc = new SolrInputDocument(); for (String field : paramMap.keySet()) { Map fieldMap = new HashMap<>(16); if (field.equals(PRIMARY_KEY)) { inputDoc.setField(PRIMARY_KEY, paramMap.get(field)); } else { fieldMap.put("set", paramMap.get(field)); inputDoc.setField(field, fieldMap); } } return inputDoc; } /** * 查询solr数据 * @param client solr实例 * @param queryParam 过滤参数bean * @param bean 基础查询信息bean * @param * @return * @throws IOException * @throws SolrServerException */ @Override public QueryResponse queryByBean(SolrClient client, BasicQueryParamDTO queryParam, T bean) throws IOException, SolrServerException { SolrQuery solrQuery = new SolrQuery(); if (bean != null) { solrQuery = this.ConvertQueryParam(bean); } solrQuery.set("q", "*:*"); if (queryParam != null) { boolean isKeywords = queryParam.getKeywords() != null; boolean isPage = queryParam.getPageNo() != null && queryParam.getPageSize() != null; boolean isRank = queryParam.getRankParm()!=null; // 搜索关键字是否有值 if (isKeywords) { solrQuery.remove("q"); solrQuery.set("q", "content:" + "*" + queryParam.getKeywords() + "*"); } //是否分页 if (isPage) { solrQuery.setStart((queryParam.getPageNo() - 1) * queryParam.getPageSize()); solrQuery.setRows(queryParam.getPageSize()); } //是否排序 if (isRank) { if (queryParam.getRankType() != null && queryParam.getRankType() == 1) { solrQuery.addSort(queryParam.getRankParm(), SolrQuery.ORDER.desc); } else if (queryParam.getRankType() != null && queryParam.getRankType() == 2) { solrQuery.addSort(queryParam.getRankParm(), ORDER.asc); } else { solrQuery.addSort(queryParam.getRankParm(), SolrQuery.ORDER.desc); } } } QueryResponse response = client.query(solrQuery); return response; } /** * 查询solr数据 * @param client solr实例 * @param queryParam 过滤参数bean * @param * @return * @throws IOException * @throws SolrServerException */ @Override public QueryResponse queryByBean(SolrClient client, BasicQueryParamDTO queryParam) throws IOException, SolrServerException { return this.queryByBean(client, queryParam,null); } /** * 查询solr数据 * @param client solr实例 * @param bean 基础查询信息bean * @param * @return * @throws IOException * @throws SolrServerException */ @Override public QueryResponse queryByBean(SolrClient client, T bean) throws IOException, SolrServerException { return this.queryByBean(client, null, bean); } } solrClient 对象获取 SolrClientService
package com.youxuepai.res.assistant.service.solr; import org.apache.solr.client.solrj.SolrClient; /** * Interface SolrClientService ... * 获取solr实例 * @group * @author gaobo * @date 2019/9/5 */ public interface SolrClientService { /** * 获取教辅资源solr核心client * @return SolrClient */ SolrClient getAssResourceSolrClient(); }
solrClient 对象获取 SolrClientServiceImpl
package com.youxuepai.res.assistant.service.solr.impl; import com.youxuepai.res.assistant.common.solr.SolrHandler; import com.youxuepai.res.assistant.service.solr.SolrClientService; import org.apache.solr.client.solrj.SolrClient; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; /** * 类 SolrClientServiceImpl * 获取solr实例Service * @group * @author gaobo * @date 2019/8/23 */ @Service public class SolrClientServiceImpl implements SolrClientService { @Value("${solr.host}") private String solrHost; @Value("${solr.assistantResourceCoreName}") private String assResourceCoreName; /** * 获取教辅资源solr核心client * @return SolrClient */ @Override public SolrClient getAssResourceSolrClient() { String solrUrl = solrHost + assResourceCoreName; SolrHandler instance = SolrHandler.getInstance(solrUrl); return instance.getClient(); } }
solr操作dto AssistantResource
package com.youxuepai.res.assistant.entity; import com.baomidou.mybatisplus.annotation.TableId; import java.io.Serializable; import java.util.Date; import org.apache.solr.client.solrj.beans.Field; /** *
* 教辅资源 主表 *
* * @author ${author} * @since 2019-08-12 */ public class AssistantResource implements Serializable { private static final long serialVersionUID = 1L; @TableId @Field("assistantResourceId") private String assistantResourceId; /** * 关联的教辅ID,在教材知识体系中 */ @Field("assistantId") private Integer assistantId; /** * 教辅名称,来自教材知识体系中 */ @Field("assistantName") private String assistantName; /** * 教辅资源名称,由系统生成 */ private String resourceName; /** * ISBN 格式如 978-7-107-18617-5 */ @Field("isbn") private String isbn; /** * 教辅项目编码 */ @Field("projectCode") private String projectCode; /** * 教辅品牌ID */ @Field("assistantBrandId") private Integer brandId; /** * 学段ID */ @Field("periodId") private Integer periodId; /** * 科目ID */ @Field("subjectId") private Integer subjectId; /** * 版本ID */ @Field("editionId") private Integer editionId; /** * 学制ID */ @Field("eduSystem") private Integer eduSystem; /** * 封面图url */ @Field("coverUrl") private String coverUrl; /** * 教辅简介 */ @Field("introduce") private String introduce; /** * 必选修 */ @Field("volumneId") private Integer volumneId; /** * 关联教材目录数量 */ @Field("chaptersNum") private String chaptersNum; /** * 年级ID */ @Field("gradeId") private Integer gradeId; /** * 是否删除 0-未删除,1-已删除 */ private Integer isDeleted; /** * 是否添加素材 0-未添加,1-已添加 */ @Field("materialStatus") private Integer materialStatus; /** * 素材上传的URL,基目录 */ private String materialUrl; /** * 发布状态 */ @Field("publishStatus") private Integer publishStatus; /** * 是否锁定目录,0-未锁定,1-锁定 */ private Integer lockStatus; /** * 是否导入源文件,0-未导入,1-已导入 */ @Field("importSourceStatus") private Integer importSourceStatus; /** * 是否导入资源zip包,0-未导入,1-已导入 */ @Field("importZipStatus") private Integer importZipStatus; /** * 是否生成资源zip包,0-未生成,1-已生成 */ @Field("generateZipStatus") private Integer generateZipStatus; @Field("created") private Date created; private String createdBy; @Field("updated") private Date updated; private String updatedBy; public String getAssistantResourceId() { return assistantResourceId; } public void setAssistantResourceId(String assistantResourceId) { this.assistantResourceId = assistantResourceId; } public Integer getAssistantId() { return assistantId; } public void setAssistantId(Integer assistantId) { this.assistantId = assistantId; } public String getAssistantName() { return assistantName; } public void setAssistantName(String assistantName) { this.assistantName = assistantName; } public String getResourceName() { return resourceName; } public void setResourceName(String resourceName) { this.resourceName = resourceName; } public String getIsbn() { return isbn; } public void setIsbn(String isbn) { this.isbn = isbn; } public String getProjectCode() { return projectCode; } public void setProjectCode(String projectCode) { this.projectCode = projectCode; } public Integer getBrandId() { return brandId; } public void setBrandId(Integer brandId) { this.brandId = brandId; } public Integer getPeriodId() { return periodId; } public void setPeriodId(Integer periodId) { this.periodId = periodId; } public Integer getSubjectId() { return subjectId; } public void setSubjectId(Integer subjectId) { this.subjectId = subjectId; } public Integer getEditionId() { return editionId; } public void setEditionId(Integer editionId) { this.editionId = editionId; } public Integer getEduSystem() { return eduSystem; } public void setEduSystem(Integer eduSystem) { this.eduSystem = eduSystem; } public String getCoverUrl() { return coverUrl; } public void setCoverUrl(String coverUrl) { this.coverUrl = coverUrl; } public String getIntroduce() { return introduce; } public void setIntroduce(String introduce) { this.introduce = introduce; } public Integer getVolumneId() { return volumneId; } public void setVolumneId(Integer volumneId) { this.volumneId = volumneId; } public String getChaptersNum() { return chaptersNum; } public void setChaptersNum(String chaptersNum) { this.chaptersNum = chaptersNum; } public Integer getGradeId() { return gradeId; } public void setGradeId(Integer gradeId) { this.gradeId = gradeId; } public Integer getIsDeleted() { return isDeleted; } public void setIsDeleted(Integer isDeleted) { this.isDeleted = isDeleted; } public Integer getMaterialStatus() { return materialStatus; } public void setMaterialStatus(Integer materialStatus) { this.materialStatus = materialStatus; } public String getMaterialUrl() { return materialUrl; } public void setMaterialUrl(String materialUrl) { this.materialUrl = materialUrl; } public Integer getPublishStatus() { return publishStatus; } public void setPublishStatus(Integer publishStatus) { this.publishStatus = publishStatus; } public Integer getLockStatus() { return lockStatus; } public void setLockStatus(Integer lockStatus) { this.lockStatus = lockStatus; } public Integer getImportSourceStatus() { return importSourceStatus; } public void setImportSourceStatus(Integer importSourceStatus) { this.importSourceStatus = importSourceStatus; } public Integer getImportZipStatus() { return importZipStatus; } public void setImportZipStatus(Integer importZipStatus) { this.importZipStatus = importZipStatus; } public Integer getGenerateZipStatus() { return generateZipStatus; } public void setGenerateZipStatus(Integer generateZipStatus) { this.generateZipStatus = generateZipStatus; } public Date getCreated() { return created; } public void setCreated(Date created) { this.created = created; } public String getCreatedBy() { return createdBy; } public void setCreatedBy(String createdBy) { this.createdBy = createdBy; } public Date getUpdated() { return updated; } public void setUpdated(Date updated) { this.updated = updated; } public String getUpdatedBy() { return updatedBy; } public void setUpdatedBy(String updatedBy) { this.updatedBy = updatedBy; } @Override public String toString() { return "AssistantResource{" + "assistantResourceId='" + assistantResourceId + '\'' + ", assistantId=" + assistantId + ", assistantName='" + assistantName + '\'' + ", resourceName='" + resourceName + '\'' + ", isbn='" + isbn + '\'' + ", projectCode='" + projectCode + '\'' + ", brandId=" + brandId + ", periodId=" + periodId + ", subjectId=" + subjectId + ", editionId=" + editionId + ", eduSystem=" + eduSystem + ", coverUrl='" + coverUrl + '\'' + ", introduce='" + introduce + '\'' + ", volumneId=" + volumneId + ", chaptersNum='" + chaptersNum + '\'' + ", gradeId=" + gradeId + ", isDeleted=" + isDeleted + ", materialStatus=" + materialStatus + ", materialUrl='" + materialUrl + '\'' + ", publishStatus=" + publishStatus + ", lockStatus=" + lockStatus + ", importSourceStatus=" + importSourceStatus + ", importZipStatus=" + importZipStatus + ", generateZipStatus=" + generateZipStatus + ", created=" + created + ", createdBy='" + createdBy + '\'' + ", updated=" + updated + ", updatedBy='" + updatedBy + '\'' + '}'; } }
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。
本文标题:solrJ项目开发的示例分析
分享URL:http://ybzwz.com/article/jccsds.html