Search tests now pass

This commit is contained in:
Michael Klishin 2014-05-10 17:04:55 -04:00
parent 8b237db382
commit a7cceebe9c
2 changed files with 29 additions and 24 deletions

View file

@ -11,7 +11,7 @@
"Full text search queries support (MongoDB 2.4+)" "Full text search queries support (MongoDB 2.4+)"
(:require [monger.command :as cmd] (:require [monger.command :as cmd]
[monger.conversion :as cnv]) [monger.conversion :as cnv])
(:import [com.mongodb CommandResult BasicDBList DBObject])) (:import [com.mongodb DB CommandResult BasicDBList DBObject]))
;; ;;
;; Implementation ;; Implementation
@ -28,8 +28,8 @@
(defn search (defn search
"Performs a full text search query" "Performs a full text search query"
[^String collection query] [^DB db ^String collection query]
(cmd/search collection query)) (cmd/search db collection query))
(defn results-from (defn results-from
"Returns a lazy sequence of results from a search query response, sorted by score. "Returns a lazy sequence of results from a search query response, sorted by score.

View file

@ -3,33 +3,38 @@
[monger.collection :as mc] [monger.collection :as mc]
[monger.search :as ms] [monger.search :as ms]
[monger.command :as cmd] [monger.command :as cmd]
[monger.test.helper :as helper]
[clojure.test :refer [deftest is use-fixtures]] [clojure.test :refer [deftest is use-fixtures]]
[monger.test.fixtures :refer :all] [monger.result :refer [ok?]])
[monger.result :refer [ok?]])
(:import com.mongodb.BasicDBObjectBuilder)) (:import com.mongodb.BasicDBObjectBuilder))
(helper/connect!) (let [conn (mg/connect)
db (mg/get-db conn "monger-test")
coll "search-docs"]
(defn enable-search (defn enable-search
[f] [f]
;; {:textSearchEnabled true :setParameter 1} ;; {:textSearchEnabled true :setParameter 1}
(let [bldr (doto (BasicDBObjectBuilder.) (let [bldr (doto (BasicDBObjectBuilder.)
(.append "setParameter" 1) (.append "setParameter" 1)
(.append "textSearchEnabled" true)) (.append "textSearchEnabled" true))
cmd (.get bldr)] cmd (.get bldr)]
(is (ok? (cmd/raw-admin-command cmd)))) (is (ok? (cmd/raw-admin-command conn cmd))))
(f)) (f))
(use-fixtures :each purge-docs) (defn purge-collections
(use-fixtures :once enable-search) [f]
(mc/purge-many db [coll])
(f)
(mc/purge-many db [coll]))
(deftest ^{:search true} test-basic-full-text-search-query (use-fixtures :each purge-collections)
(let [coll "docs"] (use-fixtures :once enable-search)
(mc/ensure-index coll (array-map :subject "text" :content "text"))
(mc/insert coll {:subject "hello there" :content "this should be searchable"}) (deftest ^{:search true} test-basic-full-text-search-query
(mc/insert coll {:subject "untitled" :content "this is just noize"}) (mc/ensure-index db coll (array-map :subject "text" :content "text"))
(let [res (ms/search coll "hello") (mc/insert db coll {:subject "hello there" :content "this should be searchable"})
(mc/insert db coll {:subject "untitled" :content "this is just noize"})
(let [res (ms/search db coll "hello")
xs (ms/results-from res)] xs (ms/results-from res)]
(is (= "hello there" (-> xs first :obj :subject))) (is (= "hello there" (-> xs first :obj :subject)))
(is (= 1.0 (-> xs first :score)))))) (is (= 1.0 (-> xs first :score))))))