diff --git a/README.md b/README.md index 1e91498..7818a34 100644 --- a/README.md +++ b/README.md @@ -164,6 +164,14 @@ Once a connection as been created, use the `with-connection` macro to wrap clien (q/create-query-request :post "/docs" {:q "etc"})) ``` +####Query options +You can also specify additional query options (filter queries, fields, ...): + +```clojure + (let [options {:fq {:id 1 :range_field [1 5]} :sort "id asc" :fl ["id" "another_field"]}] + (flux/query "*:*" options) +``` + ###javax.servlet/servlet-api and EmbeddedSolrServer Unfortunately, EmbeddedSolrServer requires javax.servlet/servlet-api as an implicit dependency. Because of this, Flux adds this lib as a depedency. diff --git a/src/flux/query.clj b/src/flux/query.clj index bfe2d63..d3b40bc 100644 --- a/src/flux/query.clj +++ b/src/flux/query.clj @@ -13,6 +13,15 @@ (defn- format-values [v] (into-array (mapv format-param (if (coll? v) v [v])))) +(defn- format-range [coll] + (str "[" (format-param (first coll)) " TO " (format-param (second coll)) "]")) + +(defn- format-fq [[k v]] + (str (format-param k) ":" (if (coll? v) (format-range v) (format-param v)))) + +(defn format-filter-queries [hm] + (mapv format-fq hm)) + (defn- create-solr-params [m] (MultiMapSolrParams. (reduce-kv (fn [^java.util.HashMap hm k v] @@ -22,7 +31,8 @@ (java.util.HashMap.) m))) (defn create-query [query options] - (create-solr-params (assoc options :q query))) + (let [filter-queries (format-filter-queries (:fq options))] + (create-solr-params (assoc (assoc options :fq filter-queries) :q query)))) (defn create-query-request ([params] diff --git a/test/flux/unit/query.clj b/test/flux/unit/query.clj index e6b8fe1..5f8c285 100644 --- a/test/flux/unit/query.clj +++ b/test/flux/unit/query.clj @@ -10,3 +10,7 @@ (fact "create-query-request w/path and method" (create-query-request :post "/docs" {:q "*:*"}) => anything) + +(fact "format-filter-queries" + (let [fqs {:country "IT" :range ["1" "6"]}] + (format-filter-queries fqs) => ["country:IT" "range:[1 TO 6]"]))