solrJ項(xiàng)目開(kāi)發(fā)的示例分析,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
“專業(yè)、務(wù)實(shí)、高效、創(chuàng)新、把客戶的事當(dāng)成自己的事”是我們每一個(gè)人一直以來(lái)堅(jiān)持追求的企業(yè)文化。 成都創(chuàng)新互聯(lián)是您可以信賴的網(wǎng)站建設(shè)服務(wù)商、專業(yè)的互聯(lián)網(wǎng)服務(wù)提供商! 專注于網(wǎng)站制作、網(wǎng)站建設(shè)、軟件開(kāi)發(fā)、設(shè)計(jì)服務(wù)業(yè)務(wù)。我們始終堅(jiān)持以客戶需求為導(dǎo)向,結(jié)合用戶體驗(yàn)與視覺(jué)傳達(dá),提供有針對(duì)性的項(xiàng)目解決方案,提供專業(yè)性的建議,創(chuàng)新互聯(lián)建站將不斷地超越自我,追逐市場(chǎng),引領(lǐng)市場(chǎng)!
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對(duì)象列表 */void saveOrUpdate(List beans, SolrClient client); /** * solr添加Obj對(duì)象 */ void saveOrUpdate(T beans, SolrClient client); /** * 原子更新solr * 只根據(jù)ID更新部分字段 * @param bean * @param client * @param */ void atmoUpdate(T bean, SolrClient client); /** * solr根據(jù)Id刪除對(duì)象索引 * @param id * @return */ boolean deleteById(String id,SolrClient client); /** * 轉(zhuǎn)換javabean對(duì)象為查詢params. * * @param bean 添加入solr的實(shí)體類 */ SolrQuery ConvertQueryParam(T bean); SolrInputDocument beanToSolrMap(T bean); /** * 查詢solr數(shù)據(jù) * * @param client solr實(shí)例 * @param queryParam 過(guò)濾參數(shù)bean * @param bean 基礎(chǔ)查詢信息bean */ QueryResponse queryByBean( SolrClient client, BasicQueryParamDTO queryParam, T bean) throws IOException, SolrServerException; /** * 查詢solr數(shù)據(jù) * * @param client solr實(shí)例 * @param queryParam 過(guò)濾參數(shù)bean */ QueryResponse queryByBean( SolrClient client, BasicQueryParamDTO queryParam ) throws IOException, SolrServerException; /** * 查詢solr數(shù)據(jù) * * @param client solr實(shí)例 * @param bean 基礎(chǔ)查詢信息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添加對(duì)象列表 */ @Override publicvoid saveOrUpdate(List beans, SolrClient client) { for (T bean : beans) { saveOrUpdate(bean, client); } try { client.commit(); } catch (SolrServerException | IOException e) { logger.error("保存文檔出錯(cuò)," + 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("保存文檔出錯(cuò)," + doc.toString(), e); throw new RuntimeException(e.getMessage()); } } final String PRIMARY_KEY = "assistantResourceId"; /** * 原子更新solr * 只根據(jù)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("保存文檔出錯(cuò)," + doc.toString(), e); throw new RuntimeException(e.getMessage()); } } /** * solr根據(jù)Id刪除對(duì)象索引 * @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; } /** * 轉(zhuǎn)換javabean對(duì)象為查詢params. * * @param bean 添加入solr的實(shí)體類 */ @Override public SolrQuery ConvertQueryParam(T bean) { Map paramMap = beanToMap(bean); // 添加默認(rèn)參數(shù) 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數(shù)據(jù) * @param client solr實(shí)例 * @param queryParam 過(guò)濾參數(shù)bean * @param bean 基礎(chǔ)查詢信息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; // 搜索關(guān)鍵字是否有值 if (isKeywords) { solrQuery.remove("q"); solrQuery.set("q", "content:" + "*" + queryParam.getKeywords() + "*"); } //是否分頁(yè) 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數(shù)據(jù) * @param client solr實(shí)例 * @param queryParam 過(guò)濾參數(shù)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數(shù)據(jù) * @param client solr實(shí)例 * @param bean 基礎(chǔ)查詢信息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 對(duì)象獲取 SolrClientService
package com.youxuepai.res.assistant.service.solr; import org.apache.solr.client.solrj.SolrClient; /** * Interface SolrClientService ... * 獲取solr實(shí)例 * @group * @author gaobo * @date 2019/9/5 */ public interface SolrClientService { /** * 獲取教輔資源solr核心client * @return SolrClient */ SolrClient getAssResourceSolrClient(); }
solrClient 對(duì)象獲取 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實(shí)例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; /** * 關(guān)聯(lián)的教輔ID,在教材知識(shí)體系中 */ @Field("assistantId") private Integer assistantId; /** * 教輔名稱,來(lái)自教材知識(shí)體系中 */ @Field("assistantName") private String assistantName; /** * 教輔資源名稱,由系統(tǒng)生成 */ private String resourceName; /** * ISBN 格式如 978-7-107-18617-5 */ @Field("isbn") private String isbn; /** * 教輔項(xiàng)目編碼 */ @Field("projectCode") private String projectCode; /** * 教輔品牌ID */ @Field("assistantBrandId") private Integer brandId; /** * 學(xué)段ID */ @Field("periodId") private Integer periodId; /** * 科目ID */ @Field("subjectId") private Integer subjectId; /** * 版本ID */ @Field("editionId") private Integer editionId; /** * 學(xué)制ID */ @Field("eduSystem") private Integer eduSystem; /** * 封面圖url */ @Field("coverUrl") private String coverUrl; /** * 教輔簡(jiǎn)介 */ @Field("introduce") private String introduce; /** * 必選修 */ @Field("volumneId") private Integer volumneId; /** * 關(guān)聯(lián)教材目錄數(shù)量 */ @Field("chaptersNum") private String chaptersNum; /** * 年級(jí)ID */ @Field("gradeId") private Integer gradeId; /** * 是否刪除 0-未刪除,1-已刪除 */ private Integer isDeleted; /** * 是否添加素材 0-未添加,1-已添加 */ @Field("materialStatus") private Integer materialStatus; /** * 素材上傳的URL,基目錄 */ private String materialUrl; /** * 發(fā)布狀態(tài) */ @Field("publishStatus") private Integer publishStatus; /** * 是否鎖定目錄,0-未鎖定,1-鎖定 */ private Integer lockStatus; /** * 是否導(dǎo)入源文件,0-未導(dǎo)入,1-已導(dǎo)入 */ @Field("importSourceStatus") private Integer importSourceStatus; /** * 是否導(dǎo)入資源zip包,0-未導(dǎo)入,1-已導(dǎo)入 */ @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 + '\'' + '}'; } }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。