From 569ef08f1fb50a85dc023aded7e4de70a2cd3143 Mon Sep 17 00:00:00 2001 From: danny-mhlv Date: Tue, 16 Aug 2022 19:22:27 +0300 Subject: [PATCH] Minor code cleanup. --- src/core/domain/dtos/es-hit.dto.ts | 2 +- src/core/domain/dtos/index.ts | 11 +++-- src/core/domain/dtos/paper.dto.ts | 2 - src/core/domain/dtos/request.dto.ts | 2 +- src/core/domain/dtos/search-q.dto.ts | 2 +- src/core/domain/dtos/search-result.dto.ts | 2 +- src/core/domain/enums/es-time.enum.ts | 27 +++++++++++++ src/core/domain/enums/index.ts | 3 +- src/core/domain/enums/page-order.enum.ts | 7 ++++ src/core/domain/index.ts | 4 +- .../interfaces/es-query-string.interface.ts | 5 --- src/core/domain/interfaces/index.ts | 7 +++- src/core/interceptors/page.interceptor.ts | 4 +- src/core/services/common/search.service.ts | 40 +------------------ src/infrastructure/config/env.validation.ts | 2 +- src/infrastructure/modules/index.ts | 1 + src/infrastructure/modules/search.module.ts | 2 +- src/main.ts | 3 ++ 18 files changed, 66 insertions(+), 60 deletions(-) diff --git a/src/core/domain/dtos/es-hit.dto.ts b/src/core/domain/dtos/es-hit.dto.ts index 1334729..37af015 100644 --- a/src/core/domain/dtos/es-hit.dto.ts +++ b/src/core/domain/dtos/es-hit.dto.ts @@ -1,5 +1,5 @@ import { ApiProperty } from "@nestjs/swagger"; -import { IsArray, IsDefined, IsIn, IsInt, IsNotEmpty, IsOptional, IsString } from "class-validator"; +import { IsNotEmpty, IsOptional } from "class-validator"; import { PaperDto } from "./paper.dto"; /** diff --git a/src/core/domain/dtos/index.ts b/src/core/domain/dtos/index.ts index 32eeca0..09dfd59 100644 --- a/src/core/domain/dtos/index.ts +++ b/src/core/domain/dtos/index.ts @@ -1,3 +1,8 @@ -export * from './es-response.dto' -export * from './page.dto' -export * from './search-q.dto' \ No newline at end of file +export * from './es-query.dto'; +export * from './es-response.dto'; +export * from './es-hit.dto'; +export * from './page.dto'; +export * from './search-q.dto'; +export * from './search-result.dto'; +export * from './paper.dto'; +export * from './request.dto'; \ No newline at end of file diff --git a/src/core/domain/dtos/paper.dto.ts b/src/core/domain/dtos/paper.dto.ts index 5ee8e60..bd61c37 100644 --- a/src/core/domain/dtos/paper.dto.ts +++ b/src/core/domain/dtos/paper.dto.ts @@ -1,7 +1,5 @@ import { ApiProperty } from "@nestjs/swagger"; import { IsArray, IsDefined, IsIn, IsInt, IsNotEmpty, IsOptional, IsString } from "class-validator"; -import { EsQueryDto } from "./es-query.dto"; -import { SearchQueryDto } from "./search-q.dto"; /** * List of allowed properties in this DTO diff --git a/src/core/domain/dtos/request.dto.ts b/src/core/domain/dtos/request.dto.ts index fffc0ea..8fe1a0f 100644 --- a/src/core/domain/dtos/request.dto.ts +++ b/src/core/domain/dtos/request.dto.ts @@ -1,5 +1,5 @@ import { ApiProperty } from "@nestjs/swagger"; -import { IsDefined, IsIn, IsInt, IsNotEmpty, IsOptional, IsString } from "class-validator"; +import { IsDefined, IsNotEmpty, IsOptional } from "class-validator"; import { EsQueryDto } from "./es-query.dto"; import { SearchQueryDto } from "./search-q.dto"; diff --git a/src/core/domain/dtos/search-q.dto.ts b/src/core/domain/dtos/search-q.dto.ts index 28bf733..e5ad8eb 100644 --- a/src/core/domain/dtos/search-q.dto.ts +++ b/src/core/domain/dtos/search-q.dto.ts @@ -1,5 +1,5 @@ import { ApiProperty } from "@nestjs/swagger"; -import { IsDefined, IsIn, IsInt, IsNotEmpty, IsOptional, IsString } from "class-validator"; +import { IsDefined, IsInt, IsNotEmpty, IsOptional, IsString } from "class-validator"; /** * List of allowed properties in this DTO diff --git a/src/core/domain/dtos/search-result.dto.ts b/src/core/domain/dtos/search-result.dto.ts index 5097f64..c1c42bd 100644 --- a/src/core/domain/dtos/search-result.dto.ts +++ b/src/core/domain/dtos/search-result.dto.ts @@ -1,5 +1,5 @@ import { ApiProperty } from "@nestjs/swagger"; -import { IsArray, IsDefined, IsInt, IsNotEmpty, IsOptional, IsString } from "class-validator"; +import { IsArray, IsDefined, IsInt, IsNotEmpty } from "class-validator"; import { EsResponseDto } from "./es-response.dto"; /** diff --git a/src/core/domain/enums/es-time.enum.ts b/src/core/domain/enums/es-time.enum.ts index 7c5a5b6..8429795 100644 --- a/src/core/domain/enums/es-time.enum.ts +++ b/src/core/domain/enums/es-time.enum.ts @@ -2,11 +2,38 @@ * Elasticsearch time-units */ export enum EsTime { + /** + * Days + */ days = 'd', + + /** + * Hours + */ hours = 'h', + + /** + * Minutes + */ min = 'm', + + /** + * Seconds + */ sec = 's', + + /** + * Milliseconds + */ ms = 'ms', + + /** + * Microseconds + */ us = 'micros', + + /** + * Nanoseconds + */ ns = 'nanos' } \ No newline at end of file diff --git a/src/core/domain/enums/index.ts b/src/core/domain/enums/index.ts index dd8d8c9..8cb24ea 100644 --- a/src/core/domain/enums/index.ts +++ b/src/core/domain/enums/index.ts @@ -1,3 +1,4 @@ export * from './httpResponse' export * from './roles.enum' -export * from './page-order.enum' \ No newline at end of file +export * from './page-order.enum' +export * from './es-time.enum' \ No newline at end of file diff --git a/src/core/domain/enums/page-order.enum.ts b/src/core/domain/enums/page-order.enum.ts index 942cd68..ff4a505 100644 --- a/src/core/domain/enums/page-order.enum.ts +++ b/src/core/domain/enums/page-order.enum.ts @@ -2,6 +2,13 @@ * Page display order */ export enum Order { + /** + * Ascending order + */ ASC = 'asc', + + /** + * Descending order + */ DESC = 'desc', } \ No newline at end of file diff --git a/src/core/domain/index.ts b/src/core/domain/index.ts index cea7a31..5806f60 100644 --- a/src/core/domain/index.ts +++ b/src/core/domain/index.ts @@ -1 +1,3 @@ -export * from './enums' \ No newline at end of file +export * from './enums' +export * from './dtos' +export * from './interfaces' \ No newline at end of file diff --git a/src/core/domain/interfaces/es-query-string.interface.ts b/src/core/domain/interfaces/es-query-string.interface.ts index 55c1ee6..5f6dcc8 100644 --- a/src/core/domain/interfaces/es-query-string.interface.ts +++ b/src/core/domain/interfaces/es-query-string.interface.ts @@ -18,9 +18,4 @@ * Can't be specified with 'default_field' */ fields?: string[]; - - /** - * - */ - } \ No newline at end of file diff --git a/src/core/domain/interfaces/index.ts b/src/core/domain/interfaces/index.ts index 8c2a188..c751d45 100644 --- a/src/core/domain/interfaces/index.ts +++ b/src/core/domain/interfaces/index.ts @@ -1,4 +1,7 @@ -export * from './http-response.interface'; +export * from './http-response.interface' export * from './page-meta.interface' export * from './es-query.interface' -export * from './es-query-string.interface' \ No newline at end of file +export * from './es-query-string.interface' +export * from './es-response-hits.interface' +export * from './es-pit.interface' +export * from './search-info.interface' \ No newline at end of file diff --git a/src/core/interceptors/page.interceptor.ts b/src/core/interceptors/page.interceptor.ts index adb895e..5f9d913 100644 --- a/src/core/interceptors/page.interceptor.ts +++ b/src/core/interceptors/page.interceptor.ts @@ -100,7 +100,9 @@ export class PageInterceptor implements NestInterceptor { request.es_query.size += limit - 1; reverse = true; } else if (query.page == this.prevSearch.prevPage) { - //... + // Caching should be HERE + request.es_query.sort = [{ _score: { order: 'asc' } }]; + reverse = true; } } else { this.prevSearch.pit = request.es_query.pit = await this.getPIT(1); diff --git a/src/core/services/common/search.service.ts b/src/core/services/common/search.service.ts index bcb3278..ea006f9 100644 --- a/src/core/services/common/search.service.ts +++ b/src/core/services/common/search.service.ts @@ -4,8 +4,6 @@ import { map, take } from "rxjs"; import { EsResponseDto } from "src/core/domain/dtos"; import { EsQueryDto } from "src/core/domain/dtos/es-query.dto"; import { SearchResultDto } from "src/core/domain/dtos/search-result.dto"; -import { EsTime } from "src/core/domain/enums/es-time.enum"; -import { EsPit } from "src/core/domain/interfaces/es-pit.interface"; /** * Search service provider @@ -87,40 +85,4 @@ export class SearchService { } }); } -} - -// let ESQ: EsQueryDto = new EsQueryDto; - - // if (limit) ESQ.size = limit; - // ESQ.query = { - // query_string: { - // query: query_str, - // default_field: 'content', - // } - // } - // this.getPIT(1).then((pit) => { - // ESQ.pit = pit; - // }); - -/** - * Context - * // let es_query = { // DTO - // query: { // Interface - // query_string: { // Interface - // query: query_str, - // default_field: "content" - // } - // }, - // } - */ - -/** - * Single - * // let es_query = { - // query: { - // query_string: { - // query: 'id:' + uuid - // } - // }, - // } - */ \ No newline at end of file +} \ No newline at end of file diff --git a/src/infrastructure/config/env.validation.ts b/src/infrastructure/config/env.validation.ts index c56d8d9..c92753c 100644 --- a/src/infrastructure/config/env.validation.ts +++ b/src/infrastructure/config/env.validation.ts @@ -1,5 +1,5 @@ import { plainToClass } from 'class-transformer'; -import { validateSync, IsOptional } from 'class-validator'; +import { validateSync } from 'class-validator'; /** * env vatiables diff --git a/src/infrastructure/modules/index.ts b/src/infrastructure/modules/index.ts index da53f6a..c80e102 100644 --- a/src/infrastructure/modules/index.ts +++ b/src/infrastructure/modules/index.ts @@ -1 +1,2 @@ export * from './app.module'; +export * from './search.module' \ No newline at end of file diff --git a/src/infrastructure/modules/search.module.ts b/src/infrastructure/modules/search.module.ts index bccc4e6..cbb486d 100644 --- a/src/infrastructure/modules/search.module.ts +++ b/src/infrastructure/modules/search.module.ts @@ -4,7 +4,7 @@ import { PapersController } from "src/application"; import { SearchService } from "../../core/services/common/search.service"; /** - * search module + * Search module */ @Module({ imports: [ diff --git a/src/main.ts b/src/main.ts index 4d8d5c2..eff9b48 100644 --- a/src/main.ts +++ b/src/main.ts @@ -19,6 +19,9 @@ async function bootstrap() { }) ); + /** + * Enabling URI-type versioning of the API + */ app.enableVersioning({ type: VersioningType.URI, });