more search logic

Josha von Gizycki 5 years ago
parent d5ff324f38
commit cd4c7cdf5a

@ -1,5 +1,8 @@
(ns wanijo.public.db (ns wanijo.public.db
(:require [wanijo.infrastructure.neo4j :as neo4j])) (:require [wanijo.infrastructure.neo4j :as neo4j]
[wanijo.specs :as specs]
[clojure.set :refer [map-invert]]
[clojure.spec.alpha :as spec]))
(def schema-uuids (def schema-uuids
{:coc (or (System/getenv "SCHEMA_COC") {:coc (or (System/getenv "SCHEMA_COC")
@ -191,13 +194,14 @@
:desc (propery-of-attribute i (:comp-desc attribute-uuids))))) :desc (propery-of-attribute i (:comp-desc attribute-uuids)))))
(neo4j/defquery search (neo4j/defquery search
"MATCH (i:instance) "MATCH (i:instance)-[:of]->(s:schema)
OPTIONAL MATCH (i)<-[:of]-(p:property) OPTIONAL MATCH (i)<-[:of]-(p:property)
WITH i, p WITH i, p, s
WHERE toLower(i.name) =~ toLower('(?s).*' + {term} + '.*') WHERE toLower(i.name) =~ toLower('(?s).*' + {term} + '.*')
OR toLower(p.name) =~ toLower('(?s).*' + {term} + '.*') OR toLower(p.name) =~ toLower('(?s).*' + {term} + '.*')
OR toLower(p.value) =~ toLower('(?s).*' + {term} + '.*') OR toLower(p.value) =~ toLower('(?s).*' + {term} + '.*')
RETURN i, p, RETURN i, p, s,
(i)--(:instance {uuid:{coc_uuids}}) AS path,
CASE CASE
WHEN toLower(i.name) =~ toLower('(?s).*' + {term} + '.*') THEN WHEN toLower(i.name) =~ toLower('(?s).*' + {term} + '.*') THEN
i.name i.name
@ -206,14 +210,48 @@
ELSE ELSE
p.value p.value
END AS m") END AS m")
(spec/def ::props
(spec/coll-of
(spec/keys :req-un [::specs/created_at
::specs/updated_at
::specs/uuid
::specs/value])))
(spec/def ::which (set (keys schema-uuids)))
(spec/def ::schema
(spec/keys :req-un [::specs/name
::specs/created_at
::specs/uuid
::which]))
(spec/def ::match (spec/and string?
not-empty))
(spec/def ::search-result-list
(spec/coll-of
(spec/keys :req-un [::specs/name
::specs/created_at
::specs/updated_at
::specs/uuid
::props
::match
::schema])))
(defn search! [term] (defn search! [term]
(->> (neo4j/exec-query! search {:term term}) {:post [(spec/assert ::search-result-list %)]}
(group-by :i) (let [results (->> (neo4j/exec-query! search {:term term
(map #(assoc (key %) :coc_uuids (vals coc-instance-uuids)})
:props (filter some? (group-by :i)
(map :p (val %))) (map #(assoc (key %)
:match (-> (val %) first :m))) :props (filter some?
(sort-by :updated_at #(compare %2 %1)))) (map :p (val %)))
:match (-> (val %) first :m)
:schema (-> (val %) first :s)
:path (-> (val %) first :path)))
(sort-by :updated_at #(compare %2 %1)))]
(map (fn [finding]
(assoc-in finding
[:schema :which]
(get (map-invert schema-uuids)
(-> finding :schema :uuid))))
results)))
(comment (comment
(search! "Builtools") (search! "Builtools")
(search! "Arschitektur")) (search! "Arschitektur")
(search! "Angular"))

@ -241,4 +241,3 @@
[:small (prettify-dt updated_at)]] [:small (prettify-dt updated_at)]]
(when (not= match name) (when (not= match name)
(list [:p [:pre (replace-with-mark match term)]]))])])]])) (list [:p [:pre (replace-with-mark match term)]]))])])]]))

@ -15,3 +15,4 @@
(spec/def ::uuid ::neo4j/uuid) (spec/def ::uuid ::neo4j/uuid)
(spec/def ::user_uuid ::neo4j/uuid) (spec/def ::user_uuid ::neo4j/uuid)
(spec/def ::no-whitespace #(not (re-matches #".*\s.*" %))) (spec/def ::no-whitespace #(not (re-matches #".*\s.*" %)))
(spec/def ::value string?)

Loading…
Cancel
Save