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

$text index defined on positional array element does not return all results

    • Query Integration
    • ALL
    • Hide

      Consider the following two documents:

      > db.c.find({'arr.0.str':'abc'}).pretty()
      {
      	"_id" : 1,
      	"arr" : [
      		{
      			"num" : 123,
      			"str" : "abc"
      		},
      		{
      			"num" : 789,
      			"str" : "xyz"
      		}
      	]
      }
      {
      	"_id" : 3,
      	"arr" : {
      		"0" : {
      			"num" : 123,
      			"str" : "abc"
      		},
      		"1" : {
      			"num" : 789,
      			"str" : "xyz"
      		}
      	}
      } 

      In the first one, arr is an array with two entries.  In the second, arr is just an object containing subdocuments.  As the query shows, the ambiguity of "0" results in both documents matching.

      Creating a text index on 'arr.0.str' yields only the embedded document as a matching result:

      > db.c.createIndex({'arr.0.str':'text'})
      {
      	"createdCollectionAutomatically" : false,
      	"numIndexesBefore" : 2,
      	"numIndexesAfter" : 3,
      	"ok" : 1
      }
      > db.c.find({$text:{$search:'abc'}}).pretty()
      {
      	"_id" : 3,
      	"arr" : {
      		"0" : {
      			"num" : 123,
      			"str" : "abc"
      		},
      		"1" : {
      			"num" : 789,
      			"str" : "xyz"
      		}
      	}
      } 
      >

      Removing the positional aspect of the field in the index definition results in the array document being returned as expected:

      > db.c.dropIndex("arr.0.str_text")
      { "nIndexesWas" : 3, "ok" : 1 }
      > db.c.createIndex({'arr.str':'text'})
      {
      	"createdCollectionAutomatically" : false,
      	"numIndexesBefore" : 2,
      	"numIndexesAfter" : 3,
      	"ok" : 1
      }
      > db.c.find({$text:{$search:'abc'}}).pretty()
      ...
      {
      	"_id" : 1,
      	"arr" : [
      		{
      			"num" : 123,
      			"str" : "abc"
      		},
      		{
      			"num" : 789,
      			"str" : "xyz"
      		}
      	]
      }
      >
      Show
      Consider the following two documents: > db.c.find({'arr.0.str':'abc'}).pretty() { "_id" : 1, "arr" : [ { "num" : 123, "str" : "abc" }, { "num" : 789, "str" : "xyz" } ] } { "_id" : 3, "arr" : { "0" : { "num" : 123, "str" : "abc" }, "1" : { "num" : 789, "str" : "xyz" } } } In the first one, arr is an array with two entries.  In the second, arr is just an object containing subdocuments.  As the query shows, the ambiguity of  "0" results in both documents matching. Creating a text index on 'arr.0.str' yields only the embedded document as a matching result: > db.c.createIndex({'arr.0.str':'text'}) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 2, "numIndexesAfter" : 3, "ok" : 1 } > db.c.find({$text:{$search:'abc'}}).pretty() { "_id" : 3, "arr" : { "0" : { "num" : 123, "str" : "abc" }, "1" : { "num" : 789, "str" : "xyz" } } } > Removing the positional aspect of the field in the index definition results in the array document being returned as expected: > db.c.dropIndex("arr.0.str_text") { "nIndexesWas" : 3, "ok" : 1 } > db.c.createIndex({'arr.str':'text'}) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 2, "numIndexesAfter" : 3, "ok" : 1 } > db.c.find({$text:{$search:'abc'}}).pretty() ... { "_id" : 1, "arr" : [ { "num" : 123, "str" : "abc" }, { "num" : 789, "str" : "xyz" } ] } >

      text index defined on on 'arr.0.str' does not return the following document for the query find({$text: {$search:'abc'} }):

      {
      	"_id" : 1,
      	"arr" : [
      		{
      			"num" : 123,
      			"str" : "abc"
      		},
      		{
      			"num" : 789,
      			"str" : "xyz"
      		}
      	]
      } 

            Assignee:
            backlog-query-integration [DO NOT USE] Backlog - Query Integration
            Reporter:
            christopher.harris@mongodb.com Chris Harris
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated: