public void TestMetaTextAndOtherFields() { var sortBy = SortBy.MetaTextScore("searchrelevancescore").Descending("y").Ascending("z"); string expected = "{ \"searchrelevancescore\" : { \"$meta\" : \"textScore\" }, \"y\" : -1, \"z\" : 1 }"; Assert.Equal(expected, sortBy.ToJson()); }
public void TestMetaTextGenerate() { var sortBy = SortBy.MetaTextScore("score"); string expected = "{ \"score\" : { \"$meta\" : \"textScore\" } }"; Assert.Equal(expected, sortBy.ToJson()); }
public void TestMetaTextAndOtherFields() { var sortBy = SortBy <Test> .MetaTextScore(y => y.R).Descending(y => y.A).Ascending(y => y.z); string expected = "{ \"relevance\" : { \"$meta\" : \"textScore\" }, \"a\" : -1, \"z\" : 1 }"; Assert.AreEqual(expected, sortBy.ToJson()); }
public void TestMetaTextGenerate() { var sortBy = SortBy <Test> .MetaTextScore(y => y.R); string expected = "{ \"relevance\" : { \"$meta\" : \"textScore\" } }"; Assert.AreEqual(expected, sortBy.ToJson()); }
/* public IEnumerable<T> FullTextSearch<T>(string search) * { * var _cmd = new CommandDocument * { * { "text", GetCollectionName<T>() }, * { "search", search } * }; * * * /*return Database.RunCommand(_cmd);#1# * var _doc = Database.RunCommand(_cmd).Response.GetElement("results").Value.ToString(); * * return BsonSerializer.Deserialize<IEnumerable<T>>(_doc); * * }*/ public IEnumerable <T> TextSearch <T>(IMongoQuery query, int pageIndex, int pageSize, out long totalRows) { var _sort = SortBy.MetaTextScore("score"); var _f = Fields.MetaTextScore("score"); var _data = GetCollection <T>(); totalRows = _data.Count(query); return(_data.Find(query).SetFields(_f).SetSortOrder(_sort).SetSkip((pageIndex - 1) * pageSize).SetLimit(pageSize)); }
public void TestMetaText() { var server = Configuration.TestServer; var primary = server.Primary; if (primary.Supports(FeatureId.TextSearchQuery)) { using (server.RequestStart(null, primary)) { var collection = Configuration.TestDatabase.GetCollection <Test>("test_meta_text_sort"); collection.Drop(); collection.CreateIndex(IndexKeys <Test> .Text(x => x.T)); collection.Insert(new Test { Id = 1, T = "The quick brown fox jumped", z = 1 }); collection.Insert(new Test { Id = 2, T = "over the lazy brown dog and brown cat", z = 2 }); collection.Insert(new Test { Id = 3, T = "over the lazy brown dog and brown cat", z = 4 }); collection.Insert(new Test { Id = 4, T = "over the lazy brown dog and brown cat", z = 3 }); var query = Query.Text("brown"); var fields = Fields <Test> .MetaTextScore(y => y.R); var sortBy = SortBy <Test> .MetaTextScore(y => y.R).Descending(y => y.z); var cursor = collection.FindAs <BsonDocument>(query).SetFields(fields).SetSortOrder(sortBy); var result = cursor.ToArray(); Assert.AreEqual(4, result.Length); Assert.AreEqual(3, result[0]["_id"].AsInt32); Assert.AreEqual(4, result[1]["_id"].AsInt32); Assert.AreEqual(2, result[2]["_id"].AsInt32); Assert.AreEqual(1, result[3]["_id"].AsInt32); } } }
public void TestMetaText() { var server = Configuration.TestServer; var primary = server.Primary; if (primary.Supports(FeatureId.TextSearchQuery)) { using (server.RequestStart(null, primary)) { var collection = Configuration.TestDatabase.GetCollection <BsonDocument>("test_meta_text_sort"); collection.Drop(); collection.CreateIndex(IndexKeys.Text("textfield")); collection.Insert(new BsonDocument { { "_id", 1 }, { "textfield", "The quick brown fox jumped" }, { "z", 1 } }); collection.Insert(new BsonDocument { { "_id", 2 }, { "textfield", "over the lazy brown dog and brown cat" }, { "z", 2 } }); collection.Insert(new BsonDocument { { "_id", 3 }, { "textfield", "over the lazy brown dog and brown cat" }, { "z", 4 } }); collection.Insert(new BsonDocument { { "_id", 4 }, { "textfield", "over the lazy brown dog and brown cat" }, { "z", 3 } }); var query = Query.Text("brown"); var fields = Fields.MetaTextScore("relevance"); var sortBy = SortBy.MetaTextScore("relevance").Descending("z"); var cursor = collection.FindAs <BsonDocument>(query).SetFields(fields).SetSortOrder(sortBy); var result = cursor.ToArray(); Assert.AreEqual(4, result.Length); Assert.AreEqual(3, result[0]["_id"].AsInt32); Assert.AreEqual(4, result[1]["_id"].AsInt32); Assert.AreEqual(2, result[2]["_id"].AsInt32); Assert.AreEqual(1, result[3]["_id"].AsInt32); } } }