您好,登錄后才能下訂單哦!
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對象列表 */ <T> void saveOrUpdate(List<T> beans, SolrClient client); /** * solr添加Obj對象 */ <T> void saveOrUpdate(T beans, SolrClient client); /** * 原子更新solr * 只根據ID更新部分字段 * @param bean * @param client * @param <T> */ <T> void atmoUpdate(T bean, SolrClient client); /** * solr根據Id刪除對象索引 * @param id * @return */ boolean deleteById(String id,SolrClient client); /** * 轉換javabean對象為查詢params. * * @param bean 添加入solr的實體類 */ <T> SolrQuery ConvertQueryParam(T bean); <T> SolrInputDocument beanToSolrMap(T bean); /** * 查詢solr數據 * * @param client solr實例 * @param queryParam 過濾參數bean * @param bean 基礎查詢信息bean */ <T> QueryResponse queryByBean( SolrClient client, BasicQueryParamDTO queryParam, T bean) throws IOException, SolrServerException; /** * 查詢solr數據 * * @param client solr實例 * @param queryParam 過濾參數bean */ <T> QueryResponse queryByBean( SolrClient client, BasicQueryParamDTO queryParam ) throws IOException, SolrServerException; /** * 查詢solr數據 * * @param client solr實例 * @param bean 基礎查詢信息bean */ <T> 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 <T> void saveOrUpdate(List<T> 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 <T> 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 <T> */ @Override public <T> 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 <T> SolrQuery ConvertQueryParam(T bean) { Map<String, Object> paramMap = beanToMap(bean); // 添加默認參數 SolrQuery solrQuery = new SolrQuery(); paramMap.entrySet().forEach( filterMap -> solrQuery.addFilterQuery(filterMap.getKey() + ":" + filterMap.getValue()) ); return solrQuery; } private <T> Map<String, Object> beanToMap(T bean){ SolrInputDocument doc = binder.toSolrInputDocument(bean); Collection<SolrInputField> 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 <T> SolrInputDocument beanToSolrMap(T bean) { Map<String, Object> paramMap = beanToMap(bean); SolrInputDocument inputDoc = new SolrInputDocument(); for (String field : paramMap.keySet()) { Map<String, Object> 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 <T> * @return * @throws IOException * @throws SolrServerException */ @Override public <T> 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 <T> * @return * @throws IOException * @throws SolrServerException */ @Override public <T> QueryResponse queryByBean(SolrClient client, BasicQueryParamDTO queryParam) throws IOException, SolrServerException { return this.queryByBean(client, queryParam,null); } /** * 查詢solr數據 * @param client solr實例 * @param bean 基礎查詢信息bean * @param <T> * @return * @throws IOException * @throws SolrServerException */ @Override public <T> 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; /** * <p> * 教輔資源 主表 * </p> * * @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 + '\'' + '}'; } }
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。