Next.js Discord

Discord Forum

Vercel Postgres search query with spaces?

Unanswered
Netherland Dwarf posted this in #help-forum
Open in Discord
Avatar
Netherland DwarfOP
I am very new to this and trying to figure out how best searches querys can be handled. Right now any single word search query works but when I have two words it will not work.

import { sql } from '@vercel/postgres';

import { ProductFormat } from "../server/ProductFormat"

export async function pullProducts(page = 1, search = 'Two Words') {
  const pageSize = 24
  const pageOffset = (page - 1) * 24
  
  const result = await sql`
    SELECT * FROM table
    WHERE title LIKE ${`%${search}%`}
    ORDER BY title ASC
    LIMIT ${pageSize} OFFSET ${pageOffset}
  `;
  
  const totalProducts = await sql`
    SELECT COUNT(*) FROM table
    WHERE title LIKE ${`%${search}%`}
  `;

  const productsTyped = result.rows as ProductFormat[];

  return {
    products: productsTyped,
    totalProducts: parseInt(totalProducts.rows[0].count, 10)
  };
}

2 Replies

Avatar
hey, for an advanced full text search on postgres you can take a look at it's integrated FTS functionality: https://www.crunchydata.com/blog/postgres-full-text-search-a-search-engine-in-a-database
though i'm skeptical whether it could be applied on a vercel postgres database, i haven't used one so i can't give any guarantee that it'll work just fine there