monger.search is gone
Full text search in 3.0 is supported via regular queries with the $text operator.
This commit is contained in:
parent
a8d42a62f5
commit
6ca5b9d4ba
4 changed files with 7 additions and 66 deletions
|
|
@ -81,7 +81,3 @@
|
||||||
(defn top
|
(defn top
|
||||||
[^MongoClient conn]
|
[^MongoClient conn]
|
||||||
(monger.core/command (monger.core/admin-db conn) {:top 1}))
|
(monger.core/command (monger.core/admin-db conn) {:top 1}))
|
||||||
|
|
||||||
(defn search
|
|
||||||
[^DB db ^String collection query]
|
|
||||||
(monger.core/command db {"text" collection "search" query}))
|
|
||||||
|
|
|
||||||
|
|
@ -259,3 +259,6 @@
|
||||||
|
|
||||||
;; full text search
|
;; full text search
|
||||||
(defoperator $text)
|
(defoperator $text)
|
||||||
|
(defoperator $search)
|
||||||
|
(defoperator $language)
|
||||||
|
(defoperator $natural)
|
||||||
|
|
|
||||||
|
|
@ -1,46 +0,0 @@
|
||||||
;; Copyright (c) 2011-2014 Michael S. Klishin
|
|
||||||
;;
|
|
||||||
;; The use and distribution terms for this software are covered by the
|
|
||||||
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
|
|
||||||
;; which can be found in the file epl-v10.html at the root of this distribution.
|
|
||||||
;; By using this software in any fashion, you are agreeing to be bound by
|
|
||||||
;; the terms of this license.
|
|
||||||
;; You must not remove this notice, or any other, from this software.
|
|
||||||
|
|
||||||
(ns monger.search
|
|
||||||
"Full text search queries support (MongoDB 2.4+)"
|
|
||||||
(:require [monger.command :as cmd]
|
|
||||||
[monger.conversion :as cnv])
|
|
||||||
(:import [com.mongodb DB CommandResult BasicDBList DBObject]))
|
|
||||||
|
|
||||||
;;
|
|
||||||
;; Implementation
|
|
||||||
;;
|
|
||||||
|
|
||||||
(defn- convert-hit
|
|
||||||
[^DBObject dbo keywordize-keys?]
|
|
||||||
(cnv/from-db-object dbo keywordize-keys?))
|
|
||||||
|
|
||||||
|
|
||||||
;;
|
|
||||||
;; API
|
|
||||||
;;
|
|
||||||
|
|
||||||
(defn search
|
|
||||||
"Performs a full text search query"
|
|
||||||
[^DB db ^String collection query]
|
|
||||||
(cmd/search db collection query))
|
|
||||||
|
|
||||||
(defn results-from
|
|
||||||
"Returns a lazy sequence of results from a search query response, sorted by score.
|
|
||||||
|
|
||||||
Each result is a Clojure map with two keys: :score and :obj."
|
|
||||||
([^CommandResult res]
|
|
||||||
(results-from res true))
|
|
||||||
([^CommandResult res keywordize-keys?]
|
|
||||||
(let [sorter (if keywordize-keys?
|
|
||||||
:score
|
|
||||||
(fn [m]
|
|
||||||
(get m "score")))]
|
|
||||||
(sort-by sorter >
|
|
||||||
(map #(convert-hit % keywordize-keys?) ^BasicDBList (.get res "results"))))))
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
(ns monger.test.full-text-search-test
|
(ns monger.test.full-text-search-test
|
||||||
(:require [monger.core :as mg]
|
(:require [monger.core :as mg]
|
||||||
[monger.collection :as mc]
|
[monger.collection :as mc]
|
||||||
[monger.search :as ms]
|
|
||||||
[monger.command :as cmd]
|
[monger.command :as cmd]
|
||||||
|
[monger.operators :refer :all]
|
||||||
[clojure.test :refer [deftest is use-fixtures]]
|
[clojure.test :refer [deftest is use-fixtures]]
|
||||||
[monger.result :refer [acknowledged?]])
|
[monger.result :refer [acknowledged?]])
|
||||||
(:import com.mongodb.BasicDBObjectBuilder))
|
(:import com.mongodb.BasicDBObjectBuilder))
|
||||||
|
|
@ -11,16 +11,6 @@
|
||||||
db (mg/get-db conn "monger-test")
|
db (mg/get-db conn "monger-test")
|
||||||
coll "search-docs"]
|
coll "search-docs"]
|
||||||
|
|
||||||
(defn enable-search
|
|
||||||
[f]
|
|
||||||
;; {:textSearchEnabled true :setParameter 1}
|
|
||||||
(let [bldr (doto (BasicDBObjectBuilder.)
|
|
||||||
(.append "setParameter" 1)
|
|
||||||
(.append "textSearchEnabled" true))
|
|
||||||
cmd (.get bldr)]
|
|
||||||
(is (acknowledged? (cmd/raw-admin-command conn cmd))))
|
|
||||||
(f))
|
|
||||||
|
|
||||||
(defn purge-collections
|
(defn purge-collections
|
||||||
[f]
|
[f]
|
||||||
(mc/purge-many db [coll])
|
(mc/purge-many db [coll])
|
||||||
|
|
@ -28,13 +18,11 @@
|
||||||
(mc/purge-many db [coll]))
|
(mc/purge-many db [coll]))
|
||||||
|
|
||||||
(use-fixtures :each purge-collections)
|
(use-fixtures :each purge-collections)
|
||||||
(use-fixtures :once enable-search)
|
|
||||||
|
|
||||||
(deftest ^{:search true} test-basic-full-text-search-query
|
(deftest ^{:search true} test-basic-full-text-search-query
|
||||||
(mc/ensure-index db coll (array-map :subject "text" :content "text"))
|
(mc/ensure-index db coll (array-map :subject "text" :content "text"))
|
||||||
(mc/insert db coll {:subject "hello there" :content "this should be searchable"})
|
(mc/insert db coll {:subject "hello there" :content "this should be searchable"})
|
||||||
(mc/insert db coll {:subject "untitled" :content "this is just noize"})
|
(mc/insert db coll {:subject "untitled" :content "this is just noize"})
|
||||||
(let [res (ms/search db coll "hello")
|
(let [xs (mc/find-maps db coll {$text {$search "hello"}})]
|
||||||
xs (ms/results-from res)]
|
(is (= 1 (count xs)))
|
||||||
(is (= "hello there" (-> xs first :obj :subject)))
|
(is (= "hello there" (-> xs first :subject))))))
|
||||||
(is (= 1.0 (-> xs first :score))))))
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue