From cd4c7cdf5a286a0d3c45418a6039c900716e5735 Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Mon, 16 Mar 2020 11:09:09 +0100 Subject: [PATCH] more search logic --- src/wanijo/public/db.clj | 62 ++++++++++++++++++++++++++++++-------- src/wanijo/public/view.clj | 1 - src/wanijo/specs.clj | 1 + 3 files changed, 51 insertions(+), 13 deletions(-) diff --git a/src/wanijo/public/db.clj b/src/wanijo/public/db.clj index 2f0f68d..4106f2a 100644 --- a/src/wanijo/public/db.clj +++ b/src/wanijo/public/db.clj @@ -1,5 +1,8 @@ (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 {:coc (or (System/getenv "SCHEMA_COC") @@ -191,13 +194,14 @@ :desc (propery-of-attribute i (:comp-desc attribute-uuids))))) (neo4j/defquery search - "MATCH (i:instance) + "MATCH (i:instance)-[:of]->(s:schema) OPTIONAL MATCH (i)<-[:of]-(p:property) - WITH i, p + WITH i, p, s WHERE toLower(i.name) =~ toLower('(?s).*' + {term} + '.*') OR toLower(p.name) =~ 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 WHEN toLower(i.name) =~ toLower('(?s).*' + {term} + '.*') THEN i.name @@ -206,14 +210,48 @@ ELSE p.value 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] - (->> (neo4j/exec-query! search {:term term}) - (group-by :i) - (map #(assoc (key %) - :props (filter some? - (map :p (val %))) - :match (-> (val %) first :m))) - (sort-by :updated_at #(compare %2 %1)))) + {:post [(spec/assert ::search-result-list %)]} + (let [results (->> (neo4j/exec-query! search {:term term + :coc_uuids (vals coc-instance-uuids)}) + (group-by :i) + (map #(assoc (key %) + :props (filter some? + (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 (search! "Builtools") - (search! "Arschitektur")) + (search! "Arschitektur") + (search! "Angular")) diff --git a/src/wanijo/public/view.clj b/src/wanijo/public/view.clj index 96f5f14..91b4222 100644 --- a/src/wanijo/public/view.clj +++ b/src/wanijo/public/view.clj @@ -241,4 +241,3 @@ [:small (prettify-dt updated_at)]] (when (not= match name) (list [:p [:pre (replace-with-mark match term)]]))])])]])) - diff --git a/src/wanijo/specs.clj b/src/wanijo/specs.clj index e6faf61..87da3d0 100644 --- a/src/wanijo/specs.clj +++ b/src/wanijo/specs.clj @@ -15,3 +15,4 @@ (spec/def ::uuid ::neo4j/uuid) (spec/def ::user_uuid ::neo4j/uuid) (spec/def ::no-whitespace #(not (re-matches #".*\s.*" %))) +(spec/def ::value string?)