question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support order by relevance

See original GitHub issue

Problem

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:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
abeaclarkcommented, Nov 19, 2021

@janpio thanks for the tip! Working now!

1reaction
MichalLytekcommented, Nov 8, 2021

Ah right, my fault 😥 I had fullTextSearch in my experiment project, the tests for defaults (without flags) are passing. Sorry for the noise 😉

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found