Atlas Search - scoring

You can try using search score details to explain the results you’re seeing but I believe one factor that may be dl (length of the field in the document) - I did some brief testing off a smaller data set from the JSON file you provided which appeared to also show that dl was a factor that caused the scoring to be how it was amongst the last two documents shown in your screenshot. In short, it does appear like the behaviour you’ve mentioned is expected based off your index definition, search query and test documents.

As per the Score documentation:

Every document returned by an Atlas Search query is assigned a score based on relevance, and the documents included in a result set are returned in order from highest score to lowest.

Many factors can influence a document’s score, including:

  • The position of the search term in the document,
  • The frequency of occurrence of the search term in the document,
  • The type of operator the query uses,
  • The type of analyzer the query uses.

In this particular case here,the document only containing dodge was scored higher due to the significantly longer length of the other document(s) containing more terms. Please also consider that those documents contained more words that did not match the search criteria.

You can try again with the following index definition:

{
  "analyzer": "lucene.english",
  "searchAnalyzer": "lucene.english",
  "mappings": {
    "dynamic": false,
    "fields": {
      "name": {
        "indexOptions": "docs",
        "norms": "omit",
        "type": "string"
      }
    }
  },
  "storedSource": {
    "include": [
      "name"
    ]
  }
}

The main changes performed above were setting:

  • norms value to "omit"
  • indexOptions value to "docs"

The following is the output I get (you can see the document only containing "dodge" is at the end):

dodge>db.collection.aggregate([
 {
 	$search: {
 		index: 'nameindex',
 		text: {
 			query: 'dodge ram 2500',
 			path: 'name'
 		}
 	}
 },
 {$project:{_id:0,name:1,score:{$meta:'searchScore'}}}
 ])
[
  { name: 'dodge ram 2500', score: 3.8390729427337646 },
  {
    name: 'Floor Mats for Dodge Ram 2019 2020 1500 All new Crew Cab (not Classic) weather guard Front & Rear Row TPE Slush Liner Mats',
    score: 1.9325730800628662
  },
  { name: 'dodge ram', score: 1.9325730800628662 },
  { name: 'ram dodge 2016', score: 1.9325730800628662 },
  { name: 'floor mats for dodge ram', score: 1.9325730800628662 },
  { name: 'Floor Mats for Dodge Ram 2019', score: 1.9325730800628662 },
  {
    name: 'Floor Mats for Dodge Ram 2019 2020 1500 All new Crew Cab (not classic)...',
    score: 1.9325730800628662
  },
  {
    name: 'Floor Mats for Dodge Ram 2019 2020 1500 All new Crew Cab (not classic) random text random stuff randomly random testing test',
    score: 1.9325730800628662
  },
  {
    name: 'Floor Mats for Dodge Ram 2019 2020 1500 All new Crew Cab (not classic)...',
    score: 1.9325730800628662
  },
  {
    name: 'Floor Mats for Dodge Ram 2019 2020 1500 All new Crew Cab (not classic)...',
    score: 1.9325730800628662
  },
  {
    name: 'Floor Mats for Dodge Ram 2019 2020 1500 All new Crew Cab (not classic)...',
    score: 1.9325730800628662
  },
  {
    name: 'Floor Mats for Dodge Ram 2019 2020 1500 All new Crew Cab (not classic)...',
    score: 1.9325730800628662
  },
  {
    name: 'Floor Mats for 2014-2018 Chevrolet Silverado/GMC Sierra 1500 Crew Cab, 2015-2019 Silverado/Sierra 2500/3500 HD Crew Cab All Weather Guard 1st and 2nd Row Mat TPE Slush Liners',
    score: 1.906499981880188
  },
  { name: '2500', score: 1.906499981880188 },
  { name: 'dodge', score: 0.9386987090110779 }
]

Regards,
Jason

2 Likes