Auxílio RS搜索功能优化:快速定位避难所资源
在暴雨和洪水等紧急情况下,快速找到可用的避难所资源可能关系到生命安全。Auxílio RS项目的避难所搜索功能经过优化后,现在能够帮助救援人员和受灾群众更高效地定位所需资源。本文将详细介绍这些优化措施及其实现方式。
搜索功能架构概述
优化后的搜索功能主要由以下核心组件构成:
- ShelterSearch类:实现搜索逻辑的核心类,位于src/shelter/ShelterSearch.ts
- 搜索参数验证:使用Zod模式验证搜索输入,确保数据有效性,定义在src/shelter/types/search.types.ts
- 避难所服务:协调搜索过程并处理结果,实现于src/shelter/shelter.service.ts
- 数据模型:定义避难所和相关资源的数据结构,位于prisma/schema.prisma
多维度搜索条件
新的搜索功能支持多种筛选条件组合,让用户能够精确找到所需资源:
地理位置搜索
用户可以通过提供经纬度和搜索半径,找到附近的避难所。系统会自动计算地理边界:
// 地理边界计算逻辑
const { minLat, maxLat, minLong, maxLong } = calculateGeolocationBounds(
this.formProps.geolocation,
);
return {
latitude: { gte: minLat, lte: maxLat },
longitude: { gte: minLong, lte: maxLong },
};
状态筛选
避难所状态分为三类:
available:有可用容量的避难所unavailable:已满的避难所waiting:等待确认状态
状态筛选实现于ShelterSearch.ts的shelterStatus getter方法中。
资源优先级
根据避难所的紧急需求程度进行筛选,支持多种优先级组合搜索:
// 优先级筛选逻辑
priority(supplyIds: string[] = []): Prisma.ShelterWhereInput {
if (!this.formProps.priorities?.length) return {};
return {
shelterSupplies: {
some: {
priority: { in: this.formProps.priorities },
supplyId: supplyIds.length > 0 ? { in: supplyIds } : undefined,
},
},
};
}
搜索性能优化
为了在紧急情况下提供快速响应,搜索功能进行了多方面的性能优化:
全文搜索优化
使用PostgreSQL的unaccent扩展实现高效的全文搜索,忽略重音符号并支持模糊匹配:
// 全文搜索实现
async getSearch(): Promise<Prisma.ShelterWhereInput> {
if (!this.formProps.search) return {};
const search = `${this.formProps.search.toLowerCase()}`;
const results = await this.prismaService.$queryRaw<{ id: string }[]>(
Prisma.sql`SELECT id FROM shelters WHERE lower(unaccent(address)) LIKE '%' || unaccent(${search}) || '%' OR lower(unaccent(name)) LIKE '%' || unaccent(${search}) || '%';`,
);
return { id: { in: results.map((r) => r.id) } };
}
数据索引
在数据库层面,为常用搜索字段建立了索引,特别是地理位置和状态字段,大幅提升查询速度。
搜索结果处理
搜索结果经过多层处理,确保用户获得最相关和最新的信息:
标签化响应
系统会自动为搜索结果添加标签,标识避难所的资源需求类型:
// 标签处理逻辑
function parseTagResponse(
tagProps: Partial<Pick<ShelterSearchProps, 'tags'>> = {},
results: SearchShelterTagResponse[],
voluntaryIds: string[],
): SearchShelterTagResponse[] {
const tags: ShelterTagInfo = {
...defaultTagsData,
...(tagProps?.tags ?? {}),
};
// 为每个避难所添加标签
const parsed = results.map((result) => {
return {
...result,
shelterSupplies: result.shelterSupplies.reduce((prev, shelterSupply) => {
const supplyTags: ShelterTagType[] = [];
// 根据优先级添加需求标签
if (tags.NeedDonations && [SupplyPriority.Needing, SupplyPriority.Urgent].includes(shelterSupply.priority)) {
supplyTags.push('NeedDonations');
}
// 志愿者需求标签
if (tags.NeedVolunteers && voluntaryIds.includes(shelterSupply.supply.supplyCategoryId) &&
[SupplyPriority.Urgent, SupplyPriority.Needing].includes(shelterSupply.priority)) {
supplyTags.push('NeedVolunteers');
}
// 剩余物资标签
if (tags.RemainingSupplies && [SupplyPriority.Remaining].includes(shelterSupply.priority)) {
supplyTags.push('RemainingSupplies');
}
return [...prev, { ...shelterSupply, tags: supplyTags }];
}, [] as any),
};
});
return parsed;
}
资源状态衰减机制
系统会自动更新资源优先级,确保显示的信息反映最新的需求状态:
// 资源状态衰减处理
private async decayShelterSupply(shelterSupplies: ShelterSupply[]) {
// 紧急状态超过48小时降级为需要状态
this.handleDecayShelterSupply(
shelterSupplies
.map(this.parseShelterSupply)
.filter((f) => this.canDecayShelterSupply(f, [SupplyPriority.Urgent], 48)),
SupplyPriority.Needing
);
// 需要状态超过72小时降级为受控状态
this.handleDecayShelterSupply(
shelterSupplies
.map(this.parseShelterSupply)
.filter((f) => this.canDecayShelterSupply(
f, [SupplyPriority.Needing, SupplyPriority.Remaining], 72)),
SupplyPriority.UnderControl
);
}
使用示例
以下是一个完整的搜索请求示例,展示如何组合多种条件查找避难所:
// 搜索参数示例
const searchParams = {
search: "学校",
geolocation: {
latitude: -29.9957,
longitude: -51.1700,
radiusInMeters: 10000
},
shelterStatus: ["available"],
priorities: [SupplyPriority.Urgent, SupplyPriority.Needing],
cities: ["Porto Alegre"]
};
// 执行搜索
const shelterService = new ShelterService(prismaService);
const results = await shelterService.index(searchParams);
总结
Auxílio RS的搜索功能优化显著提升了紧急情况下定位避难所资源的效率。通过多维度筛选、性能优化和智能结果处理,救援人员和受灾群众能够快速找到最需要的资源。
核心代码实现:
这些优化不仅提高了搜索速度和准确性,还确保了资源信息的及时性和相关性,为应对暴雨和洪水等紧急情况提供了有力支持。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考



