如何使用 Sequelize 实现 PostgresQL tsvector 进行全文搜索?

atk*_*yla 2 postgresql sequelize.js

我正在阅读本教程: https://www.digitalocean.com/community/tutorials/how-to-use-full-text-search-in-postgresql-on-ubuntu-16-04

建议添加一document列 astsvector并对其应用索引。

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');
Run Code Online (Sandbox Code Playgroud)

如何使用documentsequelize 实现专栏?没有tsvector数据类型:http://docs.sequelizejs.com/variable/index.html

(现在是尝试 Knex.js 的好时机吗?)

mtk*_*one 5

Sequelize 版本 6.5.0+ 支持该TSVECTOR数据类型。但到目前为止,我在任何地方都找不到任何文档,因此:

声明一下:

sequelize.define('User', {
  myVector: { type: DataTypes.TSVECTOR },
  ...
})
Run Code Online (Sandbox Code Playgroud)

填充它:

User.myVector = sequelize.fn('to_tsvector', 'My Content About Travel and Apparently Some Cures')
Run Code Online (Sandbox Code Playgroud)

在查询中使用它:

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

探索拉取请求以获取更多详细信息:https ://github.com/sequelize/sequelize/pull/12955