Skip to content

Commit

Permalink
Merge add-document and add-documents
Browse files Browse the repository at this point in the history
  • Loading branch information
fracek committed Jul 21, 2011
1 parent 1e62c63 commit 3353867
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.mdown
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ You can add, update or delete documents from the index

(with-client privurl
(add-document "blog" first-post) ;; Add one document
(add-documents "blog" two-posts) ;; Add more documents at once
(add-document "blog" two-posts) ;; Add more documents at once
(delete-document "blog" "p1")) ;; Delete the doc with :docid "p1"

**SCORING VARIABLES**
Expand Down
31 changes: 18 additions & 13 deletions src/indextank_clj/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,27 @@
[doc]
(and (contains? doc :docid) (contains? doc :fields)))

;; TODO: merge add-document and add-documents
(defn add-document
"Adds a document to the index name"
(defn document-arg-type
[arg]
(cond (and (map? arg) (valid-doc? arg)) :map
(and (vector? arg) (every? valid-doc? arg)) :vector
:else (throw
(IllegalArgumentException.
"Either a map or a vector of maps with :docid and :fields keys."))))

(defmulti add-document
"Takes a map or a vector of maps and adds them to the index.
The maps must have :docid and :fields keys"
(fn [name arg]
(document-arg-type arg)))

(defmethod add-document :map
[name doc]
(if (valid-doc? doc)
(wrap-request :put
(str "/v1/indexes/" name "/docs") (json-str doc))
(throw (Exception. "the doc map must have :docid and :fields"))))
(wrap-request :put (str "/v1/indexes/" name "/docs") (json-str doc)))

(defn add-documents
"Adds a batch of documents to the index name"
(defmethod add-document :vector
[name docs]
(if (every? valid-doc? docs)
(wrap-request :put
(str "/v1/indexes/" name "/docs") (json-str docs))
(throw (Exception. "the doc map must have :docid and :fields"))))
(wrap-request :put (str "/v1/indexes/" name "/docs") (json-str docs)))

;; We use a querystring because we can't send a body in DELETE
(defn delete-document
Expand Down

0 comments on commit 3353867

Please sign in to comment.