© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•13mo ago
rumack

Define a full-text search index using raw SQL concatenation

Hello, I have a
posts
posts
schema that looks like this:
export const post_schema = pgTable(
    'posts',
    {
        id: serial('id').primaryKey(),
        slug: text('slug').notNull(),
        heading: text('heading').notNull(),
        subheading: text('subheading').notNull(),
        image: text('image').notNull(),
        caption: text('caption').notNull(),
        author_id: integer('author_id'),
        tags: text('tags').array().notNull(),
        article_html: text('article_html').notNull(),
        article_text: text('article_text').notNull(),
        published: boolean('published').default(false).notNull(),
        date_published: timestamp('date_published'),
        updated_by: text('updated_by').notNull(),
        created_at: timestamp('created_at').defaultNow().notNull(),
        updated_at: timestamp('updated_at').$onUpdate(() => new Date())
    },
    (table) => [
        index('posts_search_index').using(
            'gin',
            sql`to_tsvector('english', coalesce(${table.article_text}, ''))`
        )
    ]
);
export const post_schema = pgTable(
    'posts',
    {
        id: serial('id').primaryKey(),
        slug: text('slug').notNull(),
        heading: text('heading').notNull(),
        subheading: text('subheading').notNull(),
        image: text('image').notNull(),
        caption: text('caption').notNull(),
        author_id: integer('author_id'),
        tags: text('tags').array().notNull(),
        article_html: text('article_html').notNull(),
        article_text: text('article_text').notNull(),
        published: boolean('published').default(false).notNull(),
        date_published: timestamp('date_published'),
        updated_by: text('updated_by').notNull(),
        created_at: timestamp('created_at').defaultNow().notNull(),
        updated_at: timestamp('updated_at').$onUpdate(() => new Date())
    },
    (table) => [
        index('posts_search_index').using(
            'gin',
            sql`to_tsvector('english', coalesce(${table.article_text}, ''))`
        )
    ]
);

It works fine but I'd like the index definition to refer to more than one field, for example:
(table) => [
    index('posts_search_index')
      .using(
        'gin',
        sql`to_tsvector('english', coalesce(${table.heading}, '')) ||
            to_tsvector('english', coalesce(${table.subheading}, '')) ||
            to_tsvector('english', coalesce(${table.article_text}, '')) ||
            to_tsvector('english', array_to_string(${table.tags}, ' '))`
      ),
  ]
(table) => [
    index('posts_search_index')
      .using(
        'gin',
        sql`to_tsvector('english', coalesce(${table.heading}, '')) ||
            to_tsvector('english', coalesce(${table.subheading}, '')) ||
            to_tsvector('english', coalesce(${table.article_text}, '')) ||
            to_tsvector('english', array_to_string(${table.tags}, ' '))`
      ),
  ]

But I keep getting a 42601 postgres error:
error: syntax error at or near "||"
error: syntax error at or near "||"
. Any pointers as to how I can make the concatination work? Thanks!
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Full Text Search
Drizzle TeamDTDrizzle Team / help
3y ago
How to define schemas (any dialect) with full text search column types?
Drizzle TeamDTDrizzle Team / help
2y ago
Define a index with condition
Drizzle TeamDTDrizzle Team / help
3y ago
Full-text search on jsonb column
Drizzle TeamDTDrizzle Team / help
3y ago