Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Support [Query] Validation API

  • Loading branch information...
commit 1f037722189b6ca3d53d5101f16c3b7acdb64ab7 1 parent e780923
@michaelklishin michaelklishin authored
View
4 src/clojurewerkz/elastisch/rest.clj
@@ -187,6 +187,10 @@
[^String index-name ^String percolator]
(join slash [(:uri *endpoint*) index-name percolator "_percolate" ]))
+(defn query-validation-url
+ [^String index-name]
+ (join slash [(:uri *endpoint*) index-name "_validate" "query"]))
+
;;
;; API
;;
View
16 src/clojurewerkz/elastisch/rest/document.clj
@@ -1,6 +1,7 @@
(ns clojurewerkz.elastisch.rest.document
(:refer-clojure :exclude [get replace count sort])
- (:require [clojurewerkz.elastisch.rest :as rest])
+ (:require [clojurewerkz.elastisch.rest :as rest]
+ [cheshire.core :as json])
(:use clojure.set
[clojurewerkz.elastisch.rest.utils :only [join-names]]
[clojurewerkz.elastisch.rest.response :only [not-found?]]))
@@ -65,7 +66,7 @@
[index mapping-type & {:as options}]
(let [qk [:search_type :scroll]
qp (select-keys options qk)
- body (dissoc options qk)]
+ body (apply dissoc (concat [options] qk))]
(rest/post (rest/search-url (join-names index) (join-names mapping-type))
:body body
:query-params qp)))
@@ -75,7 +76,7 @@
[index & {:as options}]
(let [qk [:search_type :scroll]
qp (select-keys options qk)
- body (dissoc options qk)]
+ body (apply dissoc (concat [options] qk))]
(rest/post (rest/search-url (join-names index))
:body body
:query-params qp)))
@@ -86,7 +87,7 @@
[index & {:as options}]
(let [qk [:search_type :scroll]
qp (select-keys options qk)
- body (dissoc options qk)]
+ body (apply dissoc (concat [options] qk))]
(rest/post (rest/search-url)
:body body
:query-params qp)))
@@ -158,5 +159,8 @@
(rest/get (rest/more-like-this-url index mapping-type id)
:query-params params))
-;; TODO percolate
-;; TODO multi-search
+(defn validate-query
+ "Validates a query without actually executing it. Has the same API as clojurewerkz.elastisch.rest.document/search
+ but does not take the mapping type parameter."
+ [index query & {:as options}]
+ (rest/get (rest/query-validation-url index) :body (json/encode query) :query-params options))
View
3  src/clojurewerkz/elastisch/rest/percolation.clj
@@ -20,6 +20,5 @@
"Percolates a document and see which queries match on it. The document is not indexed, just
matched against the queries you register with clojurewerkz.elastisch.rest.percolation/register-query."
[index percolator & {:as options}]
- ;; rest/get won't serialize the body for us because we don't use GET requests
- ;; w/ body for other features. MK.
+ ;; rest/get won't serialize the body for us. MK.
(rest/get (rest/index-percolation-url index percolator) :body (json/encode options)))
View
5 src/clojurewerkz/elastisch/rest/response.clj
@@ -24,6 +24,11 @@
[response]
(:acknowledged response))
+(defn valid?
+ "Returns true if a validation query response indicates valid query, false otherwise"
+ [response]
+ (:valid response))
+
(defn timed-out?
[response]
(:timed_out response))
View
10 test/clojurewerkz/elastisch/rest_api/search_test.clj
@@ -45,3 +45,13 @@
:filter {:term {:username "esmary"}}))]
(is (= 1 (count hits)))
(is (= "Lindey" (-> hits first :_source :last-name)))))
+
+
+;;
+;; Query validation
+;;
+
+(deftest ^{:query true} test-query-validation
+ (let [index-name "articles"
+ response (doc/validate-query index-name (q/field "latest-edit.author" "Thorwald") :explain true)]
+ (is (valid? response))))
Please sign in to comment.
Something went wrong with that request. Please try again.