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,63 @@
import { Module, type OnModuleInit } from '@nestjs/common';
import { CqrsModule } from '@nestjs/cqrs';
// Domain
import { SEARCH_REPOSITORY } from './domain/repositories/search.repository';
// Infrastructure
import { TypesenseClientService } from './infrastructure/services/typesense-client.service';
import { TypesenseSearchRepository } from './infrastructure/services/typesense-search.repository';
import { ListingIndexerService } from './infrastructure/services/listing-indexer.service';
import { ListingApprovedEventHandler } from './infrastructure/event-handlers/listing-approved.handler';
// Application
import { SyncListingHandler } from './application/commands/sync-listing/sync-listing.handler';
import { ReindexAllHandler } from './application/commands/reindex-all/reindex-all.handler';
import { SearchPropertiesHandler } from './application/queries/search-properties/search-properties.handler';
import { GeoSearchHandler } from './application/queries/geo-search/geo-search.handler';
// Presentation
import { SearchController } from './presentation/controllers/search.controller';
import { LoggerService } from '@modules/shared/infrastructure/logger.service';
const CommandHandlers = [SyncListingHandler, ReindexAllHandler];
const QueryHandlers = [SearchPropertiesHandler, GeoSearchHandler];
@Module({
imports: [CqrsModule],
controllers: [SearchController],
providers: [
// Infrastructure
TypesenseClientService,
{ provide: SEARCH_REPOSITORY, useClass: TypesenseSearchRepository },
ListingIndexerService,
// Event handlers
ListingApprovedEventHandler,
// CQRS
...CommandHandlers,
...QueryHandlers,
],
exports: [ListingIndexerService, SEARCH_REPOSITORY],
})
export class SearchModule implements OnModuleInit {
constructor(
private readonly typesenseClient: TypesenseClientService,
private readonly searchRepo: TypesenseSearchRepository,
private readonly logger: LoggerService,
) {}
async onModuleInit(): Promise<void> {
try {
await this.searchRepo.ensureCollection();
this.logger.log('Search module initialized — Typesense collection ready', 'SearchModule');
} catch (err) {
this.logger.error(
`Failed to initialize Typesense collection: ${err instanceof Error ? err.message : String(err)}`,
'SearchModule',
);
}
}
}