From 3aef7ac8629a92a6663526e46b88ad1eea51f030 Mon Sep 17 00:00:00 2001 From: Jort van Breda Date: Mon, 17 Dec 2012 10:45:41 +0100 Subject: [PATCH 1/7] Added update-doc What's up doc! --- src/clj_elasticsearch/client.clj | 1 + 1 file changed, 1 insertion(+) diff --git a/src/clj_elasticsearch/client.clj b/src/clj_elasticsearch/client.clj index 5713ff6..f1dee4d 100644 --- a/src/clj_elasticsearch/client.clj +++ b/src/clj_elasticsearch/client.clj @@ -434,6 +434,7 @@ (index-doc "org.elasticsearch.action.index.IndexRequest" []) (search "org.elasticsearch.action.search.SearchRequest" []) (get-doc "org.elasticsearch.action.get.GetRequest" [:index]) + (update-doc "org.elasticsearch.action.update.UpdateRequest" [:index :type :id]) (count-docs "org.elasticsearch.action.count.CountRequest" [:indices]) (delete-doc "org.elasticsearch.action.delete.DeleteRequest" []) (delete-by-query "org.elasticsearch.action.deletebyquery.DeleteByQueryRequest" []) From 68ed5e524e2ebdc5023e3d3cc025670bda623a78 Mon Sep 17 00:00:00 2001 From: Jort van Breda Date: Mon, 17 Dec 2012 11:10:29 +0100 Subject: [PATCH 2/7] Converting update response now as well Supah Trolllolol! --- src/clj_elasticsearch/client.clj | 1 + 1 file changed, 1 insertion(+) diff --git a/src/clj_elasticsearch/client.clj b/src/clj_elasticsearch/client.clj index f1dee4d..8ce6e53 100644 --- a/src/clj_elasticsearch/client.clj +++ b/src/clj_elasticsearch/client.clj @@ -404,6 +404,7 @@ (convert-delete-by-query "org.elasticsearch.action.deletebyquery.DeleteByQueryResponse" :object) (convert-delete-template "org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateResponse" :object) (convert-index "org.elasticsearch.action.index.IndexResponse" :object) + (convert-update "org.elasticsearch.action.update.UpdateResponse" :object) (convert-percolate "org.elasticsearch.action.percolate.PercolateResponse" :object) (convert-optimize "org.elasticsearch.action.admin.indices.optimize.OptimizeResponse" :object) (convert-analyze "org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse" :object) From 8f849d56b9f71af2a9bcc33de2e8e9e8d9e00654 Mon Sep 17 00:00:00 2001 From: Danny Wilson Date: Thu, 6 Jun 2013 13:26:09 +0200 Subject: [PATCH 3/7] Support ElasticSeacrch 0.90 and that version only. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes: - 0.90 isField, getField, setField API changes. - convert-fields only returning first value from arrays because: org.elasticsearch.index.get.GetField.getValue() only ever returns the first value… Removes: - make-compatible-decode-smile - nodes-stats: I don't know how to choose overload --- project.clj | 10 ++++------ src/clj_elasticsearch/client.clj | 30 +++++++----------------------- 2 files changed, 11 insertions(+), 29 deletions(-) diff --git a/project.clj b/project.clj index 1825b3b..6bc3c26 100644 --- a/project.clj +++ b/project.clj @@ -1,12 +1,10 @@ -(defproject clj-elasticsearch "0.4.0-RC1" +(defproject org.clojars.touch/clj-elasticsearch "0.4.0-RC2" :description "Native Java API client wrapper for Elasticsearch" - :dependencies [[org.clojure/clojure "1.4.0"] - [cheshire "5.0.1"] + :dependencies [[org.clojure/clojure "1.5.1"] + [cheshire "5.2.0"] [gavagai "0.3.1"]] :plugins [[codox "0.6.4"]] - :profiles {:dev {:dependencies [[org.elasticsearch/elasticsearch "0.20.5"]]} - :0.20 {:dependencies [[org.elasticsearch/elasticsearch "0.20.0"]]} - :0.19 {:dependencies [[org.elasticsearch/elasticsearch "0.19.0"]]}} + :profiles {:dev {:dependencies [[org.elasticsearch/elasticsearch "0.90.1"]]}} :repositories {"sonatype.org" "http://oss.sonatype.org/content/repositories/releases/"} :url "https://github.com/ngrunwald/clj-elasticsearch" :codox {:src-dir-uri "https://github.com/ngrunwald/clj-elasticsearch/blob/v0.4.0-RC1" diff --git a/src/clj_elasticsearch/client.clj b/src/clj_elasticsearch/client.clj index bed684b..20e8356 100644 --- a/src/clj_elasticsearch/client.clj +++ b/src/clj_elasticsearch/client.clj @@ -134,18 +134,6 @@ :smile (XContentFactory/smileBuilder) (XContentFactory/smileBuilder))) -(defn- make-compatible-decode-smile - "produces a fn for backward compatibility with old es version" - [] - (let [new-met (try - (.getMethod FastByteArrayOutputStream "bytes" (make-array Class 0)) - (catch NoSuchMethodException _ nil))] - (if new-met - (fn [^FastByteArrayOutputStream os] (json/decode-smile (.. os bytes toBytes) true)) - (fn [^FastByteArrayOutputStream os] (json/decode-smile (.underlyingBytes os) true))))) - -(def compatible-decode-smile (make-compatible-decode-smile)) - (defn- convert-source-result [src] (cond @@ -159,17 +147,17 @@ (defn- convert-fields [^java.util.HashMap hm] (into {} (map (fn [^org.elasticsearch.index.get.GetField f] - [(keyword (.getName f)) (convert-source-result (.getValue f))]) (.values hm)))) + [(keyword (.getName f)) (convert-source-result (if (>= 1 (count (.getValues f))) (.getValue f) #_else (.getValues f)))]) (.values hm)))) (defn- convert-get [_ ^org.elasticsearch.action.get.GetResponse response _] - (let [data (if (.exists response) + (let [data (if (.isExists response) {:_index (.getIndex response) :_type (.getType response) :_id (.getId response) :_version (.getVersion response)}) data (if-not (.isSourceEmpty response) - (assoc data :_source (convert-source-result (.sourceAsMap response))) + (assoc data :_source (convert-source-result (.getSourceAsMap response))) data) data (let [fields (.getFields response)] (if-not (empty? fields) @@ -196,7 +184,7 @@ (.toXContent response builder empty-params) (.endObject builder) (.flush builder) - (compatible-decode-smile os))) + (json/decode-smile (.. os bytes toBytes) true))) (defn- make-xconverter [class-name] @@ -289,9 +277,6 @@ ["org.elasticsearch.cluster.routing.RoutingNodes" :exclude [:blocks]] ["org.elasticsearch.cluster.routing.IndexRoutingTable"] ["org.elasticsearch.action.admin.cluster.node.stats.NodeStats"] - ;; for es < 0.20 - ["org.elasticsearch.action.admin.indices.exists.IndicesExistsResponse" - :throw? false] ;; for es > 0.20 ["org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse" :throw? false] @@ -341,7 +326,7 @@ "org.elasticsearch.action.admin.indices.segments.IndicesSegmentResponse" "org.elasticsearch.action.admin.indices.status.IndicesStatusResponse" "org.elasticsearch.action.admin.indices.stats.CommonStats" - "org.elasticsearch.action.admin.indices.stats.IndicesStats" + "org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse" "org.elasticsearch.indices.NodeIndicesStats" "org.elasticsearch.common.xcontent.ToXContent"]))) @@ -836,8 +821,6 @@ (delete-index "org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest" [:indices] []) (delete-mapping "org.elasticsearch.action.admin.indices.mapping.delete.DeleteMappingRequest" [:indices] []) (delete-template "org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest" [:name] []) - ;; for es < 0.20 - (exists-index "org.elasticsearch.action.admin.indices.exists.IndicesExistsRequest" [:indices] []) ;; for es > 0.20 (exists-index "org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest" [:indices] []) (flush-index "org.elasticsearch.action.admin.indices.flush.FlushRequest" [:indices] []) @@ -856,7 +839,8 @@ (node-info "org.elasticsearch.action.admin.cluster.node.info.NodesInfoRequest" [] []) (node-restart "org.elasticsearch.action.admin.cluster.node.restart.NodesRestartRequest" [:nodes-ids] []) (node-shutdown "org.elasticsearch.action.admin.cluster.node.shutdown.NodesShutdownRequest" [:nodes-ids] []) - (nodes-stats "org.elasticsearch.action.admin.cluster.node.stats.NodesStatsRequest" [:nodes-ids] []) + ;;; Method signature problem with method indices from class class org.elasticsearch.action.admin.cluster.node.stats.NodesStatsRequest {:class org.elasticsearch.action.admin.cluster.node.stats.NodesStatsRequest, :method "indices", :types #{boolean org.elasticsearch.action.admin.indices.stats.CommonStatsFlags}, :count 2, :all-methods [# #]} + ;(nodes-stats "org.elasticsearch.action.admin.cluster.node.stats.NodesStatsRequest" [:nodes-ids] []) (update-cluster-settings "org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest" [] [])) (def base-wrapped-params-doc From e56523dea5874fa580538349ff228631e0e3e43a Mon Sep 17 00:00:00 2001 From: Danny Wilson Date: Sun, 22 Sep 2013 15:49:58 +0200 Subject: [PATCH 4/7] Update to ElasticSearch 0.90.5 --- project.clj | 4 ++-- src/clj_elasticsearch/client.clj | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/project.clj b/project.clj index 6bc3c26..5face29 100644 --- a/project.clj +++ b/project.clj @@ -1,10 +1,10 @@ -(defproject org.clojars.touch/clj-elasticsearch "0.4.0-RC2" +(defproject org.clojars.touch/clj-elasticsearch "0.4.1" :description "Native Java API client wrapper for Elasticsearch" :dependencies [[org.clojure/clojure "1.5.1"] [cheshire "5.2.0"] [gavagai "0.3.1"]] :plugins [[codox "0.6.4"]] - :profiles {:dev {:dependencies [[org.elasticsearch/elasticsearch "0.90.1"]]}} + :profiles {:dev {:dependencies [[org.elasticsearch/elasticsearch "0.90.5"]]}} :repositories {"sonatype.org" "http://oss.sonatype.org/content/repositories/releases/"} :url "https://github.com/ngrunwald/clj-elasticsearch" :codox {:src-dir-uri "https://github.com/ngrunwald/clj-elasticsearch/blob/v0.4.0-RC1" diff --git a/src/clj_elasticsearch/client.clj b/src/clj_elasticsearch/client.clj index 20e8356..9c3bcea 100644 --- a/src/clj_elasticsearch/client.clj +++ b/src/clj_elasticsearch/client.clj @@ -8,7 +8,7 @@ [org.elasticsearch.common.settings ImmutableSettings ImmutableSettings$Builder] [org.elasticsearch.action.admin.indices.status IndicesStatusRequest] [org.elasticsearch.action ActionFuture] - [org.elasticsearch.common.io FastByteArrayOutputStream] + [org.elasticsearch.common.io.stream BytesStreamOutput] [org.elasticsearch.client.transport TransportClient] [org.elasticsearch.client.support AbstractClient] [org.elasticsearch.node Node] @@ -176,7 +176,7 @@ (defn- convert-xcontent [^org.elasticsearch.common.xcontent.ToXContent response empty-params] - (let [os (FastByteArrayOutputStream.) + (let [os (BytesStreamOutput.) builder (if (= format :json) (XContentFactory/jsonBuilder os) (XContentFactory/smileBuilder os))] From c13d5656cc1963e962e095d7e393e73d877d0d97 Mon Sep 17 00:00:00 2001 From: aroemers Date: Fri, 13 Dec 2013 13:38:56 +0100 Subject: [PATCH 5/7] Add support for explicit methods. Used this to add support for the `docAsUpsert` method of UpdateRequest. The tests have been extended accordingly (plus fixed some older ones). --- src/clj_elasticsearch/client.clj | 23 ++++++++++++++--------- test/clj_elasticsearch/test/client.clj | 19 +++++++++++++------ 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/clj_elasticsearch/client.clj b/src/clj_elasticsearch/client.clj index 9c3bcea..54eda30 100644 --- a/src/clj_elasticsearch/client.clj +++ b/src/clj_elasticsearch/client.clj @@ -489,13 +489,15 @@ (-> client (getClient) (.admin) (.cluster))) (defn- is-settable-method? - [^Class klass ^Method method] + [^Class klass ^Method method req-args] (let [return (.getReturnType method) super (.getSuperclass klass) allowed #{klass super} parameters (.getParameterTypes method) nb-params (alength parameters)] - (and (allowed return) (= nb-params 1)))) + (and (or (allowed return) + (get (set req-args) (keyword (method->arg method)))) + (= nb-params 1)))) (defn- get-executable-methods [^Class klass methods] @@ -551,9 +553,9 @@ {:method method :mapper identity :doc (clean-class-name klass)}))) (defn- get-settable-methods - [^Class klass] + [^Class klass req-args] (let [methods (.getMethods klass) - settable (filter #(is-settable-method? klass %) (seq methods)) + settable (filter #(is-settable-method? klass % req-args) (seq methods)) by-name (group-by (fn [^Method m] (.getName m)) settable)] (for [[n ms] by-name] (if (= 1 (count ms)) @@ -633,8 +635,8 @@ (.invoke async client (into-array Object (list request listener))))))) (defn- request-signature - [^Class klass] - (let [methods (get-settable-methods klass) + [^Class klass req-args] + (let [methods (get-settable-methods klass req-args) fns (for [{:keys [^Method method mapper doc]} methods] (let [m-name (-> method (method->arg) (keyword))] [m-name @@ -645,7 +647,10 @@ (.invoke method obj (into-array Object (list (mapper arg))))))) merge {::type-doc doc})]))] - (into {} fns))) + (let [result (into {} fns)] + (prn klass (keys result)) + (println) + result))) (defn select-vals [h ks] @@ -720,7 +725,7 @@ [client-type request-class-name cst-args req-args] (if-let [r-klass (class-for-name request-class-name)] (if-let [c-klass (get client-types client-type)] - (let [sig (request-signature r-klass) + (let [sig (request-signature r-klass req-args) all-args (keys sig) setters (vals sig) get-client-fn (case client-type @@ -810,7 +815,7 @@ (percolate "org.elasticsearch.action.percolate.PercolateRequest" [:index :type] [:source]) (scroll "org.elasticsearch.action.search.SearchScrollRequest" [:scroll-id] []) ;; for es > 0.20 - (update-doc "org.elasticsearch.action.update.UpdateRequest" [:index :type :id] [:script])) + (update-doc "org.elasticsearch.action.update.UpdateRequest" [:index :type :id] [:script :doc-as-upsert?])) (def-requests :indices (optimize-index "org.elasticsearch.action.admin.indices.optimize.OptimizeRequest" [] []) diff --git a/test/clj_elasticsearch/test/client.clj b/test/clj_elasticsearch/test/client.clj index 6978b7d..3d3b136 100644 --- a/test/clj_elasticsearch/test/client.clj +++ b/test/clj_elasticsearch/test/client.clj @@ -59,7 +59,7 @@ (is (realized? ft)) (is (not (future-cancelled? ft)))) (let [status (index-status {:indices ["test"]})] - (is (= 1 (get-in status [:indices :test :docs :num_docs])))) + (is (= 1 (get-in status [:indices :test :docs :num_docs])))) (let [d (get-doc {:index "test" :type "tyu" :id "mid" :fields ["field1" "field2" :field3]})] (is (nil? (:_source d))) (is (= {:tyu {:foo "bar"}} (get-in d [:fields :field3]))) @@ -94,10 +94,17 @@ (assoc o :bar "baz")) {:index "test" :type "vvv" :id "neo"}) _ (atomic-update-from-source (fn [_] {:foo "bar"}) {:index "test" :type "vvv" :id "geo"})] - (is {:foo "bar" :bar "baz" :ooh "sneaky"} - (:_source (get-doc {:index "test" :type "vvv" :id "neo"}))) - (is {:foo "bar"} - (:_source (get-doc {:index "test" :type "vvv" :id "geo"}))) + (is (= {:foo "bar" :bar "baz"} + (:_source (get-doc {:index "test" :type "vvv" :id "neo"})))) + (is (= {:foo "bar"} + (:_source (get-doc {:index "test" :type "vvv" :id "geo"})))) (is (not (empty? (get (get-mapping {}) "test")))) (is (not (empty? (get @(get-mapping {:async? true}) "test")))) - (is (empty? (get (get-mapping {:types ["wrong"]}) "test"))))) + (is (empty? (get (get-mapping {:types ["wrong"]}) "test")))) + + (let [payload {:index "test" :type "abc" + :doc {:new "source"} + :id "upserted"}] + (update-doc (assoc payload :doc-as-upsert? true)) + (is (= {:new "source"} + (:_source (get-doc {:index "test" :type "abc" :id "upserted"})))))) From 59d6980706f0c5d620c04a08a705b3f3f74eeb02 Mon Sep 17 00:00:00 2001 From: aroemers Date: Fri, 13 Dec 2013 13:41:10 +0100 Subject: [PATCH 6/7] Update to version 0.4.2. --- project.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project.clj b/project.clj index 5face29..82a958a 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject org.clojars.touch/clj-elasticsearch "0.4.1" +(defproject org.clojars.touch/clj-elasticsearch "0.4.2" :description "Native Java API client wrapper for Elasticsearch" :dependencies [[org.clojure/clojure "1.5.1"] [cheshire "5.2.0"] From ad469bc0b67537f08c4cc4cfddbd243ec2744644 Mon Sep 17 00:00:00 2001 From: aroemers Date: Fri, 13 Dec 2013 13:46:48 +0100 Subject: [PATCH 7/7] Remove silly println. --- src/clj_elasticsearch/client.clj | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/clj_elasticsearch/client.clj b/src/clj_elasticsearch/client.clj index 54eda30..a1531a4 100644 --- a/src/clj_elasticsearch/client.clj +++ b/src/clj_elasticsearch/client.clj @@ -647,10 +647,7 @@ (.invoke method obj (into-array Object (list (mapper arg))))))) merge {::type-doc doc})]))] - (let [result (into {} fns)] - (prn klass (keys result)) - (println) - result))) + (into {} fns))) (defn select-vals [h ks]