Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to do using gin (<column2> gin_trgm_ops, <column2>, <column3>); with Kysely? #1190

Open
lancejpollard opened this issue Oct 22, 2024 · 1 comment

Comments

@lancejpollard
Copy link

lancejpollard commented Oct 22, 2024

How can you write this sort of query with Kysely?

CREATE INDEX idx_words_lang_phon ON searches 
USING gin (text gin_trgm_ops, language_id, is_phonetic);

I know you can do this so far:

await db.schema
  .createIndex('searches_text_index')
  .on('searches')
  .using('gin')
  .column('text')
  .execute()

But how about specifying the gin_trgm_ops and the rest of the columns, is there a nice way? Perhaps:

await db.schema
  .createIndex('searches_text_with_other_columns_index')
  .on('searches')
  .using('gin')
  .columns(['text gin_trgm_ops', 'language_id', 'is_phonetic'])
  .execute()

ClaudeAI is suggesting these two:

await db.schema
  .raw(`
    CREATE INDEX idx_words_lang_phon ON searches 
    USING gin (text gin_trgm_ops, language_id, is_phonetic)
  `);

await db.schema.createIndex('idx_words_lang_phon')
  .on('searches')
  .expression(sql`(text gin_trgm_ops, language_id, is_phonetic) USING gin`)
  .execute()

Any of these work or ideal? What is recommended?

And same for this:

CREATE INDEX idx_words_prefix_fuzzy ON searches 
USING gin (text gin_trgm_ops)
WHERE length(text) >= 3;

Is that just this? (Is there a better way)?

await db.schema
  .createIndex('idx_words_prefix_fuzzy')
  .on('searches')
  .using('gin')
  .column('text')
  .where('length(text)', '>=', 3)
  .execute()

Also (tangent), do I need to do this somewhere (and how to do it in Kysely)?

CREATE EXTENSION IF NOT EXISTS pg_trgm;

Thanks for the help.

@lancejpollard
Copy link
Author

await createGinIndex(
  db,
  'searches_text_language_gin_index',
  'searches',
  ['language_id', 'text gin_trgm_ops'],
  'WHERE is_phonetic = FALSE',
)

async function createGinIndex(
  db: Kysely<any>,
  indexName: string,
  tableName: string,
  columns: Array<string>,
  extra: string
) {
  const columnsStr = columns.join(", ");
  const query = sql`CREATE INDEX ${indexName} ON ${tableName} USING gin (${columnsStr}) ${extra}`;
  console.log(query);
  await query.execute(db);
  // let builder = db.schema
  //   .createIndex(indexName)
  //   .on(tableName)
  //   .using('gin')
  //   .expression(sql`USING gin (${columnsStr})`)
  // if (cb) {
  //   builder = cb(builder)
  // }
  // return await builder.execute()
}

Error:

RawBuilderImpl {}
failed to execute migration "foo_0001"
failed to migrate
error: syntax error at or near "$1"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant