liveBook · Manning

Retrieval ranking

As explained in Just Use Postgres!

Relevance ranking orders text-search results according to how closely their contents match a query.

Relevance ranking orders full-text search results by how closely their contents match a query, rather than relying only on a movie's vote average. Postgres compares matching lexemes, their frequency, and their positions to produce a more useful result order. section 6.4 section 6.0

Postgres provides two built-in ranking functions:

  • Use ts_rank to rank documents by query-term frequency and position. section 6.4
  • Use ts_rank_cd when term density matters, giving higher scores to documents whose query terms appear closer together. section 6.4
  • Use setweight to make matches in important fields, such as a movie name, outrank matches in descriptions. section 6.4

Ranking movies for “ghosts”

A basic query orders matching movies by vote_average, placing A Girl Walks Home Alone at Night first even though its lexemes contain “ghost” once. Ranking with ts_rank instead places Ghost first because “ghost” appears three times. Weighting the name as A and the description as B further emphasizes title matches; adding ghosts:A restricts matching to the higher-weight field.

How Just Use Postgres! covers it

How deep it goes
Explains ts_rank, ts_rank_cd, lexeme frequency, position, density, and field weighting through SQL examples, without deeper scoring theory.
Examples
Demonstrates movie searches for “ghosts,” lexeme frequency comparisons, ts_rank output, weighted fields, and ghosts:A filtering.
What it assumes
Assumes familiarity with SQL queries and full-text search structures while introducing ranking functions and weighted lexemes.
What you can do after
Apply it via Postgres by calculating ts_rank, ordering by search_rank, weighting fields, and filtering by weights.
How it connects
Connects relevance ranking with full-text search, lexemes, tsvector, tsquery, stemming, and field weighting.

Also explained in

Compare how 4 books explain retrieval ranking →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Just Use Postgres!. The section links point to the parts of the book it draws on.