feat(search): implement Search module with Typesense full-text & geo search

- TypesenseClient service with configurable connection
- Collection schema for listings with facets, geo-point, and Vietnamese text
- ListingIndexer service with PostGIS coordinate extraction for geo search
- CQRS commands: SyncListing, ReindexAll (batch with pagination)
- CQRS queries: SearchProperties (filters, sorting), GeoSearch (radius)
- Event handlers for listing.approved/updated/deactivated auto-sync
- REST endpoints: GET /search, GET /search/geo, POST /search/reindex (admin)
- DTOs with class-validator validation and pagination

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
Ho Ngoc Hai
2026-04-08 01:46:20 +07:00
parent 0b29fac35e
commit 6741592cbe
31 changed files with 1143 additions and 0 deletions

View File

@@ -0,0 +1,72 @@
import {
Controller,
Get,
Post,
Query,
UseGuards,
} from '@nestjs/common';
import { CommandBus, QueryBus } from '@nestjs/cqrs';
import { SearchPropertiesQuery } from '../../application/queries/search-properties/search-properties.query';
import { GeoSearchQuery } from '../../application/queries/geo-search/geo-search.query';
import { ReindexAllCommand } from '../../application/commands/reindex-all/reindex-all.command';
import { SearchPropertiesDto } from '../dto/search-properties.dto';
import { GeoSearchDto } from '../dto/geo-search.dto';
import { JwtAuthGuard } from '@modules/auth/presentation/guards/jwt-auth.guard';
import { RolesGuard } from '@modules/auth/presentation/guards/roles.guard';
import { Roles } from '@modules/auth/presentation/decorators/roles.decorator';
import { type SearchResult } from '../../domain/repositories/search.repository';
import { type ReindexResult } from '../../application/commands/reindex-all/reindex-all.handler';
@Controller('search')
export class SearchController {
constructor(
private readonly commandBus: CommandBus,
private readonly queryBus: QueryBus,
) {}
@Get()
async search(@Query() dto: SearchPropertiesDto): Promise<SearchResult> {
return this.queryBus.execute(
new SearchPropertiesQuery(
dto.q,
dto.propertyType,
dto.transactionType,
dto.priceMin,
dto.priceMax,
dto.areaMin,
dto.areaMax,
dto.bedrooms,
dto.district,
dto.city,
dto.sortBy,
dto.page,
dto.perPage,
),
);
}
@Get('geo')
async geoSearch(@Query() dto: GeoSearchDto): Promise<SearchResult> {
return this.queryBus.execute(
new GeoSearchQuery(
dto.lat,
dto.lng,
dto.radiusKm,
dto.propertyType,
dto.transactionType,
dto.priceMin,
dto.priceMax,
dto.sortBy,
dto.page,
dto.perPage,
),
);
}
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('ADMIN')
@Post('reindex')
async reindex(): Promise<ReindexResult> {
return this.commandBus.execute(new ReindexAllCommand());
}
}