Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial bits of the Solr API support

  • Loading branch information...
commit 52d19f177931c359eb5a2b25d17da725385b4044 1 parent 1ffd504
@michaelklishin authored
View
1  .travis.yml
@@ -1,5 +1,6 @@
language: clojure
lein: lein2
+before_script: lein2 with-profile dev javac
script: lein2 all test
branches:
only:
View
9 project.clj
@@ -7,8 +7,13 @@
[com.basho.riak/riak-client "1.0.5"]
[cheshire "4.0.2"]
[clojurewerkz/support "0.7.0"]
- [com.novemberain/validateur "1.2.0"]]
- :source-paths ["src/clojure"]
+ [com.novemberain/validateur "1.2.0"]
+ ;; for the Riak Search Solr API support. When Riak Client supports
+ ;; search natively, we should be able to just use what it provides.
+ [clj-http "0.5.2"]]
+ :source-paths ["src/clojure"]
+ :java-source-paths ["src/java"]
+ :javac-options ["-target" "1.6" "-source" "1.6"]
:profiles {:1.3 {:dependencies [[org.clojure/clojure "1.3.0"]]}
:1.5 {:dependencies [[org.clojure/clojure "1.5.0-master-SNAPSHOT"]]}
:dev {:resource-paths ["test/resources"]
View
32 src/clojure/clojurewerkz/welle/core.clj
@@ -1,8 +1,9 @@
(ns clojurewerkz.welle.core
(:import com.basho.riak.client.raw.RawClient
- [com.basho.riak.client.raw.http HTTPClientAdapter HTTPClusterClient HTTPClusterConfig]
+ [com.basho.riak.client.raw.http HTTPClusterClient HTTPClusterConfig]
[com.basho.riak.client.raw.pbc PBClientAdapter PBClusterClient PBClusterConfig]
- com.basho.riak.client.raw.config.ClusterConfig))
+ com.basho.riak.client.raw.config.ClusterConfig
+ clojurewerkz.welle.HTTPClient))
@@ -20,16 +21,16 @@
(def ^{:const true} default-cluster-connection-limit 32)
-(defn ^com.basho.riak.client.raw.RawClient
+(defn ^clojurewerkz.welle.HTTPClient
connect
([]
(connect default-url))
([^String url]
- (let [c (HTTPClientAdapter. (com.basho.riak.client.http.RiakClient. ^String url))]
+ (let [c (HTTPClient. (com.basho.riak.client.http.RiakClient. ^String url))]
(.generateAndSetClientId c)
c))
([^String url ^bytes client-id]
- (let [^RawClient c (connect url)]
+ (let [^HTTPClient c (connect url)]
(.setClientId c client-id)
c)))
@@ -114,3 +115,24 @@
(defn stats
[]
(.stats *riak-client*))
+
+(defn get-base-url
+ "Returns base HTTP transport URL (e.g. http://127.0.0.1:8098)"
+ ([]
+ (.getBaseUrl ^HTTPClient *riak-client*))
+ ([^HTTPClient client]
+ (.getBaseUrl client)))
+
+(defn get-base-solr-url
+ "Returns base Sorl API URL (e.g. http://127.0.0.1:8098/solr)"
+ ([]
+ (get-base-solr-url *riak-client*))
+ ([^HTTPClient client]
+ (str (.getBaseUrl client) "/solr")))
+
+(defn get-solr-url
+ "Returns Sorl endpoint URL for the given index (e.g. http://127.0.0.1:8098/solr/production_index/select)"
+ ([^String index]
+ (get-solr-url *riak-client* index))
+ ([^HTTPClient client ^String index]
+ (str (get-base-solr-url *riak-client*) "/" index "/select")))
View
36 src/clojure/clojurewerkz/welle/solr.clj
@@ -0,0 +1,36 @@
+(ns ^{:doc "Provides access to Riak Search via the Solr API.
+
+ Only HTTP transport is supported."}
+ clojurewerkz.welle.solr
+ (:require [clojurewerkz.welle.core :as wc]
+ [clj-http.client :as http]
+ [cheshire.core :as json]))
+
+;;
+;; API
+;;
+
+(defn search
+ [^String index ^String query & {:as options}]
+ (let [url (wc/get-solr-url index)
+ qp (merge options
+ {"wt" "json" "q" query})
+ {:keys [body]} (http/get url {:query-params qp})]
+ (json/parse-string body true)))
+
+(defn total-hits
+ [response]
+ (get-in response [:response :numFound]))
+
+(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 [:response :docs]))
View
25 src/java/clojurewerkz/welle/HTTPClient.java
@@ -0,0 +1,25 @@
+package clojurewerkz.welle;
+
+import com.basho.riak.client.http.RiakClient;
+import com.basho.riak.client.http.RiakConfig;
+import com.basho.riak.client.raw.http.HTTPClientAdapter;
+
+/**
+ * Just like {@link com.basho.riak.client.raw.http.HTTPClientAdapter} but exposes its configuration
+ */
+public class HTTPClient extends HTTPClientAdapter {
+ protected final RiakConfig config;
+
+ public HTTPClient(RiakClient client) {
+ super(client);
+ this.config = client.getConfig();
+ }
+
+ public RiakConfig getConfig() {
+ return config;
+ }
+
+ public String getBaseUrl() {
+ return config.getBaseUrl();
+ }
+}
View
27 test/clojurewerkz/welle/test/search_test.clj
@@ -0,0 +1,27 @@
+(ns clojurewerkz.welle.test.search-test
+ (:use clojure.test
+ [clojurewerkz.welle.testkit :only [drain]])
+ (:require [clojurewerkz.welle.core :as wc]
+ [clojurewerkz.welle.kv :as kv]
+ [clojurewerkz.welle.buckets :as wb]
+ [clojurewerkz.welle.solr :as wsolr])
+ (:import com.basho.riak.client.http.util.Constants))
+
+(wc/connect!)
+
+(deftest ^{:search true} test-query-string-query-via-the-solr-api
+ (let [bucket-name "clojurewerkz.welle.solr-search-api"
+ bucket (wb/update bucket-name :last-write-wins true :enable-search true)]
+ (drain bucket-name)
+ (kv/store bucket-name "1" {:field "one"} :content-type Constants/CTYPE_JSON)
+ (kv/store bucket-name "2" {:field "two"} :content-type Constants/CTYPE_JSON)
+ (kv/store bucket-name "3" {:field "three"} :content-type Constants/CTYPE_JSON)
+ (kv/store bucket-name "4" {:field "four"} :content-type Constants/CTYPE_JSON)
+ (kv/store bucket-name "5" {:field "five"} :content-type Constants/CTYPE_JSON)
+ (kv/store bucket-name "6" {:field "six"} :content-type Constants/CTYPE_JSON)
+ (Thread/sleep 1000)
+ (let [result (wsolr/search bucket-name "five")
+ hits (wsolr/hits-from result)]
+ (println result)
+ (println hits))
+ (drain bucket-name)))
Please sign in to comment.
Something went wrong with that request. Please try again.