Merge pull request 'Changed 'match' query to 'multi-match'. Allows to search for both 'content' and 'title' of the paper' (#17) from fix/search into develop

Reviewed-on: http://85.143.176.51:3000/free-land/backend/pulls/17
This commit is contained in:
moeidtopcoder 2022-11-09 09:14:38 +00:00
commit 762af0c3dd
2 changed files with 72 additions and 7 deletions

View File

@ -0,0 +1,68 @@
import { ApiExtraModels, ApiProperty, ApiPropertyOptional } from "@nestjs/swagger";
import { IsArray, IsDefined, IsInt, IsObject, IsOptional } from "class-validator";
import { EsPit } from "../../interfaces/elastic/es-pit.interface";
import { EsQuery } from "../../interfaces/elastic/es-query.interface"
/**
* List of allowed properties in this DTO
*/
const allowedProperties = ['query'];
/**
* Elasticsearch multi-match query DTO
*/
@ApiExtraModels()
export class EsMultimatchQueryDto {
/**
* Offset from the start of the list of hits
*/
@IsOptional()
@IsInt()
@ApiPropertyOptional({
description: 'Offset from the start of the list of hits',
example: 5,
})
public from?: number;
/**
* Maximum number of elements returned by Elasticsearch
*/
@IsOptional()
@IsInt()
@ApiPropertyOptional({
description: 'Maximum number of elements returned by Elasticsearch',
example: 30
})
public size?: number;
/**
* The search query object passed to Elasticsearch
*/
@IsDefined()
@IsObject()
@ApiProperty({
description: 'Search query object passed to Elasticsearch',
example: {
multi_match: {
query: 'Maths',
fields: [
'title',
'content'
]
}
},
})
private readonly query: Object;
/**
* Constructs a multi-match
*/
constructor(query: string = '', fields: Array<string> = ['content']) {
this.query = {
multi_match: {
query: query,
fields: fields
}
}
}
}

View File

@ -1,6 +1,7 @@
import { HttpService } from "@nestjs/axios"; import { HttpService } from "@nestjs/axios";
import { GatewayTimeoutException, ImATeapotException, Injectable, NotFoundException } from "@nestjs/common"; import { GatewayTimeoutException, ImATeapotException, Injectable, NotFoundException } from "@nestjs/common";
import { map, take } from "rxjs"; import { map, take } from "rxjs";
import { EsMultimatchQueryDto } from "src/core/domain/dtos/elastic/es-multimatch.dto";
import { EsResponseDto, SearchQueryDto} from "../../domain/dtos"; import { EsResponseDto, SearchQueryDto} from "../../domain/dtos";
import { EsQueryDto } from "../../domain/dtos/elastic/es-query.dto"; import { EsQueryDto } from "../../domain/dtos/elastic/es-query.dto";
@ -69,13 +70,9 @@ export class SearchService {
*/ */
async findByContext(query: SearchQueryDto): Promise<EsResponseDto> { async findByContext(query: SearchQueryDto): Promise<EsResponseDto> {
// Contruct a body for querying Elasticsearch // Contruct a body for querying Elasticsearch
const es_query: EsQueryDto = new EsQueryDto(); const es_query: EsMultimatchQueryDto = new EsMultimatchQueryDto(query.query, [
es_query.query = { 'title', 'content'
query_string: { ]);
query: query.query,
default_field: 'content',
}
};
es_query.from = query.offset; es_query.from = query.offset;
es_query.size = query.limit; es_query.size = query.limit;