Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

147
Vistas
How to use typescript mapping for ElasticSearch response?

How do I set the Post SearchResult interface to the search return value

I can't set the interface to get the return results You can review the code below

dependencies

   "@nestjs/elasticsearch": "^8.1.0",
   "@elastic/elasticsearch": "^8.2.1",
   "@types/elasticsearch": "^5.0.40",

interface Post

export interface PostSearchResultInterface {
    hits: {
        total: {
            value: number;
        };
        hits: Array<{
            total: {
                value: number;
            }
            _source: PostSearchBodyInterface;
        }>;
    };
}

code

     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: Property 'value' does not exist on type 'number | SearchTotalHits'. Property 'value' does not exist on type 'number'.

ERROR: let count = search.hits.total.value
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Assuming dependencies are (as question posted) :

"@nestjs/elasticsearch": "^8.1.0",
"@elastic/elasticsearch": "^8.2.1",
"@types/elasticsearch": "^5.0.40",

First, create a dedicated type for the indexed document, for example :

export type Document = { 
  title: string, 
  tags: string[]
};

Then, simply search for documents :

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...
})
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda