Skip to content

Commit

Permalink
Implement elastisch.document/count
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelklishin committed Apr 2, 2012
1 parent a941f84 commit 0f096c1
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 35 deletions.
35 changes: 26 additions & 9 deletions src/clojurewerkz/elastisch/document.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(ns clojurewerkz.elastisch.document
(:refer-clojure :exclude [get replace])
(:require [clojurewerkz.elastisch.utils :as utils]
[clojurewerkz.elastisch.rest :as rest])
(:require [clojurewerkz.elastisch.rest :as rest])
(:use clojure.set
[clojurewerkz.elastisch.utils :only [join-names]]
[clojurewerkz.elastisch.response :only [not-found?]]))

;;
Expand Down Expand Up @@ -57,10 +57,11 @@

(defn search
"Performs a search query"
[index-name type-name & { :as options }]
(let [qp (select-keys options [:search-type :scroll :size])
body (difference options qp)]
(rest/post (rest/search (utils/join-names index-name) (utils/join-names type-name))
[index type & { :as options }]
(let [qk [:search_type :scroll :size]
qp (select-keys options qk)
body (dissoc options qk)]
(rest/post (rest/search (join-names index) (join-names type))
:body body
:query-params qp)))

Expand All @@ -70,6 +71,22 @@
(delete idx-name idx-type id)
(put idx-name idx-type id document))

;; defn count
;; defn delete-by-query
;; defn more-like-this
(defn count
"Performs a count query.
For Elastic Search reference, see http://www.elasticsearch.org/guide/reference/api/count.html"
([index type]
(rest/post (rest/count-path) (join-names index) (join-names type)))
([index type query]
(rest/post (rest/count-path) (join-names index) (join-names type) :body query))
([idx-name idx-type query & { :as options }]
(let [qk [:q :df :analyzer :default_operator]
qp (select-keys options qk)]
(rest/post (rest/count-path) (join-names index) (join-names type)
:query-params qp
:body query))))

;; TODO delete-by-query
;; TODO more-like-this
;; TODO percolate
;; TODO multi-search
4 changes: 4 additions & 0 deletions src/clojurewerkz/elastisch/response.clj
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@
(defn hits-from
[response]
(get-in response [:hits :hits]))

(defn count-from
[response]
(get response :count))
14 changes: 10 additions & 4 deletions src/clojurewerkz/elastisch/rest.clj
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,23 @@
(:uri *endpoint*))

(defn index
[^String index-name]
(join slash [(base) index-name]))
[index-name]
(str (base) slash index-name))

(defn index-type
[^String index-name ^String index-type]
[index-name index-type]
(join slash [(base) index-name index-type]))

(defn search
[^String index-name ^String index-type]
[index-name index-type]
(join slash [(base) index-name index-type "_search"]))

(defn count-path
([]
(str (base) slash "_count"))
([index-name index-type]
(join slash [(base) index-name index-type "_count"])))

(defn record
[^String index-name ^String type id]
(join slash [(base) index-name type id]))
Expand Down
16 changes: 16 additions & 0 deletions test/clojurewerkz/elastisch/document_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,22 @@
(doc/put index-name index-type "1" fx/person-jack)
(is (doc/present? index-name index-type "1")))


;;
;; count
;;

(deftest test-count-with-a-term-query
(idx/create index-name :mappings fx/people-mapping)
(doc/create index-name index-type fx/person-jack)
(doc/create index-name index-type fx/person-joe)
(idx/refresh index-name)
(are [c r] (is (= c (count-from r)))
1 (doc/count index-name index-type (q/term :username "esjack"))
1 (doc/count index-name index-type (q/term :username "esjoe"))
0 (doc/count index-name index-type (q/term :username "esmary"))))


;;
;; delete
;;
Expand Down
46 changes: 24 additions & 22 deletions test/clojurewerkz/elastisch/fixtures.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,30 @@
(f)
(index/delete "people"))

(def person-jack {
:first-name "Jack"
:last-name "Black"
:title "Sales Manager"
:biography "Tries to avoid eating fat, being good to other people and does sports every now and then" })
(def person-jack
{:username "esjack"
:first-name "Jack"
:last-name "Black"
:title "Sales Manager"
:biography "Tries to avoid eating fat, being good to other people and does sports every now and then" })

(def person-joe {
:first-name "Joe"
:last-name "Mahjo"
:title "Trader"
:biography "Quite a nice guy" })
(def person-joe
{:username "esjoe"
:first-name "Joe"
:last-name "Mahjo"
:title "Trader"
:biography "Quite a nice guy" })

(def person-mary {
:first-name "Mary"
:last-name "Lindey"
:title "Copywriter"
:biography "Writes copy and copies writes" })
(def person-mary
{:username "esmary"
:first-name "Mary"
:last-name "Lindey"
:title "Copywriter"
:biography "Writes copy and copies writes" })

(def people-mapping {
:person {
:properties {
:first-name { :type "string" :store "yes" }
:last-name { :type "string" }
:title { :type "string" :analyzer "snowball" }
:biography { :type "string" :analyzer "snowball"}}}})
(def people-mapping
{ :person { :properties {:username { :type "string" :store "yes" }
:first-name { :type "string" :store "yes" }
:last-name { :type "string" }
:title { :type "string" :analyzer "snowball" }
:biography { :type "string" :analyzer "snowball"}}}})

0 comments on commit 0f096c1

Please sign in to comment.