MongoDB text search with sorting in C# -
i'm trying implement text search in mongodb, in c#.
the documentation doesn't cover how sort text search results in c#.
i have list of tags, separated spaces, match.
if provide string "tag1 tag2", results provided in following order:
- anything has both 'tag1' , 'tag2', followed by
- anything has 'tag1', followed by
- anything has 'tag2'
i have been trying piece code:
var f = builders<mongopost>.filter.text(tags); var s = builders<mongopost>.sort.metatextscore("textscore"); return mongo.driver.find(f).sort(s).tolistasync().result;
but following error:
{"queryfailure flag true (response { \"$err\" : \"can't canonicalize query: badvalue must have $meta projection $meta sort keys\", \"code\" : 17287 })."}
no proper documentation error...
then found following code on site:
var pipeline = new list<bsondocument> { bsonserializer.deserialize<bsondocument>("{ $match: { $text: { $search: \"" + tags + "\" } } }"), bsonserializer.deserialize<bsondocument>("{ $sort: { score: { $meta: \"textscore\" } } }") }; var r = mongo.driver.aggregateasync(new bsondocumentstagepipelinedefinition<mongopost, mongopost>(pipeline)).result; return r.tolistasync().result;
at least runs without error, trying make code in style @ top, can rest of api, , not having go console style text strings have parsed every execution. also, need add lot more criteria search, syntax not practical me.
is there proper documentation missed? if not, know how implement in style @ top?
using post: retrieve relevance ordered result text query on mongodb collection using c# driver
i got work following code:
var f = builders<mongopost>.filter.text(tags); var p = builders<mongopost>.projection.metatextscore("textmatchscore"); var s = builders<mongopost>.sort.metatextscore("textmatchscore"); return mongo.driver.find(f).project<mongopost>(p).sort(s).tolistasync().result;
and class mongopost has following field:
[bsonignoreifnull] public double? textmatchscore { get; set; }
Comments
Post a Comment