fix: 修复搜索SQL字符集不一致问题及优化代码

- HwSearchMapper 统一 UNION 各分支的字符集和排序规则,避免历史表结构 collation 不一致时搜索兜底 SQL 直接失败
- `order`避免与关键字冲突
main
zangch@mesnac.com 5 days ago
parent 1daba19bc5
commit d159219830

@ -2,6 +2,7 @@ package com.ruoyi.web.controller.search.controller;
import org.dromara.easyes.core.biz.EsPageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
@ -26,6 +27,7 @@ import com.ruoyi.web.controller.search.service.ISearchService;
* @author ruoyi
*/
@RestController
@ConditionalOnProperty(prefix = "ruoyi.search.admin-demo", name = "enabled", havingValue = "true")
@RequestMapping("/search/fulltext")
public class TextSearchController
{

@ -6,7 +6,7 @@ import jakarta.annotation.PostConstruct;
import org.dromara.easyes.core.biz.EsPageInfo;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.dromara.easyes.core.conditions.update.LambdaEsUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
@ -16,7 +16,6 @@ import com.ruoyi.web.controller.search.mapper.EsTextDocumentMapper;
import com.ruoyi.web.controller.search.model.EsTextDocument;
import com.ruoyi.web.controller.search.model.Search;
import com.ruoyi.web.controller.search.service.ISearchService;
import lombok.RequiredArgsConstructor;
/**
* Service
@ -24,13 +23,17 @@ import lombok.RequiredArgsConstructor;
* @author ruoyi
*/
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@ConditionalOnProperty(prefix = "ruoyi.search.admin-demo", name = "enabled", havingValue = "true")
public class SearchImpl implements ISearchService
{
private final EsTextDocumentMapper esTextDocumentMapper;
private final SysNoticeMapper noticeMapper;
@Autowired
private SysNoticeMapper noticeMapper;
public SearchImpl(EsTextDocumentMapper esTextDocumentMapper, SysNoticeMapper noticeMapper)
{
this.esTextDocumentMapper = esTextDocumentMapper;
this.noticeMapper = noticeMapper;
}
/**
*

@ -19,37 +19,18 @@
</resultMap>
<select id="searchByKeyword" resultMap="SearchRawRecordResult">
<!-- 统一 UNION 各分支的字符集和排序规则,避免历史表结构 collation 不一致时搜索兜底 SQL 直接失败 -->
SELECT *
FROM (
SELECT 'menu' AS source_type,
CAST(m.web_menu_id AS CHAR) AS biz_id,
m.web_menu_name AS title,
CONCAT(IFNULL(m.web_menu_name, ''), ' ', IFNULL(m.ancestors, '')) AS content,
NULL AS web_code,
NULL AS type_id,
NULL AS device_id,
CAST(m.web_menu_id AS CHAR) AS menu_id,
NULL AS document_id,
110 AS score,
m.update_time AS updated_at
FROM hw_web_menu m
WHERE m.is_delete = '0'
AND (
m.web_menu_name LIKE CONCAT('%', #{keyword}, '%')
OR m.ancestors LIKE CONCAT('%', #{keyword}, '%')
)
UNION ALL
SELECT 'web' AS source_type,
CAST(w.web_id AS CHAR) AS biz_id,
CONCAT('页面#', w.web_code) AS title,
w.web_json_string AS content,
CAST(w.web_code AS CHAR) AS web_code,
NULL AS type_id,
NULL AS device_id,
NULL AS menu_id,
NULL AS document_id,
SELECT CONVERT('web' USING utf8mb4) COLLATE utf8mb4_general_ci AS source_type,
CONVERT(CAST(w.web_id AS CHAR) USING utf8mb4) COLLATE utf8mb4_general_ci AS biz_id,
CONVERT(CONCAT('页面#', w.web_code) USING utf8mb4) COLLATE utf8mb4_general_ci AS title,
CONVERT(IFNULL(w.web_json_string, '') USING utf8mb4) COLLATE utf8mb4_general_ci AS content,
CONVERT(CAST(w.web_code AS CHAR) USING utf8mb4) COLLATE utf8mb4_general_ci AS web_code,
CAST(NULL AS CHAR CHARACTER SET utf8mb4) COLLATE utf8mb4_general_ci AS type_id,
CAST(NULL AS CHAR CHARACTER SET utf8mb4) COLLATE utf8mb4_general_ci AS device_id,
CAST(NULL AS CHAR CHARACTER SET utf8mb4) COLLATE utf8mb4_general_ci AS menu_id,
CAST(NULL AS CHAR CHARACTER SET utf8mb4) COLLATE utf8mb4_general_ci AS document_id,
80 AS score,
w.update_time AS updated_at
FROM hw_web w
@ -58,15 +39,15 @@
UNION ALL
SELECT 'web1' AS source_type,
CAST(w1.web_id AS CHAR) AS biz_id,
CONCAT('详情#', w1.web_code, '-', w1.typeId, '-', w1.device_id) AS title,
w1.web_json_string AS content,
CAST(w1.web_code AS CHAR) AS web_code,
CAST(w1.typeId AS CHAR) AS type_id,
CAST(w1.device_id AS CHAR) AS device_id,
NULL AS menu_id,
NULL AS document_id,
SELECT CONVERT('web1' USING utf8mb4) COLLATE utf8mb4_general_ci AS source_type,
CONVERT(CAST(w1.web_id AS CHAR) USING utf8mb4) COLLATE utf8mb4_general_ci AS biz_id,
CONVERT(CONCAT('详情#', w1.web_code, '-', w1.typeId, '-', w1.device_id) USING utf8mb4) COLLATE utf8mb4_general_ci AS title,
CONVERT(IFNULL(w1.web_json_string, '') USING utf8mb4) COLLATE utf8mb4_general_ci AS content,
CONVERT(CAST(w1.web_code AS CHAR) USING utf8mb4) COLLATE utf8mb4_general_ci AS web_code,
CONVERT(CAST(w1.typeId AS CHAR) USING utf8mb4) COLLATE utf8mb4_general_ci AS type_id,
CONVERT(CAST(w1.device_id AS CHAR) USING utf8mb4) COLLATE utf8mb4_general_ci AS device_id,
CAST(NULL AS CHAR CHARACTER SET utf8mb4) COLLATE utf8mb4_general_ci AS menu_id,
CAST(NULL AS CHAR CHARACTER SET utf8mb4) COLLATE utf8mb4_general_ci AS document_id,
90 AS score,
w1.update_time AS updated_at
FROM hw_web1 w1
@ -75,15 +56,15 @@
UNION ALL
SELECT 'document' AS source_type,
d.document_id AS biz_id,
IFNULL(NULLIF(d.json, ''), d.document_id) AS title,
IFNULL(d.json, '') AS content,
d.web_code AS web_code,
d.type AS type_id,
NULL AS device_id,
NULL AS menu_id,
d.document_id AS document_id,
SELECT CONVERT('document' USING utf8mb4) COLLATE utf8mb4_general_ci AS source_type,
CONVERT(IFNULL(d.document_id, '') USING utf8mb4) COLLATE utf8mb4_general_ci AS biz_id,
CONVERT(IFNULL(NULLIF(d.json, ''), d.document_id) USING utf8mb4) COLLATE utf8mb4_general_ci AS title,
CONVERT(IFNULL(d.json, '') USING utf8mb4) COLLATE utf8mb4_general_ci AS content,
CONVERT(IFNULL(d.web_code, '') USING utf8mb4) COLLATE utf8mb4_general_ci AS web_code,
CONVERT(IFNULL(d.type, '') USING utf8mb4) COLLATE utf8mb4_general_ci AS type_id,
CAST(NULL AS CHAR CHARACTER SET utf8mb4) COLLATE utf8mb4_general_ci AS device_id,
CAST(NULL AS CHAR CHARACTER SET utf8mb4) COLLATE utf8mb4_general_ci AS menu_id,
CONVERT(IFNULL(d.document_id, '') USING utf8mb4) COLLATE utf8mb4_general_ci AS document_id,
70 AS score,
d.update_time AS updated_at
FROM hw_web_document d
@ -92,24 +73,6 @@
d.json LIKE CONCAT('%', #{keyword}, '%')
OR d.document_id LIKE CONCAT('%', #{keyword}, '%')
)
UNION ALL
SELECT 'configType' AS source_type,
CAST(ct.config_type_id AS CHAR) AS biz_id,
ct.config_type_name AS title,
CONCAT(IFNULL(ct.config_type_name, ''), ' ', IFNULL(ct.home_config_type_name, ''), ' ', IFNULL(ct.config_type_desc, '')) AS content,
NULL AS web_code,
CAST(ct.config_type_id AS CHAR) AS type_id,
NULL AS device_id,
NULL AS menu_id,
NULL AS document_id,
65 AS score,
ct.update_time AS updated_at
FROM hw_portal_config_type ct
WHERE ct.config_type_name LIKE CONCAT('%', #{keyword}, '%')
OR ct.home_config_type_name LIKE CONCAT('%', #{keyword}, '%')
OR ct.config_type_desc LIKE CONCAT('%', #{keyword}, '%')
) s
ORDER BY s.score DESC, s.updated_at DESC
LIMIT 500

@ -20,7 +20,7 @@
<sql id="selectHwWebMenuVo">
select web_menu_id, parent, ancestors, status, web_menu_name, tenant_id, web_menu__pic, web_menu_type,
order,
`order`,
web_menu_name_english,
is_delete
from hw_web_menu
@ -40,7 +40,7 @@
<if test="order != null "> and `order` = #{order}</if>
<if test="webMenuNameEnglish != null and webMenuNameEnglish != ''"> and web_menu_name_english = #{webMenuNameEnglish}</if>
</where>
order by parent, order asc, web_menu_id asc
order by parent, `order` asc, web_menu_id asc
</select>
<select id="selectHwWebMenuByWebMenuId" parameterType="Long" resultMap="HwWebMenuResult">
@ -48,7 +48,7 @@
where is_delete = '0' and web_menu_id = #{webMenuId}
</select>
<insert id="insertHwWebMenu" parameterType="HwWebMenu">
<insert id="insertHwWebMenu" parameterType="HwWebMenu" useGeneratedKeys="true" keyProperty="webMenuId">
insert into hw_web_menu
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="webMenuId != null">web_menu_id,</if>

@ -48,7 +48,7 @@
where is_delete = '0' and web_menu_id = #{webMenuId}
</select>
<insert id="insertHwWebMenu" parameterType="HwWebMenu1">
<insert id="insertHwWebMenu" parameterType="HwWebMenu1" useGeneratedKeys="true" keyProperty="webMenuId">
insert into hw_web_menu1
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="webMenuId != null">web_menu_id,</if>

Loading…
Cancel
Save