Permalink
Browse files

Favor json for searches. Remove multimethod.

  • Loading branch information...
drewr committed Sep 24, 2011
1 parent bc51fd2 commit 88a2c992d32b03909da6cf90382c37526d60e9a4
Showing with 19 additions and 21 deletions.
  1. +16 −18 src/esperanto/search.clj
  2. +3 −3 test/esperanto/test/search.clj
View
@@ -2,25 +2,17 @@
(:refer-clojure :exclude [count])
(:require [cheshire.core :as json])
(:use [esperanto.action :only [execute]])
- (:import (clojure.lang PersistentVector)
- (org.elasticsearch.action.search SearchType)
+ (:import (org.elasticsearch.action.search SearchType)
(org.elasticsearch.client.node NodeClient)
(org.elasticsearch.common.unit TimeValue)
(org.elasticsearch.index.query QueryBuilders)))
-(def make-search-request* #(vec (map type %&)))
-
-(defmulti make-search-request #(apply #'make-search-request* %&))
-
-(defmethod make-search-request [NodeClient String String]
- [client idx query]
- (make-search-request client [idx] query))
-
-(defmethod make-search-request [NodeClient PersistentVector String]
- [client idxs query]
- (-> client
- (.prepareSearch (into-array idxs))
- (.setQuery (QueryBuilders/queryString query))))
+(defn make-search-request [client idxs query]
+ (let [idxs (if (sequential? idxs) idxs (vector idxs))
+ idxs (into-array String idxs)]
+ (-> client
+ (.prepareSearch idxs)
+ (.setQuery (json/generate-string query)))))
(defn make-scroll-request [client id timeout]
(-> client
@@ -54,9 +46,15 @@
(defn search
([client idx]
- (search client idx "*:*"))
+ (search client idx {:match_all {}}))
+ ([client idx query]
+ (search->clj
+ @(execute (make-search-request client idx query)))))
+
+(defn searchq
([client idx query]
- (search->clj @(execute (make-search-request client idx query)))))
+ (search client idx {:query_string
+ {:query query}})))
(defn count
([client idx]
@@ -87,7 +85,7 @@
(defn index-seq
([client idx]
- (index-seq client idx "*:*"))
+ (index-seq client idx {:match_all {}}))
([client idx query]
(index-seq client idx query 120000))
([client idx query timeout]
@@ -33,18 +33,18 @@
(deftest t-index-single
(index-doc client index doc)
(refresh client index)
- (is (= 1 (-> (search client index "quick") meta :total))))
+ (is (= 1 (-> (searchq client index "quick") meta :total))))
(deftest t-index-double
(index-doc client index doc)
(index-doc client index doc)
(refresh client index)
- (is (= 2 (-> (search client index "quick") meta :total))))
+ (is (= 2 (-> (searchq client index "quick") meta :total))))
(deftest t-index-bulk
(let [resp (index-bulk client index (repeat 100 doc))
_ (refresh client index)
- sresp (search client index "quick")
+ sresp (searchq client index "quick")
timeout 500]
(is (< (.getTookInMillis resp) timeout)
(format "*** bulk index took longer than %dms" timeout))

0 comments on commit 88a2c99

Please sign in to comment.