Skip to content

Instantly share code, notes, and snippets.

@zentrope
Created April 11, 2016 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zentrope/aea55ff520da2fad85837c4ce6514222 to your computer and use it in GitHub Desktop.
Save zentrope/aea55ff520da2fad85837c4ce6514222 to your computer and use it in GitHub Desktop.
(def question-search-rules
;;
;; Provides an "or" set of rules for finding a string inside some of
;; the question attributes: doesn't require all of the attributes to
;; be present.
;;
'[[(msearch? ?p ?q) ; search the question/description
[?q :question/description ?text]
[(clojure.string/lower-case ?text) ?s]
[(.contains ^String ?s ?p)]]
;; [(msearch? ?p ?q) ; search the category string
;; [?q :question/category ?text]
;; [(clojure.string/lower-case ?text) ?s]
;; [(.contains ^String ?s ?p)]]
[(mearch? ?p ?q) ; search question aspects
[?q :question/aspects ?a]
[?a :aspect/text ?text]
[(clojure.string/lower-case ?text) ?s]
[(.contains ^String ?s ?p)]]
[(msearch? ?p ?q) ; search choices
[?q :question/choices ?c]
[?c :choice/text ?text]
[(clojure.string/lower-case ?text) ?s]
[(.contains ^String ?s ?p)]]])
(defn question-search
[db {:keys [pattern] :as query}]
(if (s/blank? pattern)
[]
(-> (d/q '{:find [[(pull ?e pat) ...]]
:in [$ % ?p pat]
:where [[?e :question/id]
(msearch? ?p ?e)]}
db
question-search-rules
(s/trim (s/lower-case pattern))
question-spec)
de-refs
de-ns)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment