Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Keywordify doc keys, cljify responses and add :id.

  • Loading branch information...
commit 1cc9d2c904e73466f569de6cd2148b2bc1a51fbf 1 parent 699636c
@drewr drewr authored
View
14 src/esperanto/index.clj
@@ -4,15 +4,18 @@
(defn make-index-request
([client idx source]
- (make-index-request client idx (or (get source "_type")
+ (make-index-request client idx (or (get source :_type)
+ (get source "_type")
+ (get source :type)
(get source "type"))
source))
([client idx type source]
(let [req (-> client
(.prepareIndex idx type)
- #_(.setSource (json/generate-string source))
- (.setSource source))]
- (if-let [id (get source "id")]
+ (.setSource (json/generate-string source))
+ #_(.setSource source))]
+ (if-let [id (or (get source :id)
+ (get source "id"))]
(-> req (.setId (str id)))
req))))
@@ -26,7 +29,8 @@
br)))
(defn index-doc [client idx doc]
- @(execute (make-index-request client idx doc)))
+ (merge doc
+ {:id (.getId @(execute (make-index-request client idx doc)))}))
(defn index-bulk
([client reqs]
View
24 src/esperanto/search.clj
@@ -31,11 +31,31 @@
(.prepareCount (into-array idxs))
(.setQuery (QueryBuilders/queryString query))))
+(defn hit->clj [hit]
+ (with-meta (merge {:id (.getId hit)}
+ (cheshire.core/parse-string
+ (.sourceAsString hit) :kw))
+ {:index (.getIndex hit)
+ :node (-> hit .getShard .getNodeId)
+ :shard (-> hit .getShard .getShardId)
+ :sort-vals (seq (.getSortValues hit))}))
+
+(defn search->clj [r]
+ (with-meta (map hit->clj (.getHits r))
+ {:facets (.getFacets r)
+ :shards (.getTotalShards r)
+ :shards-bad (.getFailedShards r)
+ :shards-good (.getSuccessfulShards r)
+ :status (bean (.status r))
+ :timed-out? (.isTimedOut r)
+ :took (.getTookInMillis r)
+ :total (-> r .getHits .getTotalHits)}))
+
(defn search
([client idx]
(search client idx "*:*"))
([client idx query]
- @(execute (make-search-request client idx query))))
+ (search->clj @(execute (make-search-request client idx query)))))
(defn count
([client idx]
@@ -51,7 +71,7 @@
(when-let [hits (seq
(-> @(execute (make-scroll-request client id timeout))
.hits))]
- (cons hits (scroll client id timeout)))))
+ (cons (map hit->clj hits) (scroll client id timeout)))))
(defn scan
([client idx query timeout]
View
19 test/esperanto/test/search.clj
@@ -13,8 +13,8 @@
(def index "twitter")
-(def doc {"type" "tweet"
- "text" "The quick brown fox jumps over the lazy dog"})
+(def doc {:type "tweet"
+ :text "The quick brown fox jumps over the lazy dog"})
(defn doc-seq [n]
(for [i (range n)]
@@ -25,7 +25,7 @@
(defn doc-seq [n]
(for [i (range n)]
- (merge doc {"id" (str i)})))
+ (merge doc {:id (str i)})))
(use-fixtures :once (node-fixture node))
(use-fixtures :each (index-fixture node index))
@@ -33,13 +33,13 @@
(deftest t-index-single
(index-doc client index doc)
(refresh client index)
- (is (= 1 (-> (search client index "quick") .hits .totalHits))))
+ (is (= 1 (-> (search 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") .hits .totalHits))))
+ (is (= 2 (-> (search client index "quick") meta :total))))
(deftest t-index-bulk
(let [resp (index-bulk client index (repeat 100 doc))
@@ -49,23 +49,22 @@
(is (< (.getTookInMillis resp) timeout)
(format "*** bulk index took longer than %dms" timeout))
(is (not (.hasFailures resp)))
- (is (= 100 (-> sresp .hits .totalHits)))))
+ (is (= 100 (-> sresp meta :total)))))
(deftest t-index-seq
(let [ct 100
coll (doc-seq ct)
- ids (into #{} (map #(get % "id") coll))
+ ids (into #{} (map :id coll))
bulk (index-bulk client index coll)
_ (refresh client index)]
(is (not (.hasFailures bulk)))
;; How many docs does ES think the index has?
(is (= ct (count client index)))
;; How many docs does a search for all docs return?
- (is (= ct (-> (search client index) .hits .totalHits)))
+ (is (= ct (-> (search client index) meta :total)))
;; Are the ids exactly what we indexed?
(is (= ct (clojure.core/count (index-seq client index))))
- (is (= ids (into #{} (map #(-> % .id)
- (index-seq client index)))))))
+ (is (= ids (into #{} (map :id (index-seq client index)))))))
(deftest t-count
(index-doc client index doc)
Please sign in to comment.
Something went wrong with that request. Please try again.