Occur error when paginate document result used search index in golang

hi, mongo community.
I have bug report.
When a paging request is made using ‘pagination token’ using ‘search index’, an aggregation query error occurs intermittently.
The development environment is golang 1.19, and mongodb.org/mongo-driver 1.14 is in use.

Below is an error message.
(UnknownError) class java.lang.Float cannot be cast to class org.bson.BsonValue (java.lang.Float is in module java.base of loader 'bootstrap'; org.bson.BsonValue is in unnamed module of loader 'app')

Below is an query example

pipeline := []bson.D{
  {{
      Key: "$search",
      Value: bson.M{
         "index": "{search-index-name}",
         "sort": bson.M{
            "unused": bson.M{"$meta": "searchScore"},
           "name": 1
         },
         "searchAfter": "{token}",
        "compound": bson.M{
            "filter": bson.A{
                some condition
            },
           "must": bson.A{
                some condition
           }
        },
      },
  }},
  {{Key:"$limit", Value:10}},
  {{Key:"$project", Value:bson.M{"token": bson.M{"$meta": "searchSequenceToken"}}}},
}

Hey @Damon_Kim, thanks for the question! I believe the problem is that bson.M is a Go map, which does not maintain the order of fields, but the the fields in the “sort” document are sensitive to ordering.

In your provided example, sometimes the “unused” field would come first in the marshaled BSON document, and sometimes the “name” field would come first. However, Atlas Search seems to expect “unused” to always come first.

Here is a modified pipeline that uses bson.D for “sort”:

pipeline := []bson.D{
	{{
		Key: "$search",
		Value: bson.M{
			"index": "{search-index-name}",
			"sort": bson.D{
				{Key: "unused", Value: bson.M{"$meta": "searchScore"}},
				{Key: "name", Value: 1},
			},
			"searchAfter": "{token}",
			"compound": bson.M{
				"filter": bson.A{
					// some condition
				},
				"must": bson.A{
					// some condition
				},
			},
		},
	}},
	{{Key: "$limit", Value: 10}},
	{{Key: "$project", Value: bson.M{"token": bson.M{"$meta": "searchSequenceToken"}}}},
}

Does that fix the intermittent errors?

1 Like