Auxílio RS搜索功能优化:快速定位避难所资源

Auxílio RS搜索功能优化:快速定位避难所资源

【免费下载链接】backend Auxílio RS: Projetos de Resposta a Emergências por Chuvas e Alagamentos 【免费下载链接】backend 项目地址: https://gitcode.com/GitHub_Trending/bac/backend

在暴雨和洪水等紧急情况下,快速找到可用的避难所资源可能关系到生命安全。Auxílio RS项目的避难所搜索功能经过优化后,现在能够帮助救援人员和受灾群众更高效地定位所需资源。本文将详细介绍这些优化措施及其实现方式。

搜索功能架构概述

优化后的搜索功能主要由以下核心组件构成:

多维度搜索条件

新的搜索功能支持多种筛选条件组合,让用户能够精确找到所需资源:

地理位置搜索

用户可以通过提供经纬度和搜索半径,找到附近的避难所。系统会自动计算地理边界:

// 地理边界计算逻辑
const { minLat, maxLat, minLong, maxLong } = calculateGeolocationBounds(
  this.formProps.geolocation,
);

return {
  latitude: { gte: minLat, lte: maxLat },
  longitude: { gte: minLong, lte: maxLong },
};

状态筛选

避难所状态分为三类:

  • available:有可用容量的避难所
  • unavailable:已满的避难所
  • waiting:等待确认状态

状态筛选实现于ShelterSearch.tsshelterStatus 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的搜索功能优化显著提升了紧急情况下定位避难所资源的效率。通过多维度筛选、性能优化和智能结果处理,救援人员和受灾群众能够快速找到最需要的资源。

核心代码实现:

这些优化不仅提高了搜索速度和准确性,还确保了资源信息的及时性和相关性,为应对暴雨和洪水等紧急情况提供了有力支持。

【免费下载链接】backend Auxílio RS: Projetos de Resposta a Emergências por Chuvas e Alagamentos 【免费下载链接】backend 项目地址: https://gitcode.com/GitHub_Trending/bac/backend

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值