/
response.clj
66 lines (52 loc) · 1.43 KB
/
response.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
(ns clojurewerkz.elastisch.rest.response
(:require [clojurewerkz.support.http.statuses :as statuses]))
;;
;; API
;;
(defn ok?
[response]
(true? (get response :ok)))
(defn conflict?
[response]
(let [s (:status response)]
(and s (statuses/conflict? s))))
(defn not-found?
[response]
(let [s (:status response)]
(or (false? (:exists response))
(and s (statuses/missing? s)))))
(defn acknowledged?
[response]
(:acknowledged response))
(defn timed-out?
[response]
(:timed_out response))
(defn total-hits
"Returns number of search hits from a response"
[response]
(get-in response [:hits :total]))
(defn count-from
"Returns total number of search hits from a response"
[response]
(get response :count))
(defn any-hits?
"Returns true if a response has any search hits, false otherwise"
[response]
(> (total-hits response) 0))
(def no-hits? (complement any-hits?))
(defn hits-from
"Returns search hits from a response as a collection. To retrieve hits overview, get the :hits
key from the response"
[response]
(get-in response [:hits :hits]))
(defn facets-from
"Returns facets information (overview and actual facets) from a response as a map. Keys in the map depend on
the facets query performed"
[response]
(get response :facets {}))
(defn ids-from
"Returns search hit ids from a response"
[response]
(if (any-hits? response)
(set (map :_id (hits-from response)))
#{}))