Skip to content
This repository was archived by the owner on Oct 18, 2021. It is now read-only.

Commit f8a52c4

Browse files
CircuitCoderkyeah
authored andcommitted
Allowed IndexModel keys to map to text, hashed, 2d and 2dsphere (#228)
1 parent 4a432f0 commit f8a52c4

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

src/coll/options.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,11 @@ impl IndexModel {
365365
name.push('_');
366366
match *bson {
367367
Bson::I32(ref i) => name.push_str(&format!("{}", i)),
368-
_ => return Err(ArgumentError(String::from("Index model keys must map to i32."))),
368+
Bson::String(ref s) if s == "text" || s == "hashed" || s == "2d" || s == "2dsphere"
369+
=> name.push_str(s),
370+
_ => return Err(ArgumentError(String::from(
371+
"Index model keys must map to i32, \"text\", \"hashed\", \"2d\" or \"2dsphere\""
372+
))),
369373
}
370374
}
371375
Ok(name)

tests/client/coll.rs

+81
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,87 @@ fn create_list_drop_indexes() {
688688
assert_eq!(1, results.len());
689689
}
690690

691+
#[test]
692+
fn create_text_hashed_2d_2dsphere_index() {
693+
let client = Client::connect("localhost", 27017).unwrap();
694+
let db = client.db("test-client-coll");
695+
let coll = db.collection("create_text_hashed_2d_2dsphere_index");
696+
697+
coll.create_index(doc!{
698+
"a" => "text"
699+
}, None).expect("Failed to create text indexes");
700+
701+
coll.create_index(doc!{
702+
"b" => "hashed"
703+
}, None).expect("Failed to create hashed indexes");
704+
705+
coll.create_index(doc!{
706+
"c" => "2d"
707+
}, None).expect("Failed to create 2d indexes");
708+
709+
coll.create_index(doc!{
710+
"d" => "2dsphere"
711+
}, None).expect("Failed to create 2dsphere indexes");
712+
}
713+
714+
#[test]
715+
fn block_ill_formed_index_models() {
716+
assert!(IndexModel::new(doc!{ "test" => 1.1 }, None).name().is_err());
717+
assert!(IndexModel::new(doc!{ "test" => 1i64 }, None).name().is_err());
718+
assert!(IndexModel::new(doc!{ "test" => "arbitrystr" }, None).name().is_err());
719+
}
720+
721+
#[test]
722+
fn create_query_text_index() {
723+
let client = Client::connect("localhost", 27017).unwrap();
724+
let db = client.db("test-client-coll");
725+
let coll = db.collection("create_query_text_index");
726+
727+
coll.drop().expect("Failed to drop collection");
728+
729+
let mut index_opt = IndexOptions::new();
730+
index_opt.weights = Some(doc!{
731+
"title" => 10,
732+
"content" => 5
733+
});
734+
coll.create_index(doc!{
735+
"title" => "text",
736+
"content" => "text"
737+
},
738+
Some(index_opt)).unwrap();
739+
740+
let doc1 = doc! { "title" => "keyword keyword keyword", "content" => "nonkeyword", "id" => 1 };
741+
let doc2 = doc! { "title" => "nonkeyword", "content" => "keyword keyword keyword", "id" => 2 };
742+
let doc3 = doc! { "title" => "nonkeyword", "content" => "nonkeyword", "id" => 3 };
743+
744+
coll.insert_many(vec![doc1.clone(), doc2.clone(), doc3.clone()], None).unwrap();
745+
746+
let count = coll.count(Some(doc!{ "$text" => { "$search" => "keyword" }}), None).unwrap();
747+
assert_eq!(count, 2);
748+
749+
let mut opts = FindOptions::new();
750+
opts.projection = Some(doc!{ "score" => { "$meta" => "textScore" }});
751+
opts.sort = Some(doc!{ "score" => { "$meta" => "textScore" }});
752+
753+
let mut cursor = coll.find(
754+
Some(doc!{ "$text" => { "$search" => "keyword" }}),
755+
Some(opts)).unwrap();
756+
let results = cursor.next_n(2).unwrap();
757+
758+
match results[0].get("id").unwrap() {
759+
&Bson::I32(id) => assert_eq!(1, id),
760+
_ => panic!("Why is id not a i32?")
761+
};
762+
763+
match results[1].get("id").unwrap() {
764+
&Bson::I32(id) => assert_eq!(2, id),
765+
_ => panic!("Why is id not a i32?")
766+
};
767+
768+
let count = coll.count(Some(doc!{ "$text" => { "$search" => "keyword nonkeyword" }}), None).unwrap();
769+
assert_eq!(count, 3);
770+
}
771+
691772
#[test]
692773
fn drop_all_indexes() {
693774
let client = Client::connect("localhost", 27017).unwrap();

0 commit comments

Comments
 (0)