Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-13272

Text search should automatically sort by textScore; currently results are scrambled

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.6.0-rc1
    • Component/s: None
    • None
    • ALL
    • Hide
      > db.monty.find()
      { "_id" : 1, "a" : "What… is the air-speed velocity of an unladen swallow?" }
      { "_id" : 2, "a" : "What do you mean? An African or a European swallow?" }
      { "_id" : 3, "a" : "Huh? I… I don’t know that." }
      { "_id" : 45, "a" : "You’re using coconuts!" }
      { "_id" : 55, "a" : "What? A swallow carrying a coconut?" }
      > db.monty.ensureIndex({a: "text"})
      WriteResult({ "nInserted" : 1 })
      > db.monty.find({$text: {$search: "swallow coconut"}})
      { "_id" : 2, "a" : "What do you mean? An African or a European swallow?" }
      { "_id" : 55, "a" : "What? A swallow carrying a coconut?" }
      { "_id" : 45, "a" : "You’re using coconuts!" }
      { "_id" : 1, "a" : "What… is the air-speed velocity of an unladen swallow?" }
      > db.monty.find({$text: {$search: "swallow coconut"}}, {textScore: {$meta: "textScore"}}).sort({textScore: {$meta: "textScore"}})
      { "_id" : 55, "a" : "What? A swallow carrying a coconut?", "textScore" : 1.3333333333333333 }
      { "_id" : 45, "a" : "You’re using coconuts!", "textScore" : 0.6666666666666666 }
      { "_id" : 2, "a" : "What do you mean? An African or a European swallow?", "textScore" : 0.625 }
      { "_id" : 1, "a" : "What… is the air-speed velocity of an unladen swallow?", "textScore" : 0.5833333333333334 }
      >
      
      Show
      > db.monty.find() { "_id" : 1, "a" : "What… is the air-speed velocity of an unladen swallow?" } { "_id" : 2, "a" : "What do you mean? An African or a European swallow?" } { "_id" : 3, "a" : "Huh? I… I don’t know that." } { "_id" : 45, "a" : "You’re using coconuts!" } { "_id" : 55, "a" : "What? A swallow carrying a coconut?" } > db.monty.ensureIndex({a: "text" }) WriteResult({ "nInserted" : 1 }) > db.monty.find({$text: {$search: "swallow coconut" }}) { "_id" : 2, "a" : "What do you mean? An African or a European swallow?" } { "_id" : 55, "a" : "What? A swallow carrying a coconut?" } { "_id" : 45, "a" : "You’re using coconuts!" } { "_id" : 1, "a" : "What… is the air-speed velocity of an unladen swallow?" } > db.monty.find({$text: {$search: "swallow coconut" }}, {textScore: {$meta: "textScore" }}).sort({textScore: {$meta: "textScore" }}) { "_id" : 55, "a" : "What? A swallow carrying a coconut?" , "textScore" : 1.3333333333333333 } { "_id" : 45, "a" : "You’re using coconuts!" , "textScore" : 0.6666666666666666 } { "_id" : 2, "a" : "What do you mean? An African or a European swallow?" , "textScore" : 0.625 } { "_id" : 1, "a" : "What… is the air-speed velocity of an unladen swallow?" , "textScore" : 0.5833333333333334 } >

      Currently, a text search does not sort by textScore, or by $natural, or by anything that I can discern.

      This is a problem because people who put in multiple words will nearly always want to have the best match come first.

      I suggest we sort by textScore by default.

      Note from the example in steps to reproduce that a .find( { $text : {$search: "string"} } } ) returns documents that are neither in natural order, nor in score order.

            Assignee:
            Unassigned Unassigned
            Reporter:
            william.cross William Cross (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: