如何使用 Sequelize 实现 PostgresQL tsvector 进行全文搜索? [英] How to implement PostgresQL tsvector for full-text search using Sequelize?

查看:108
本文介绍了如何使用 Sequelize 实现 PostgresQL tsvector 进行全文搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

建议添加一个 document 列作为 tsvector 并对其应用索引.

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');

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

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

(这是试用 Knex.js 的好时机吗?)

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

推荐答案

Sequelize 6.5.0+ 版支持 TSVECTOR 数据类型.但是到目前为止我还没有任何文档可以找到,所以:

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:

声明:

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

填充它:

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

在查询中使用它:

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

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

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

这篇关于如何使用 Sequelize 实现 PostgresQL tsvector 进行全文搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆