Skip to content
Browse files

Merge `add-document` and `add-documents`

  • Loading branch information...
1 parent 1e62c63 commit 335386753d5024b084b01121e7f388aa92efcea9 @fracek committed Jul 21, 2011
Showing with 19 additions and 14 deletions.
  1. +1 −1 README.mdown
  2. +18 −13 src/indextank_clj/core.clj
View
2 README.mdown
@@ -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**
View
31 src/indextank_clj/core.clj
@@ -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

0 comments on commit 3353867

Please sign in to comment.
Something went wrong with that request. Please try again.