src/core/services/common/search.service.ts
Search service provider
Properties |
|
Methods |
|
constructor(httpService: HttpService)
|
||||||
Constructs the service with injection of HTTPService instance
Parameters :
|
Async findByContext | ||||||
findByContext(query_str: string)
|
||||||
Finds relevant documents by context using the given query string
Parameters :
Returns :
Promise<SearchResultDto>
Elasticsearch hits or an error object |
Async findByID | ||||||
findByID(uuid: string)
|
||||||
Finds a paper by its own ID
Parameters :
Returns :
Promise<SearchResultDto>
Elasticsearch hits or an error object |
Private Readonly ES_PORT |
Default value : process.env.ES_PORT
|
Elastichsearch server port-number |
import { HttpService } from "@nestjs/axios";
import { Injectable } from "@nestjs/common";
import { map, take } from "rxjs";
import { EsResponseDto } from "src/core/domain/dtos";
import { SearchResultDto } from "src/core/domain/dtos/search-result.dto";
/**
* Search service provider
*/
@Injectable()
export class SearchService {
/**
* Constructs the service with injection of
* HTTPService instance
* @param httpService
*/
constructor(private readonly httpService: HttpService) {}
/**
* Elastichsearch server port-number
*/
private readonly ES_PORT = process.env.ES_PORT;
/**
* Finds a paper by its own ID
* @param uuid
* @returns Elasticsearch hits or an error object
*/
async findByID(uuid: string): Promise<SearchResultDto> { // Should I change 'object' to specific DTO?
let es_query = {
query: {
query_string: {
query: 'id:' + uuid
}
}
}
return new Promise((resolve, reject) => {
try {
(this.httpService.get<EsResponseDto>('http://localhost:' + this.ES_PORT + '/_search', {
data: es_query,
headers: {'Content-Type': 'application/json'},
}))
.pipe(take(1), map(axiosRes => axiosRes.data))
.subscribe((res: any) => {
if (res.timed_out) {
reject(new SearchResultDto(504, {message: 'Timed Out'}));
}
if (!res.hits.hits.length) {
reject(new SearchResultDto(404, {message: 'Not Found'}));
}
resolve(new SearchResultDto(200, res.hits));
});
} catch (error) {
reject(new SearchResultDto(700, error));
}
});
}
/**
* Finds relevant documents by context using the given query string
* @param query_str
* @returns Elasticsearch hits or an error object
*/
async findByContext(query_str: string): Promise<SearchResultDto> {
let es_query = {
query: {
query_string: {
query: query_str,
default_field: "content"
}
}
}
return new Promise((resolve, reject) => {
try {
(this.httpService.get<EsResponseDto>('http://localhost:'+ this.ES_PORT + '/_search', {
data: es_query,
headers: {'Content-Type': 'application/json'},
}))
.pipe(take(1), map(axiosRes => axiosRes.data))
.subscribe((res: any) => {
if (res.timed_out) {
reject(new SearchResultDto(504, {status: 504, message: 'Timed Out'}));
}
if (!res.hits.hits.length) {
reject(new SearchResultDto(404, {status: 404, message: 'Not Found'}));
}
resolve(new SearchResultDto(200, res.hits));
});
} catch (error) {
reject(new SearchResultDto(700, error));
}
});
}
}