¿Cómo configuro la interfaz Post SearchResult en el valor de retorno de la búsqueda?
No puedo configurar la interfaz para obtener los resultados de retorno Puede revisar el código a continuación
dependencias
"@nestjs/elasticsearch": "^8.1.0", "@elastic/elasticsearch": "^8.2.1", "@types/elasticsearch": "^5.0.40",publicación de interfaz
export interface PostSearchResultInterface { hits: { total: { value: number; }; hits: Array<{ total: { value: number; } _source: PostSearchBodyInterface; }>; }; }código
const search = await this.elasticsearchService.search<PostSearchBodyInterface>({ index: this.index, from: offset, size: limit, body: { query: { bool: { should: { multi_match: { query: text, fields: ['title', 'story', 'other_titles'] } }, filter: { range: { id: { gte: startId } } } }, }, sort: { id: { order: 'asc' } } } }) ; // error here let count = search.hits.total.value const hits = search.hits.hits; const results = hits.map((item) => item._source); return { count : startId ? separateCount : count, results }error TS2339: la propiedad 'valor' no existe en el tipo 'número | BuscarTotalAccesos'. La propiedad 'valor' no existe en el tipo 'número'.
ERROR: let count = search.hits.total.valueAsumiendo que las dependencias son (como se publicó la pregunta):
"@nestjs/elasticsearch": "^8.1.0", "@elastic/elasticsearch": "^8.2.1", "@types/elasticsearch": "^5.0.40",Primero, cree un tipo dedicado para el documento indexado, por ejemplo:
export type Document = { title: string, tags: string[] };Luego, simplemente busque documentos:
const a = await this.elastic.search<Document>({ //...request... }); const total = a.hits.total; const documents = a.hits.hits.map(document => { // query metadata fields returned by elastic document._id; // get fields for <Document> type document._source.title... document._source.tags... })