Search code examples
mongodb-atlas-search

Why does the Mongodb Atlas Search $search operator not return score=1 on exact query match?


My objective is to implement an effect user-name search aggregation with Mongodb Atlas full text search. It's not going super well. Currently I struggle to understand, why the following does not result in a score of 1?

data

[{name:  "John Doe"}, {"John Eriksen", {"Lara Croft"} ]

Search Index

{ "mappings": { "dynamic": true } }

Pipeline

[
  {
    '$search': {
      'text': {
        'query': 'John Doe', 
        'path': 'name'
      }
    }
  }, {
    '$addFields': {
      'score': {
        '$meta': 'searchScore'
      }
    }
  }
]

Result

John Doe → 0.66
John Eriksen" → 0.21

Why does "John Doe" not return a score of 1, it's an exact score after all?


Solution

  • Atlas Search is a Lucene-based system, which uses complicated scoring model (BM25 by default), taking into account things like term frequency in the document, it's inverse frequency in the whole dataset and documents' length. In this particular example, text query also contains 2 terms separated by space, so Lucene will break the overall query down into 2 subqueries and score them independently before sum to a total score. Also note that Lucene scores are relative and valid in the context of a specific query, so their only goal is to order the hits - without any correctness guarantees as an absolute number