I followed the guide in the page mongo Text Search but when I build text index on my collection (about 2 million lines) and try to search some and sort by score, it returns an error:
Error: error: {
"ok" : 0,
"errmsg" : "Executor error during find command: OperationFailed: Sort operation used more than the maximum 33554432 bytes of RAM. Add an index, or specify a smaller limit.",
"code" : 96,
"codeName" : "OperationFailed"
}
According to the docs, if mongoDB can't obtain the sort order via an index scan it will use an algorithm that will buffers the first k results of the query. If the memory footprint exceeds 32 megabytes, the query will fail which is what you are experiencing. Try to limit the number of scanned results by using the limit() method.
@Tristan is correct but a better solution than using limit would be to filter by the score first. By using limit you may not get the highest score in your record set.
As an example if your score was out of 100 and you were interested in high scores filter on that first before the performing the sort.
db.getCollection("myCollection").find(
{
"score" : {
"$gt" : 90
}
}
).sort(
{
"score" : 1.0
}
);