feat(PortalSearchEsServiceImpl): 添加ES索引缺失自动重建机制

main
zangch@mesnac.com 4 months ago
parent b9bc38bbcc
commit fbc7d12cc5

@ -9,8 +9,10 @@ import com.ruoyi.portal.search.convert.PortalSearchDocConverter;
import com.ruoyi.portal.search.es.entity.PortalSearchDoc;
import com.ruoyi.portal.search.es.mapper.PortalSearchMapper;
import com.ruoyi.portal.search.service.PortalSearchEsService;
import com.ruoyi.portal.service.IHwSearchRebuildService;
import org.dromara.easyes.core.biz.EsPageInfo;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@ -32,15 +34,21 @@ public class PortalSearchEsServiceImpl implements PortalSearchEsService
{
private static final Logger log = LoggerFactory.getLogger(PortalSearchEsServiceImpl.class);
private static final String INDEX_NAME = "hw_portal_content";
private static final Pattern ESCAPE_PATTERN = Pattern.compile("([\\\\.*+\\[\\](){}^$?|])");
private final PortalSearchMapper portalSearchMapper;
private final IHwSearchRebuildService hwSearchRebuildService;
private final ObjectMapper objectMapper = new ObjectMapper();
public PortalSearchEsServiceImpl(PortalSearchMapper portalSearchMapper)
public PortalSearchEsServiceImpl(PortalSearchMapper portalSearchMapper,
@Autowired(required = false) IHwSearchRebuildService hwSearchRebuildService)
{
this.portalSearchMapper = portalSearchMapper;
this.hwSearchRebuildService = hwSearchRebuildService;
}
@Override
@ -50,7 +58,21 @@ public class PortalSearchEsServiceImpl implements PortalSearchEsService
wrapper.and(q -> q.match(PortalSearchDoc::getTitle, keyword, 5.0f).or().match(PortalSearchDoc::getContent, keyword, 2.0f));
wrapper.eq(PortalSearchDoc::getIsDelete, "0");
EsPageInfo<PortalSearchDoc> pageInfo = portalSearchMapper.pageQuery(wrapper, pageNum, pageSize);
EsPageInfo<PortalSearchDoc> pageInfo;
try
{
pageInfo = portalSearchMapper.pageQuery(wrapper, pageNum, pageSize);
}
catch (Exception e)
{
if (!isIndexNotFoundException(e))
{
throw e;
}
// 首次切到 ES 或索引被误删时,自动重建索引并重试一次,避免接口只能依赖人工预热
rebuildIndexAndRetry();
pageInfo = portalSearchMapper.pageQuery(wrapper, pageNum, pageSize);
}
List<SearchResultDTO> rows = pageInfo.getList().stream().map(doc -> toResult(doc, keyword, editMode)).collect(Collectors.toList());
SearchPageDTO page = new SearchPageDTO();
@ -59,6 +81,32 @@ public class PortalSearchEsServiceImpl implements PortalSearchEsService
return page;
}
private void rebuildIndexAndRetry()
{
if (hwSearchRebuildService == null)
{
throw new IllegalStateException("门户搜索索引不存在,且未注入索引重建服务");
}
log.warn("portal search index {} not found, rebuild automatically", INDEX_NAME);
hwSearchRebuildService.rebuildAll();
}
private boolean isIndexNotFoundException(Throwable throwable)
{
Throwable current = throwable;
while (current != null)
{
String message = current.getMessage();
if (StringUtils.containsIgnoreCase(message, "index_not_found_exception")
|| StringUtils.containsIgnoreCase(message, "no such index [" + INDEX_NAME + "]"))
{
return true;
}
current = current.getCause();
}
return false;
}
private SearchResultDTO toResult(PortalSearchDoc doc, String keyword, boolean editMode)
{
SearchResultDTO result = new SearchResultDTO();

Loading…
Cancel
Save