资源条件有限,需要支持智搜的数据量也不大,上es搜索有点大材小用了,只好写个简版mysql的智搜,处理全文搜素,支持拼音搜索,中文分词,自定义分词断词,地图范围搜索,周边搜索,自定义多边形搜索。
通过设置定时SpringBatch抽取需要进行检索的表,并将检索的标题,概要,内容等存入检索表中。通过检索表进行全文检索。
代码主要包:通过网盘分享的文件:mysql智搜.zip
链接: https://pan.baidu.com/s/1MMhmyVD8o56Grp1Aa5IiKQ 提取码: 0530
1、提供的接口
@RestController
@Tag(name = "智搜全局搜索API")
@RequestMapping(path = "/smart/serach")
public class SearchIndexController {
@Resource
private SearchIndexService searchIndexService;
@PostMapping("/smartSearch")
@Operation(summary = "智搜数据查询")
@Log(title = "一张图智搜数据查询", businessType = BusinessType.QUERY)
public Response> getSearchIndexDataByPage(@RequestBody SearchIndexParamDTO searchIndexParam) {
return Response.with(searchIndexService.getSearchIndexDataByPage(searchIndexParam));
}
@PostMapping("/getSearchIndexByPage")
@Operation(summary = "普通智搜内容分页查询")
@Log(title = "普通智搜内容分页查询", businessType = BusinessType.QUERY)
public Response> getSearchIndexByPage(@RequestBody SearchIndexParamDTO searchIndexParam) {
return Response.with(searchIndexService.getSearchIndexByPage(searchIndexParam));
}
@PostMapping("/getSearchIndexById")
@Operation(summary = "通过主键获取检索详细信息")
@Log(title = "普通智搜内容分页查询", businessType = BusinessType.QUERY)
public Response getSearchIndexById(@RequestBody SearchIndexParamDTO searchIndexParam) {
return Response.with(searchIndexService.getSearchIndexById(searchIndexParam));
}
@PostMapping("/smartSearchRiver")
@Operation(summary = "智搜溯源河流数据查询")
@Log(title = "一张图智搜溯源河流数据查询", businessType = BusinessType.QUERY)
public Response getSmartSearchRiver(@RequestBody SearchPointParamDTO searchIndexParam) {
return Response.with(searchIndexService.getSmartSearchRiver(searchIndexParam));
}
}
2、service
public interface SearchIndexService extends IService {
Page getSearchIndexByPage(SearchIndexParamDTO searchIndexParamDTO);
SearchIndexDTO getSearchIndexById(SearchIndexParamDTO searchIndexParamDTO);
Page getSearchIndexDataByPage(SearchIndexParamDTO searchIndexParamDTO);
String getSmartSearchRiver(SearchPointParamDTO searchIndexParam);
}
@Service
@Slf4j
@RequiredArgsConstructor
@DS("master")
public class SearchIndexServiceImpl extends ServiceImpl implements SearchIndexService {
@Resource
private SearchIndexMapper searchIndexMapper;
@Resource
private DataSourceService dataSourceService;
@Resource
private DynamicDataSourcesService dynamicDataSourcesService;
@Override
public Page getSearchIndexByPage(SearchIndexParamDTO searchIndexParamDTO) {
if (searchIndexParamDTO.getSearchContent() != null) {
boolean isPingyin = KeyWordUtils.isContainsPinyin(searchIndexParamDTO.getSearchContent());
if (isPingyin) {
searchIndexParamDTO.setSearchContent(KeyWordUtils.getPinyin(searchIndexParamDTO.getSearchContent(), CommonConstant.SPACE));
} else {
searchIndexParamDTO.setSearchContent(HanLpAdvancedUtil.extractKeywords(searchIndexParamDTO.getSearchContent()));
}
}
if (ObjectUtil.isNotEmpty(searchIndexParamDTO.getGeoJson())) {
List> polygonCoordinates = GeoJsonUtil.extractPolygonCoordinates(searchIndexParamDTO.getGeoJson());
String polygonWKT = GeoJsonUtil.convertGeoJsonToWKT(polygonCoordinates);
searchIndexParamDTO.setPolygonWKT(polygonWKT);
}
return searchIndexMapper.getSearchIndexByPage(searchIndexParamDTO.build(), searchIndexParamDTO);
}
@Override
public SearchIndexDTO getSearchIndexById(SearchIndexParamDTO searchIndexParamDTO) {
if (StrUtil.isEmpty(searchIndexParamDTO.getPkId())) {
throw new CoreException(ErrorCodeEnum.SYS_ERROR, "查询参数缺失");
}
SearchIndex searchIndex = searchIndexMapper.selectById(searchIndexParamDTO.getPkId());
String sql = "select * from " + searchIndex.getEntityTable() + " where " + searchIndex.getEntityKeyColumn() + " = ?";
DataSourceDTO dataSourceDTO = dataSourceService.findById(searchIndex.getEntitySourceId());
List columnList = dynamicDataSourcesService.getDbColumnList(dataSourceDTO, searchIndex.getEntityTable());
List