website icon indicating copy to clipboard operation
website copied to clipboard

Full-Text Search using TSVector

Open harrysayers opened this issue 3 years ago • 4 comments
trafficstars

Issue Creation Checklist

[] I have read the contribution guidelines

Issue Description

I'm trying to use the Sequelize.TSVector to implement Full-Text search however there doesn't seem to be any documentation on how to use TSVector or how it is used in conjunction with Op.Match. I've seen various speculations on stack overflow on how it is to be used and I've tried them all with no success. It would be great to get some documentation on how to use TSVector for Full-text search.

What was unclear/insufficient/not covered in the documentation

There is not any documentation.

If possible: Provide some suggestion on how we can enhance the docs

Write here.

Additional context

Add any other context or screenshots about the issue here.

harrysayers avatar Apr 14 '22 18:04 harrysayers

Original post where it was recommended to open this issue regarding documentation -> https://github.com/sequelize/sequelize/pull/12955#issuecomment-1099513508

harrysayers avatar Apr 14 '22 18:04 harrysayers

Any update on this? :)

harrysayers avatar Jun 02 '22 15:06 harrysayers

Not yet, we're focus on getting Sequelize 7 ready at the moment

DataTypes.TSVector is used like this:

Creating a TSVector attribute:

class User {}

User.init({
  search: {
    type: DataTypes.TSVector,
    allowNull: false,
  },
});

Setting its value:

import { fn } from 'sequelize';

// if your string is already a ts_vector
await User.create({
  search: `a:1 fat:2 cat:3 sat:4 on:5 a:6 mat:7 and:8 ate:9 a:10 fat:11 rat:12`
});

// if your string is not yet a ts_vector
await User.create({
  search: fn('to_tsvector', 'english', 'The Fat Rats');
});

Comparing against a tsvector:

import { fn } from 'sequelize';

await User.findAll({
  where: {
    search: { [Op.match]: fn('to_tsquery', 'Cats') },
  },
});

That's about it for how it's used in Sequelize, you'll still need to understand how to use tsvector / tsquery. The documentation is here: https://www.postgresql.org/docs/current/datatype-textsearch.html

ephys avatar Jun 02 '22 18:06 ephys

@ephys Thank you very much, I'll give this a try :)

harrysayers avatar Jun 03 '22 08:06 harrysayers