柚子快報(bào)激活碼778899分享:基于數(shù)據(jù)庫(kù)的全文檢索實(shí)現(xiàn)
柚子快報(bào)激活碼778899分享:基于數(shù)據(jù)庫(kù)的全文檢索實(shí)現(xiàn)
對(duì)于內(nèi)容摘要,信件內(nèi)容進(jìn)行全文檢索 基于SpringBoot 2.5.6+Postgresql+jpa+hibernate實(shí)現(xiàn)
依賴
業(yè)務(wù)邏輯
登記保存之后,處理完成業(yè)務(wù)邏輯,發(fā)送全文檢索事件
//附加類(lèi)型對(duì)應(yīng)的附件ids
Map
attCategoryToAttIds.put(cmpRecord.getFileCategory(), files==null?null:files.stream().map(d->d.getId()).collect(Collectors.toList()));
//處理監(jiān)聽(tīng)事件所需要的數(shù)據(jù)
Map
//創(chuàng)建全文檢索事件
DomainEvent de = new DefaultDomainEvent(cmpRecord.getId() + "_Handle_CmpRecord_FullTextSearch", operateInfo, ExecutePoint.CURR_THREAD,
eventData, new Date(), "Handle_CmpRecord_FullTextSearch");
//發(fā)布事件
DomainEventPublisherFactory.getRegisteredPublisher().publishEvent(de);
處理業(yè)務(wù)發(fā)送全文檢索事件
@Service
@Transactional
@SuppressWarnings("unchecked")
public class HandleCmpRecordFullTextSearchListener implements IDomainEventListener {
@Autowired
private CmpRecordRepository cmpRecordRepository;
@Autowired
private DataChangeLogEventRepository dataChangeLogEventRepository;
@Override
public void onEvent(DomainEvent event) {
AccessTokenUser operator=event.getOperator();
Date operateTime=event.obtainEventTime();
Map
String recordId = (String) otherData.get("recordId");
boolean newRecord=(boolean)otherData.get("newRecord");
String comment = (String) otherData.get("comment");//辦理記錄的備注
if(StringUtils.isBlank(recordId)) {
throw new RuntimeException("未指定信訪記錄id");
}
//獲取登記信息
CmpRecord cmdRecord = cmpRecordRepository.getCmpRecordById(recordId);
//指定關(guān)聯(lián)關(guān)系
RelateProjValObj cmpRdProj=new RelateProjValObj(recordId,RelateProjConstants.PROJTYPE_CMP_RECORD);
//這是關(guān)聯(lián)那個(gè)業(yè)務(wù)
List
DomainEvent de=null;
//登記信息是無(wú)效的 則刪除已存在的和這個(gè)件相關(guān)的
if(cmdRecord==null||!cmdRecord.isValidEntity()) {
//刪除全文檢索信息
de=new FullTextSearchOperateEvent(recordId+"_FullTextSearch_Remove", null, operator, operateTime,
mainProjs, null);
DomainEventPublisherFactory.getRegisteredPublisher().publishEvent(de);
return;
}
//全文檢索 類(lèi)型前綴
String contentTypepPefix=RelateProjConstants.PROJTYPE_CMP_RECORD;
//在當(dāng)前線程中執(zhí)行,保證事務(wù)一致性
ExecutePoint executePoint=ExecutePoint.CURR_THREAD;
/***********************************************關(guān)鍵詞檢索-內(nèi)容摘要***********************************************/
//全文檢索的類(lèi)型 區(qū)分內(nèi)容摘要 附件內(nèi)容
List
String contentAbstract =cmdRecord.getBaseInfo().getContentAbstract();//內(nèi)容摘要
if(StringUtils.isBlank(contentAbstract)) contentAbstract="";
if(StringUtils.isNotBlank(comment)) {
if(StringUtils.isNotBlank(contentAbstract)) contentAbstract=contentAbstract + ",";
contentAbstract=contentAbstract+comment;
}
de=new FullTextSearchOperateEvent(recordId+"_FullTextSearch_Update", executePoint, operator, operateTime,
mainProjs, contentTypes, contentAbstract, null);
DomainEventPublisherFactory.getRegisteredPublisher().publishEvent(de);
/***********************************************關(guān)鍵詞檢索-信件內(nèi)容***********************************************/
contentTypes=Arrays.asList(contentTypepPefix+"_content");
String content =cmdRecord.getBaseInfo().getContent();//信件內(nèi)容
de=new FullTextSearchOperateEvent(recordId+"_FullTextSearch_Update", executePoint, operator, operateTime,
mainProjs, contentTypes, content, null);
DomainEventPublisherFactory.getRegisteredPublisher().publishEvent(de);
/***********************************************關(guān)鍵詞檢索-附件(原信等)***********************************************/
//如果附件也需要檢索 設(shè)置attIds參數(shù)
Map
if(attCategoryToAttIds!=null && attCategoryToAttIds.size() > 0) {
//按附件類(lèi)型分開(kāi)
for (Map.Entry
contentTypes=Arrays.asList(contentTypepPefix+"_att_"+d.getKey());
List
de=new FullTextSearchOperateEvent(recordId+"_att_"+d.getKey()+"_FullTextSearch_Update", executePoint,
operator, operateTime, mainProjs, contentTypes, null, attIds);
DomainEventPublisherFactory.getRegisteredPublisher().publishEvent(de);
}
}
}
@Override
public boolean listenOn(String eventType) {
return "Handle_CmpRecord_FullTextSearch".equals(eventType);
}
}
統(tǒng)一處理全文檢索事件
@Service
@Transactional
public class FullTextSearchListener extends JpaHibernateRepository implements IDomainEventListener{
@Autowired
private FullTextSearchRepository fullTextSearchRepository;
@Autowired
private IFileSysService fileSysService;
@Override
public void onEvent(DomainEvent event) {
if("true".equals(BaseConstants.getProperty("prefetchingRecordNo", "false"))){
return;
}
FullTextSearchOperateEvent de = null;
if(event instanceof FullTextSearchOperateEvent) {
de=(FullTextSearchOperateEvent)event;
}
if(de==null) {
return;
}
if(FullTextSearchOperateEvent.EVENTTYPE_UPDATE.equals(de.getEventType())) {
/**
"mainProjs":List
"contentType":String 必選
"content":String 可選
"attIds":List
"relProjs":List
"removeOtherRelProjs":false 可選 是否清除 指定relProjs以外的關(guān)聯(lián)記錄
*/
this.fullTextSearchUpdate(de);
}else if(FullTextSearchOperateEvent.EVENTTYPE_REMOVE.equals(de.getEventType())) {
/**
"mainProjs":List
*/
this.fullTextSearchRemoveByProjs(de);
}
}
//關(guān)鍵詞檢索增加
private void fullTextSearchUpdate(FullTextSearchOperateEvent de) {
Date date=de.obtainEventTime();
if(date==null) {
date=new Date();
}
List
String contentType=null;
if(de.getContentTypes()!=null&&de.getContentTypes().size()==1) {
contentType=de.getContentTypes().get(0);
}
String content=de.getContent();
List
if(mainProjs==null||mainProjs.size()==0
||StringUtils.isBlank(contentType)
) {
throw new RuntimeException("數(shù)據(jù)指定錯(cuò)誤");
}
Set
for (RelateProjValObj mainProj : mainProjs) {
if(StringUtils.isBlank(mainProj.getProjId())||StringUtils.isBlank(mainProj.getProjType())) {
continue;
}
fullTextIds.add(new FullTextSearch(mainProj,contentType,null,null).getId());
}
if(fullTextIds.size()==0) {
throw new RuntimeException("數(shù)據(jù)指定錯(cuò)誤");
}
//這是從附件中獲取文本數(shù)據(jù)
if(StringUtils.isBlank(content)&&attIds!=null) {
content="";
try {
if(attIds.size()>0) {
Map
return fileSysService.findFileContentByIds(attIds, true);
});
for (String attContent : attIdToContentMao.values()) {
if(StringUtils.isBlank(attContent)) {
continue;
}
if(StringUtils.isNotBlank(content)) {
content+=",";
}
content+=RegExUtils.replaceAll(attContent, "\\u0000", "");//處理掉非法字符
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
//從數(shù)據(jù)庫(kù)中獲取已經(jīng)存的
List
Map
//遍歷這次需要更新的記錄
for (RelateProjValObj mainProj : mainProjs) {
if(StringUtils.isBlank(mainProj.getProjId())||StringUtils.isBlank(mainProj.getProjType())) {
continue;
}
FullTextSearch fullText=new FullTextSearch(mainProj, contentType, content, date);
FullTextSearch oldFullText=oldFullTextMap.get(fullText.getId());
//舊的記錄中已存在 則更新
if(oldFullText!=null) {
if(StringUtils.isBlank(content)) {
//如果內(nèi)容未空 則刪除
this.fullTextSearchRepository.removeFullTextSearch(oldFullText);
return;
}
//如果存在內(nèi)容,則更新
this.fullTextSearchRepository
.updateFullTextSearchContent(fullText.getId(), content, date);
}else {
if(StringUtils.isBlank(content)) {
return;
}
try {//否則 創(chuàng)建全文檢索記錄
this.fullTextSearchRepository.createFullTextSearch(fullText);
} catch (Exception e) {
e.printStackTrace();
return;
}
}
}
}
//關(guān)鍵詞檢索刪除 根據(jù)主相關(guān)件
private void fullTextSearchRemoveByProjs(FullTextSearchOperateEvent de) {
Date date=de.obtainEventTime();
if(date==null) {
date=new Date();
}
List
if(mainProjs==null||mainProjs.size()==0) {
throw new RuntimeException("數(shù)據(jù)指定錯(cuò)誤");
}
List
for (RelateProjValObj mainProj : mainProjs) {
projKeys.add(mainProj.getProjKey());
}
Map
StringBuffer hql=new StringBuffer();
hql.append("delete from ").append(FullTextSearch.class.getName()).append(" ");
hql.append("where mainProj.projKey IN(:projKeys) ");
params.put("projKeys", projKeys);
if(de.getContentTypes()!=null&&de.getContentTypes().size()>0) {
params.put("contentTypes", de.getContentTypes());
}
this.createHQLQueryByMapParams(hql.toString(), params).executeUpdate();
}
@Override
public boolean listenOn(String eventType) {
return eventType.startsWith(FullTextSearchOperateEvent.class.getName());
}
}
全文檢索實(shí)體
@Entity
@Table(
name="TV_FULLTEXT_SEARCH",
indexes={
@Index(name="idx_TV_FULLTEXT_SEARCH1",columnList="projKey"),
@Index(name="idx_TV_FULLTEXT_SEARCH2",columnList="contentType")
}
)
public class FullTextSearch extends IEntity {
@Id
@Column(length=200)
private String id;
private RelateProjValObj mainProj;//來(lái)源相關(guān)件
@Lob
@Type(type="org.hibernate.type.TextType")
private String content;//檢索內(nèi)容
@Column(length=100)
private String contentType;//檢索類(lèi)型
@Column(length=100)
private Date lastUpdateDate;//最后更新時(shí)間
public String getId() {
return id;
}
public String getContent() {
return content;
}
public String getContentType() {
return contentType;
}
public RelateProjValObj getMainProj() {
return mainProj;
}
public Date getLastUpdateDate() {
return lastUpdateDate;
}
public FullTextSearch() {
}
public FullTextSearch(RelateProjValObj mainProj, String contentType,
String content, Date lastUpdateDate) {
this.id = mainProj.getProjKey()+"_"+contentType;
this.mainProj = mainProj;
this.content = content;
this.contentType = contentType;
this.lastUpdateDate = lastUpdateDate;
if(this.lastUpdateDate==null){
this.lastUpdateDate = new Date();
}
}
}
存儲(chǔ)數(shù)據(jù)格式
查詢
sql大致就是這樣的邏輯
select tv.id from tv_cmp_dw_query tv join tv_fulltext_search tvs on tv.id = tvs.proj_id where tvs.contet_type in () and conent like '%測(cè)試%'
事件處理機(jī)制請(qǐng)看另一篇文章 自定義事件處理機(jī)制
柚子快報(bào)激活碼778899分享:基于數(shù)據(jù)庫(kù)的全文檢索實(shí)現(xiàn)
推薦文章
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。