2

I was going through this tutorial: https://www.digitalocean.com/community/tutorials/how-to-use-full-text-search-in-postgresql-on-ubuntu-16-04

Where it is recommended to add a document column as tsvector and applying an index to it.

ALTER TABLE news ADD "document" tsvector;
CREATE INDEX idx_fts_search ON news USING gin(document);
SELECT title, content FROM news WHERE document @@ to_tsquery('Travel | Cure');

How do I implement the document column with sequelize? There is no tsvector data type: http://docs.sequelizejs.com/variable/index.html

(Would this be a good time to try out Knex.js?)

1
  • You should be able to specify the type as a string. You don't have to use the builtin types. You'll just have to do a bit of work to get the values from the database. For reference, I have used CITEXT as a type in Postgres and never had an issue. Commented Aug 23, 2017 at 17:47

2 Answers 2

6

Sequelize version 6.5.0+ has support for the TSVECTOR datatype. But there is as of yet no documentation anywhere that I can find, so:

Declare it:

sequelize.define('User', {
  myVector: { type: DataTypes.TSVECTOR },
  ...
})

Populate it:

User.myVector = sequelize.fn('to_tsvector', 'My Content About Travel and Apparently Some Cures')

Use it in a query:

User.findAll({
  where: { 
    myVector: { [Op.match]: sequelize.fn('to_tsquery', 'Travel | Cure') }
  }
})

Explore the pull request for more details: https://github.com/sequelize/sequelize/pull/12955

Sign up to request clarification or add additional context in comments.

Comments

2

Try UPDATE news before CREATE INDEX:

ALTER TABLE news ADD COLUMN document tsvector;

UPDATE news SET document = to_tsvector(coalesce(title,'') || ' ' || coalesce(content,''));

CREATE INDEX idx_fts_search ON news USING gin(document);
SELECT title, content FROM news WHERE document @@ to_tsquery('Travel | Cure');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.