Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

223
Views
Node.js Sequelize Postgres función de búsqueda relacionada

Estoy tratando de crear una función de búsqueda que también pueda mostrar búsquedas relacionadas. Por ejemplo. Si trato de buscar 'árboles', entonces debe encontrarme todas las filas que contienen árboles. y que lo escribo mal, entonces también debe mostrarme algunos resultados. Por ejemplo. 'treesabc' también debe mostrarme algunos resultados. Por ahora estoy usando el método Like que satisface mi primera necesidad, pero funciona cuando se ingresan letras adicionales.

Así que aquí está mi código.

 exports.quickSearch = async (req, res) => { let services = []; // finding all services which matches the condition let serviceData = await Service.findAll({ limit: 300, // limiting the number of search where: { // here i want to to do something which can also show related searches [Op.or]: [ { service_name: sequelize.where( sequelize.fn("LOWER", sequelize.col("service_name")), "LIKE", "%" + req.params.keyword.toLowerCase() + "%", ), }, { category: sequelize.where( sequelize.fn("LOWER", sequelize.col("category")), "LIKE", "%" + req.params.keyword.toLowerCase() + "%", ), }, { sub_category: sequelize.where( sequelize.fn("LOWER", sequelize.col("sub_category")), "LIKE", "%" + req.params.keyword.toLowerCase() + "%", ), }, { master_category: sequelize.where( sequelize.fn("LOWER", sequelize.col("master_category")), "LIKE", "%" + req.params.keyword.toLowerCase() + "%", ), }, { gender: sequelize.where( sequelize.fn("LOWER", sequelize.col("gender")), "LIKE", "%" + req.params.keyword.toLowerCase() + "%", ), }, ], }, }); // custom function asyncForEach const asyncForEach = async (array, callback) => { for (let i = 0; i < array.length; i++) { await callback(array[i]); } }; await asyncForEach(serviceData, async (service) => { // trying to find all te partner which has service_id let partnerData = await Partner.findOne({ where: { partner_id: service.partner_id, banned: false, }, raw: true, }); if (partnerData) { // inserting partners data in service partnerData.password = null; service.dataValues.outletdata = partnerData; services.push(service); } }); return res .status(200) .json({ message: "Success", services}); };
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Prueba con Op.like :

Ejemplo:

 queryParams.where = { [Op.or]: [ Sequelize.where(Sequelize.fn('lower', Sequelize.col('t_device.name')), { [Op.like]: `%${optionsWithDefault.search}%`, }), Sequelize.where(Sequelize.fn('lower', Sequelize.col('t_device.external_id')), { [Op.like]: `%${optionsWithDefault.search}%`, }),

Enlace de referencia: https://www.tabnine.com/code/javascript/functions/sequelize/SequelizeStatic/fn

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!