From 6ca5b9d4baf75a709bf361371bf92715a0af4550 Mon Sep 17 00:00:00 2001 From: Michael Klishin Date: Sat, 16 May 2015 15:38:41 +0300 Subject: [PATCH] monger.search is gone Full text search in 3.0 is supported via regular queries with the $text operator. --- src/clojure/monger/command.clj | 4 -- src/clojure/monger/operators.clj | 3 ++ src/clojure/monger/search.clj | 46 ---------------------- test/monger/test/full_text_search_test.clj | 20 ++-------- 4 files changed, 7 insertions(+), 66 deletions(-) delete mode 100644 src/clojure/monger/search.clj diff --git a/src/clojure/monger/command.clj b/src/clojure/monger/command.clj index 2ce82d2..cb59133 100644 --- a/src/clojure/monger/command.clj +++ b/src/clojure/monger/command.clj @@ -81,7 +81,3 @@ (defn top [^MongoClient conn] (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})) diff --git a/src/clojure/monger/operators.clj b/src/clojure/monger/operators.clj index 9e6b602..1da2e50 100644 --- a/src/clojure/monger/operators.clj +++ b/src/clojure/monger/operators.clj @@ -259,3 +259,6 @@ ;; full text search (defoperator $text) +(defoperator $search) +(defoperator $language) +(defoperator $natural) diff --git a/src/clojure/monger/search.clj b/src/clojure/monger/search.clj deleted file mode 100644 index 6682887..0000000 --- a/src/clojure/monger/search.clj +++ /dev/null @@ -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")))))) diff --git a/test/monger/test/full_text_search_test.clj b/test/monger/test/full_text_search_test.clj index 3e69143..29948f3 100644 --- a/test/monger/test/full_text_search_test.clj +++ b/test/monger/test/full_text_search_test.clj @@ -1,8 +1,8 @@ (ns monger.test.full-text-search-test (:require [monger.core :as mg] [monger.collection :as mc] - [monger.search :as ms] [monger.command :as cmd] + [monger.operators :refer :all] [clojure.test :refer [deftest is use-fixtures]] [monger.result :refer [acknowledged?]]) (:import com.mongodb.BasicDBObjectBuilder)) @@ -11,16 +11,6 @@ db (mg/get-db conn "monger-test") 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 [f] (mc/purge-many db [coll]) @@ -28,13 +18,11 @@ (mc/purge-many db [coll])) (use-fixtures :each purge-collections) - (use-fixtures :once enable-search) (deftest ^{:search true} test-basic-full-text-search-query (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 "untitled" :content "this is just noize"}) - (let [res (ms/search db coll "hello") - xs (ms/results-from res)] - (is (= "hello there" (-> xs first :obj :subject))) - (is (= 1.0 (-> xs first :score)))))) + (let [xs (mc/find-maps db coll {$text {$search "hello"}})] + (is (= 1 (count xs))) + (is (= "hello there" (-> xs first :subject))))))