|
|
|
|
@ -54,25 +54,14 @@ public class PortalSearchEsServiceImpl implements PortalSearchEsService
|
|
|
|
|
@Override
|
|
|
|
|
public SearchPageDTO search(String keyword, Integer pageNum, Integer pageSize, boolean editMode)
|
|
|
|
|
{
|
|
|
|
|
// 先确保索引可用且不是“空壳索引”,避免切到 ES 后只建了索引结构却没有任何文档,导致搜索始终返回空
|
|
|
|
|
ensureIndexReady();
|
|
|
|
|
|
|
|
|
|
LambdaEsQueryWrapper<PortalSearchDoc> wrapper = new LambdaEsQueryWrapper<>();
|
|
|
|
|
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;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
pageInfo = portalSearchMapper.pageQuery(wrapper, pageNum, pageSize);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
if (!isIndexNotFoundException(e))
|
|
|
|
|
{
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
// 首次切到 ES 或索引被误删时,自动重建索引并重试一次,避免接口只能依赖人工预热
|
|
|
|
|
rebuildIndexAndRetry();
|
|
|
|
|
pageInfo = portalSearchMapper.pageQuery(wrapper, pageNum, pageSize);
|
|
|
|
|
}
|
|
|
|
|
EsPageInfo<PortalSearchDoc> 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();
|
|
|
|
|
@ -81,13 +70,46 @@ public class PortalSearchEsServiceImpl implements PortalSearchEsService
|
|
|
|
|
return page;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void rebuildIndexAndRetry()
|
|
|
|
|
private void ensureIndexReady()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (!portalSearchMapper.existsIndex(INDEX_NAME))
|
|
|
|
|
{
|
|
|
|
|
rebuildIndex("索引不存在");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (getIndexedDocumentCount() > 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
rebuildIndex("索引为空");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
if (!isIndexNotFoundException(e))
|
|
|
|
|
{
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
rebuildIndex("索引不存在");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private long getIndexedDocumentCount()
|
|
|
|
|
{
|
|
|
|
|
LambdaEsQueryWrapper<PortalSearchDoc> wrapper = new LambdaEsQueryWrapper<>();
|
|
|
|
|
EsPageInfo<PortalSearchDoc> pageInfo = portalSearchMapper.pageQuery(wrapper, 1, 1);
|
|
|
|
|
return pageInfo == null ? 0L : pageInfo.getTotal();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void rebuildIndex(String reason)
|
|
|
|
|
{
|
|
|
|
|
if (hwSearchRebuildService == null)
|
|
|
|
|
{
|
|
|
|
|
throw new IllegalStateException("门户搜索索引不存在,且未注入索引重建服务");
|
|
|
|
|
throw new IllegalStateException("门户搜索索引不可用,且未注入索引重建服务");
|
|
|
|
|
}
|
|
|
|
|
log.warn("portal search index {} not found, rebuild automatically", INDEX_NAME);
|
|
|
|
|
// 自动重建只处理“索引缺失/空索引”两类可自愈问题,避免把其它查询异常误判为需要删库重建
|
|
|
|
|
log.warn("portal search index {} {},start rebuild automatically", INDEX_NAME, reason);
|
|
|
|
|
hwSearchRebuildService.rebuildAll();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|