prisma / prisma

Next-generation ORM for Node.js & TypeScript | PostgreSQL, MySQL, MariaDB, SQL Server, SQLite, MongoDB and CockroachDB
https://www.prisma.io
Apache License 2.0
38.72k stars 1.52k forks source link

Full-text search with relevance #20657

Open Mathie01 opened 1 year ago

Mathie01 commented 1 year ago

Bug description

When I do a full-text search using _relevance, the filter is not taken into account.

Code :

prisma.drugRepository.findMany({
            take: 10,
            orderBy: {
                _relevance: {
                    fields: 'libelleCourt',
                    search: query.split(' ').join(" & "),
                    sort: 'asc'
                }
            },
        });

Returns the first 10 results from the table (not filtered)

Query Logs :

prisma:query SELECT "public"."repository_drug"."id", "public"."repository_drug"."libelleCourt", "public"."repository_drug"."atc5Code", "public"."repository_drug"."atc5Libelle" FROM "public"."repository_drug" WHERE 1=1 ORDER BY ts_rank(to_tsvector(concat_ws(' ', "public"."repository_drug"."libelleCourt")), to_tsquery($1)) ASC LIMIT $2 OFFSET $3

How to reproduce

1) Generate a data table 2) Filter the data using _relevance

Expected behavior

10 lines corresponding to the filter I have indicated

Prisma information

generator client {
  provider        = "prisma-client-js"
  binaryTargets   = ["native", "linux-musl"]
  previewFeatures = ["fullTextSearch"]
}

model DrugRepository {
  id           String  @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  libelleCourt String
  atc5Code     String?
  atc5Libelle  String?

  @@map("repository_drug")
}
prisma.drugRepository.findMany({
            take: 10,
            orderBy: {
                _relevance: {
                    fields: 'libelleCourt',
                    search: query.split(' ').join(" & "),
                    sort: 'asc'
                }
            },
        });

Environment & setup

Prisma Version

prisma                  : 5.1.1
@prisma/client          : 5.1.1
Current platform        : debian-openssl-1.1.x
Query Engine (Node-API) : libquery-engine 6a3747c37ff169c90047725a05a6ef02e32ac97e (at node_modules/@prisma/engines/libquery_engine-debian-openssl-1.1.x.so.node)
Schema Engine           : schema-engine-cli 6a3747c37ff169c90047725a05a6ef02e32ac97e (at node_modules/@prisma/engines/schema-engine-debian-openssl-1.1.x)
Schema Wasm             : @prisma/prisma-schema-wasm 5.1.1-1.6a3747c37ff169c90047725a05a6ef02e32ac97e
Default Engines Hash    : 6a3747c37ff169c90047725a05a6ef02e32ac97e
Studio                  : 0.492.0
Preview Features        : fullTextSearch
reubenjh commented 2 months ago

Looks suspiciously like you're missing the full text index. Docs here - https://www.prisma.io/docs/orm/prisma-client/queries/full-text-search

Weakky commented 1 month ago

Hey @Mathie01,

I know we're late to the party here, but would you happen to know/remember what was the specific full text search query you were running?

Thanks