Support order by relevance
See original GitHub issueProblem
Right now if you use full text search, the results are unordered. This wouldn’t be a very good search engine if it doesn’t order the results.
Suggested solution
Given the following Prisma Schema
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
previewFeatures = ["fullTextSearch"]
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model Fruit {
id Int @id @default(autoincrement())
name String
}
Introduce a _relevance
field, where you can order search fields by descending or ascending order .
const results = await prisma.fruit.findMany({
orderBy: {
_relevance: {
fields: ["name"],
search: "Blue",
sort: "desc",
},
},
})
Note that we also support filtering search results:
const results = await prisma.fruit.findMany({
where: {
name: {
search: "Blue",
},
},
orderBy: {
_relevance: {
fields: ["name"],
search: "Blue",
sort: "desc",
},
},
})
These can be used together or separately.
where
filters the records (removes records with a relevance scores = 0),orderBy
orders the record by their relevance score
Additional context
Implementation PRs:
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
How Does Relevance Work When Sorting Search Results?
This article defines how the ProQuest platform sorts search results by relevance. This is what we mean by "relevance".
Read more >can we add sorting like "sort by | Relevance Views...
can we add sorting like "sort by | Relevance Views Newest Alpabetical " in our search page on portal similar to knowledge portal...
Read more >[Resolved] Sort by relevance - Toolset
I would like to sort results by relevance. I have custom code that is used to display relevance as a percentage, and I...
Read more >Mysql order by relevance and title - match - Stack Overflow
It means that if it matches both p. title and p. description it is more relevant than if it only matches p.
Read more >Relevant sorting - Algolia
If multiple results have the same value for a sorting attribute, tie-breaking proceeds as usual on the other defined sorting attributes. For ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@janpio thanks for the tip! Working now!
Ah right, my fault 😥 I had
fullTextSearch
in my experiment project, the tests for defaults (without flags) are passing. Sorry for the noise 😉