BuildBot

Content and Search

Relevance & scoring

Lesson 5 of 5

What you'll learn

  • Understand why search results are ranked, not just filtered
  • Grasp term frequency as the core scoring intuition
  • Score and sort documents for a query

A query usually matches many documents. Elasticsearch doesn't return them in random or insertion order — it ranks them by a relevance score (_score) and returns the most relevant first. Each hit carries its score so you can see why it placed where it did.

{
  "hits": [
    { "_id": 3, "_score": 2.1, "title": "search search basics" },
    { "_id": 1, "_score": 1.2, "title": "fast search engine" }
  ]
}

Term frequency is the core idea

The default scoring model (BM25) builds on a simple intuition: term frequency — a document that mentions a query term more often is probably more about it, so it scores higher. The real formula also rewards rare terms and dampens very long documents, but term frequency is the part you feel most directly: more matches, higher rank.

Filter decides if, score decides where

A filter clause only answers "does this doc qualify?" Scoring answers "how high should it rank among the ones that qualify?" Both run on every search — that's why you can both narrow and order results in one query.

The challenge implements a tiny term-frequency scorer: count how often each query term appears in a document, then sort documents by that score.

Score and rank by term frequency

Run it. It scores each document by counting query-term occurrences, then prints the documents sorted from most to least relevant.

Loading editor…

You now have the full path: model content in Strapi, serve it over its API, and make it searchable and well-ranked with Elasticsearch.

Sign in to save your progress across devices.