sequelize / website

Our beloved website that contains all versions of our documentations and the API references.
https://sequelize.org
29 stars 155 forks source link

Full-Text Search using TSVector #91

Open harrysayers opened 2 years ago

harrysayers commented 2 years ago

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 commented 2 years ago

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

harrysayers commented 2 years ago

Any update on this? :)

ephys commented 2 years ago

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

harrysayers commented 2 years ago

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