Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support fulltext search using indexes in PostgreSQL #10386

Open
Tracked by #439
pimeys opened this issue Nov 22, 2021 · 11 comments
Open
Tracked by #439

Support fulltext search using indexes in PostgreSQL #10386

pimeys opened this issue Nov 22, 2021 · 11 comments
Labels
domain/psl Issue in the "PSL" domain: Prisma Schema Language domain/schema Issue in the "Schema" domain: Prisma Schema, Introspection, Migrations etc. kind/feature A request for a new feature. topic: database-functionality topic: fullTextSearch topic: indexes topic: postgresql

Comments

@pimeys
Copy link
Contributor

pimeys commented Nov 22, 2021

This requires some ts_vector magic.

@janpio janpio added kind/feature A request for a new feature. domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. domain/schema Issue in the "Schema" domain: Prisma Schema, Introspection, Migrations etc. topic: fullTextSearch topic: postgresql and removed domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. labels Nov 22, 2021
@janpio
Copy link
Contributor

janpio commented Nov 30, 2021

An example for index usage in PostgreSQL is in #8950 (comment)

@janpio
Copy link
Contributor

janpio commented Dec 13, 2021

@matthewmueller
Copy link
Contributor

matthewmueller commented Dec 13, 2021

Can someone share why these extensions are necessary? I'm surprised that adding the index:

CREATE INDEX book_content_index ON "Book" USING GIN (to_tsvector('english', "content"));

Doesn't just work. In my testing:

explain SELECT "public"."Book"."id", "public"."Book"."title", "public"."Book"."content" FROM "public"."Book" WHERE to_tsvector("public"."Book"."content") @@ to_tsquery('great') OFFSET 0

It looks like it still does a sequential scan (1-by-1):

Seq Scan on "Book"  (cost=0.00..29.65 rows=1 width=56)
  Filter: (to_tsvector(content) @@ to_tsquery('great'::text))

@AzSiAz
Copy link

AzSiAz commented Dec 14, 2021

I think it's because you are using to_tsvector('english', "content"), the index is defined with english type, but you are not using it in your query

The query should be

EXPLAIN
SELECT
	"public"."Book"."id",
	"public"."Book"."title",
	"public"."Book"."content"
FROM
	"public"."Book"
WHERE
	to_tsvector('english', "public"."Book"."content") @@ to_tsquery('great')
	OFFSET 0

That's also why (here) I added a point about language being dynamic in @prisma/client

@matthewmueller
Copy link
Contributor

matthewmueller commented Dec 14, 2021

It worked! Beautiful @AzSiAz!

Took my query (above) from 9s to 280ms.

Bitmap Heap Scan on "Book"  (cost=16.25..20.76 rows=1 width=56)
  Recheck Cond: (to_tsvector('english'::regconfig, content) @@ to_tsquery('great'::text))
  ->  Bitmap Index Scan on book_content_index  (cost=0.00..16.25 rows=1 width=0)
      Index Cond: (to_tsvector('english'::regconfig, content) @@ to_tsquery('great'::text))

We'll take a look at your original issue, thanks again!

@janpio janpio added this to the 3.9.0 milestone Jan 15, 2022
@janpio janpio added the domain/psl Issue in the "PSL" domain: Prisma Schema Language label Feb 6, 2022
@floelhoeffel floelhoeffel removed this from the 3.9.0 milestone Feb 18, 2022
@floelhoeffel
Copy link

floelhoeffel commented Aug 3, 2022

Hello everyone 👋

We are starting design work to improve Prisma's support for Full Text Search (FTS)!

If you would like to help, please tell us about your needs via a short FTS user research survey.

Thank you!

@younes-alouani
Copy link

younes-alouani commented Aug 12, 2022

@floelhoeffel , I think Django ORM handles FTS very well. And Django is used by big companies, it can be good to get inspiration from its ORM code source.

@deep-randhawa
Copy link

Any updates here? Or is there a timeline to when this FTS feature will be completed?

@janpio janpio added the status/is-preview-feature This feature request is currently available as a Preview feature. label Mar 27, 2023
@gruckion
Copy link

Time line guys? Come on it’s been 2 years. Where can we find the timeline for indexes on fts

@janpio janpio removed the status/is-preview-feature This feature request is currently available as a Preview feature. label Mar 27, 2024
@janpio
Copy link
Contributor

janpio commented Mar 27, 2024

Generally GIN indexes (and similar) can already be configured via @@index: https://www.prisma.io/docs/orm/prisma-schema/data-model/indexes#configuring-the-access-type-of-indexes-with-type-postgresql But the usage in the full text search API (behind fullTextSearch preview feature) is currently problematic: #8950

@janpio janpio changed the title Support fulltext indexes in PostgreSQL Support fulltext search using indexes in PostgreSQL Mar 27, 2024
@winterprime
Copy link

Flipping my whole database to MySQL because of this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain/psl Issue in the "PSL" domain: Prisma Schema Language domain/schema Issue in the "Schema" domain: Prisma Schema, Introspection, Migrations etc. kind/feature A request for a new feature. topic: database-functionality topic: fullTextSearch topic: indexes topic: postgresql
Projects
None yet
Development

No branches or pull requests

9 participants