全文搜索
Prisma Client 支持 2.30.0 及更高版本的 PostgreSQL 数据库和 3.8.0 及更高版本的 MySQL 数据库的全文搜索。启用全文搜索 (FTS) 后,您可以通过搜索数据库列中的文本来为您的应用程序添加搜索功能。
在 Prisma v6 中,FTS 已在 MySQL 上升级到通用可用性(GA)。它在 PostgreSQL 上仍处于预览阶段,需要使用 fullTextSearchPostgres
预览功能标志。
为 PostgreSQL 启用全文搜索
全文搜索 API 目前是预览功能。要启用此功能,请执行以下步骤
-
更新 schema 中的
previewFeatures
块以包含fullTextSearchPostgres
预览功能标志schema.prismagenerator client {
provider = "prisma-client-js"
previewFeatures = ["fullTextSearchPostgres"]
} -
生成 Prisma Client
npx prisma generate
重新生成 Client 后,模型上创建的任何 String
字段都将有一个新的 search
字段可用。例如,以下搜索将返回所有包含单词 'cat' 的帖子。
// All posts that contain the word 'cat'.
const result = await prisma.posts.findMany({
where: {
body: {
search: 'cat',
},
},
})
注意:PostgreSQL 的全文搜索功能目前存在一个已知问题。如果您发现搜索查询缓慢,可以使用原始 SQL 优化查询。
查询数据库
search
字段底层使用了数据库的原生查询能力。这意味着可用的具体查询操作符也取决于数据库。
PostgreSQL
以下示例演示了 PostgreSQL 'and' (&
) 和 'or' (|
) 操作符的用法
// All posts that contain the words 'cat' or 'dog'.
const result = await prisma.posts.findMany({
where: {
body: {
search: 'cat | dog',
},
},
})
// All drafts that contain the words 'cat' and 'dog'.
const result = await prisma.posts.findMany({
where: {
status: 'Draft',
body: {
search: 'cat & dog',
},
},
})
为了了解查询格式的工作原理,请考虑以下文本
"The quick brown fox jumps over the lazy dog"
以下查询与该文本的匹配情况如下所示
查询 | 匹配? | 说明 |
---|---|---|
fox & dog | 是 | 文本包含 'fox' 和 'dog' |
dog & fox | 是 | 文本包含 'dog' 和 'fox' |
dog & cat | 否 | 文本包含 'dog' 但不包含 'cat' |
!cat | 是 | 文本中没有 'cat' |
fox | cat | 是 | 文本包含 'fox' 或 'cat' |
cat | pig | 否 | 文本不包含 'cat' 或 'pig' |
fox <-> dog | 是 | 文本中 'dog' 在 'fox' 之后 |
dog <-> fox | 否 | 文本中 'fox' 不在 'dog' 之后 |
有关支持操作的完整范围,请参阅 PostgreSQL 全文搜索文档。
MySQL
以下示例演示了 MySQL 'and' (+
) 和 'not' (-
) 操作符的用法
// All posts that contain the words 'cat' or 'dog'.
const result = await prisma.posts.findMany({
where: {
body: {
search: 'cat dog',
},
},
})
// All posts that contain the words 'cat' and not 'dog'.
const result = await prisma.posts.findMany({
where: {
body: {
search: '+cat -dog',
},
},
})
// All drafts that contain the words 'cat' and 'dog'.
const result = await prisma.posts.findMany({
where: {
status: 'Draft',
body: {
search: '+cat +dog',
},
},
})
为了了解查询格式的工作原理,请考虑以下文本
"The quick brown fox jumps over the lazy dog"
以下查询与该文本的匹配情况如下所示
查询 | 匹配? | 描述 |
---|---|---|
+fox +dog | 是 | 文本包含 'fox' 和 'dog' |
+dog +fox | 是 | 文本包含 'dog' 和 'fox' |
+dog -cat | 是 | 文本包含 'dog' 但不包含 'cat' |
-cat | 否 | 减号操作符不能单独使用(参见下面的注意) |
fox dog | 是 | 文本包含 'fox' 或 'dog' |
quic* | 是 | 文本包含以 'quic' 开头的单词 |
quick fox @2 | 是 | 'fox' 出现在 'quick' 的 2 个单词距离内 |
fox dog @2 | 否 | 'dog' 不出现在 'fox' 的 2 个单词距离内 |
"jumps over" | 是 | 文本包含整个短语 'jumps over' |
注意:减号操作符只用于排除本来被其他搜索词匹配的行。因此,一个只包含以 - 开头的搜索词的布尔模式搜索将返回空结果。它不会返回“所有不包含任何排除词的行”。
MySQL 还有 >
、<
和 ~
操作符用于改变搜索结果的排名顺序。举例来说,考虑以下两条记录
1. "The quick brown fox jumps over the lazy dog"
2. "The quick brown fox jumps over the lazy cat"
查询 | 结果 | 描述 |
---|---|---|
fox ~cat | 先返回第 1 条,然后返回第 2 条。 | 返回所有包含 'fox' 的记录,但将包含 'cat' 的记录排名降低 |
fox (<cat >dog) | 先返回第 1 条,然后返回第 2 条。 | 返回所有包含 'fox' 的记录,但包含 'cat' 的记录排名低于包含 'dog' 的记录 |
有关支持操作的完整范围,请参阅 MySQL 全文搜索文档。
按 _relevance
排序结果
按相关性排序仅适用于 PostgreSQL 和 MySQL。
除了 Prisma Client 的默认 orderBy
行为之外,全文搜索还增加了按给定字符串的相关性进行排序的功能。例如,如果您想按帖子标题中与术语 'database'
的相关性来排序帖子,可以使用以下方法
const posts = await prisma.post.findMany({
orderBy: {
_relevance: {
fields: ['title'],
search: 'database',
sort: 'asc'
},
},
})
添加索引
PostgreSQL
Prisma Client 目前不支持使用索引来加速全文搜索。对此已存在一个 GitHub Issue。
MySQL
对于 MySQL,需要在 schema.prisma
文件中使用 @@fulltext
参数为您搜索的任何列添加索引。
在以下示例中,一个全文索引被添加到 Blog
模型的 content
字段,另一个则同时添加到 content
和 title
字段
generator client {
provider = "prisma-client-js"
}
model Blog {
id Int @unique
content String
title String
@@fulltext([content])
@@fulltext([content, title])
}
第一个索引允许搜索 content
字段中出现的单词 'cat'
const result = await prisma.blogs.findMany({
where: {
content: {
search: 'cat',
},
},
})
第二个索引允许同时搜索 content
和 title
字段,查找 content
中出现的单词 'cat' 以及 title
中出现的单词 'food'
const result = await prisma.blogs.findMany({
where: {
content: {
search: 'cat',
},
title: {
search: 'food',
},
},
})
然而,如果只尝试搜索 title
,搜索将失败并出现错误 "Cannot find a fulltext index to use for the search"(找不到用于搜索的全文索引),消息代码为 P2030
,因为该搜索需要同时在两个字段上都有索引。
使用原始 SQL 进行全文搜索
全文搜索目前处于预览阶段,并且由于存在一个已知问题,您可能会遇到搜索查询缓慢的情况。如果是这样,您可以使用 TypedSQL 优化查询。
PostgreSQL
使用 TypedSQL,您可以使用 PostgreSQL 的 to_tsvector
和 to_tsquery
来表达您的搜索查询。
- fullTextSearch.sql
- index.ts
SELECT * FROM "Blog" WHERE to_tsvector('english', "Blog"."content") @@ to_tsquery('english', ${term});
import { fullTextSearch } from "@prisma/client/sql"
const term = `cat`
const result = await prisma.$queryRawTyped(fullTextSearch(term))
注意:根据您的语言偏好,您可以在 SQL 语句中将
english
替换为其他语言。
如果您想在搜索词中包含通配符,可以按如下方式操作
- fullTextSearch.sql
- index.ts
SELECT * FROM "Blog" WHERE to_tsvector('english', "Blog"."content") @@ to_tsquery('english', ${term});
const term = `cat:*`
const result = await prisma.$queryRawTyped(fullTextSearch(term))
MySQL
在 MySQL 中,您可以按如下方式表达您的搜索查询
- fullTextSearch.sql
- index.ts
SELECT * FROM Blog WHERE MATCH(content) AGAINST(${term} IN NATURAL LANGUAGE MODE);
const term = `cat`
const result = await prisma.$queryRawTyped(fullTextSearch(term))