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?