Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create filter queries as hashmaps #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 11 additions & 1 deletion src/flux/query.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand Down
4 changes: 4 additions & 0 deletions test/flux/unit/query.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]"]))