I try to search for a string in more than one field of a database in datahike. So far without success. Here is my best effort approach so far:
(ns my.ns
(:require [clojure.string :as st]
[datahike.api :as dh]))
(def card-db [[1 :card/name "CardA"]
[1 :card/text "Text of CardA mentioning CardB"]
[2 :card/name "CardB"]
[2 :card/text "A mystery"]])
(def rules '[
[
(matches-name ?ent ?fn ?str)
[?ent :card/name ?name]
(?fn ?name ?str)]
[(matches-text ?ent ?fn ?str)
[?ent :card/text ?text]
(?fn ?text ?str)
]
])
(defn my-search [db search-strs]
(dh/q '[:find ?e
:in $ % ?fn [?str ...]
:where
[?e :card/name ?name]
#_[(?fn ?name ?str)] ;; this finds CardB
(or
(matches-name ?e ?fn ?str)
(matches-text ?e ?fn ?str)
) ;; this finds nothing
]
db rules st/includes? search-strs))
#_(count (my-search card-db ["CardB"]))
Expected result: 2
Actual result: 0
The solution needn't use rules as far as I'm concerned. It should just return a match if a string is found in at least one of multiple fields.
I'm using [io.replikativ/datahike "0.1.1"]
DataScript doesn’t support or yet. I think Datahike does neither. But you can emulate it using rules. Try:
(def rules '[[(matches ?ent ?fn ?str)
[?ent :card/name ?name]
(?fn ?name ?str)]
[(matches ?ent ?fn ?str)
[?ent :card/text ?text]
(?fn ?text ?str)]])
(defn my-search [db search-strs]
(dh/q '[:find ?e
:in $ % ?fn [?str ...]
:where [?e :card/name ?name]
(matches ?e ?fn ?str)]
db rules st/includes? search-strs))
Related
Tested on datascript 1.3.0
datoms:
[{:db/id -1 :name "Oliver Smith" :hobbies ["reading" "sports" "music"]}]
tried to run the query below to find who like sports, but the empty set returned.
'[:find ?name
:where
[?p :name ?name]
[?p :hobbies ?hobbies]
[(some #{"sports"} ?hobbies)]]
How to formulate the query correctly to get the expected result below?
#{[Oliver Smith]}
We have to explicitly define the schema with cardinality/many against the attribute of multiple values to solve the problem since schemaless doesn't work here.
(require '[datascript.core :as d])
(def schema {:hobbies {:db/cardinality db.cardinality/many}})
(def conn (d/create-conn schema))
(def datoms [{:db/id -1 :name "Oliver Smith" :hobbies ["reading" "sports" "music"]}])
(d/transact! conn datoms)
(def query '[:find ?name :where [?p :name ?name] [?p :hobbies "sports"]])
(-> (d/q query #conn) println)
My question is quite simple but weird at the same time, I would like to create a PersistentArrayMap avoiding evaluation at the same time that I would like to get the value inside this. What is the best solution? I mean
Imagine that I have this def:
(def queen "catherine the great")
And would like to do something like this (single quote):
'{:queen queen}
for sure the output is
=> {:queen queen}
But i expected do something like this
=> {:queen "catherine the great"}
I know that I can just do it
(array-map :queen queen)
But in my case I would like only evaluate some information cause my map is more complicated, like a datomic query:
'{:find [(pull $ ?c [*])]
:with []
:in [$ ?queen-name]
:where [
[$ ?c :queen/name ?queen-name]
]
:args [queen-name]}
for this case I would like only to evaluate queen-name.
My question is, there's a simple way to do it? Maybe using update?
something like this?
(assoc-in '{:find [(pull $ ?c [*])]
:with []
:in [$ ?queen-name]
:where [
[$ ?c :queen/name ?queen-name]
]
:args []} [:args] ["catherine the great"])
For both examples, you can use syntax-quote and unquote:
user=> (def queen-name "catherine the great")
#'user/queen-name
user=> `{:queen ~queen-name}
{:queen "catherine the great"}
user> {:find '[(pull $ ?c [*])]
:with '[]
:in '[$ ?queen-name]
:where '[
[$ ?c :queen/name ?queen-name]
]
:args `[~queen-name]}
{:find [(pull $ ?c [*])],
:with [],
:in [$ ?queen-name],
:where [[$ ?c :queen/name ?queen-name]],
:args ["catherine the great"]}
If you want to do this in general, you can use tupelo.quote.
(ns demo.core
(:require [tupelo.quote :as q]))
; problem: free symbols a and b are fully-qualified using current ns
`[a b ~(+ 2 3)] => [demo.core/a
demo.core/b
5]
(q/tmpl-fn '[a b (insert (+ 2 3))]) => [a b 5]
(let [a 1 b 2]
(q/tmpl [a b (insert (+ 2 3))])) => [1 2 5]
(is= [1 [2 3 4] 5] (q/tmpl [1 (insert (t/thru 2 4)) 5]))
(is= [1 2 3 4 5] (q/tmpl [1 (splice (t/thru 2 4)) 5]))
For Datomic in particular, you can use query inputs:
(def some-name "John Lennon") ; parameter
;; query
(d/q '[:find ?release-name ; query pattern (quoted)
:in $ ?artist-name
:where [?artist :artist/name ?artist-name]
[?release :release/artists ?artist]
[?release :release/name ?release-name]]
db, some-name ; inputs (not quoted)
)
with result
#{["Power to the People"]
["Unfinished Music No. 2: Life With the Lions"]
["Live Peace in Toronto 1969"]
["Live Jam"]
...}
In the mbrainz sample data, the :artist/type is an enum. Is it possible to pull the value of the enum out of :db/ident and associate it as the value of the :artist/type key using pull syntax?
This is as close as I could get:
[:find (pull ?e [:artist/name {:artist/type [:db/ident]}])
:where
[?e :artist/name "Ray Charles"]
]
;;=> [[{:artist/name "Ray Charles", :artist/type {:db/ident :artist.type/person}}]]
Is it possible to use pull syntax to reshape the result into something like this?
;;=> [[{:artist/name "Ray Charles", :artist/type :artist.type/person}]]
I don't think you can do it using the Pull API the way you are seeking. You may find that it is easier to use the Tupelo Datomic library:
(require '[tupelo.datomic :as td]
'[tupelo.core :refer [spyx]] )
(let [x1 (td/query-scalar :let [$ db-val]
:find [ ?e ]
:where [ [?e :artist/name "Ray Charles"] ] )
x2 (td/entity-map db-val x1)
]
(spyx x1)
(spyx x2)
)
which gives the result:
x1 => 17592186049074
x2 => {:artist/sortName "Charles, Ray", :artist/name "Ray Charles",
:artist/type :artist.type/person, :artist/country :country/US,
:artist/gid #uuid "2ce02909-598b-44ef-a456-151ba0a3bd70",
:artist/startDay 23, :artist/endDay 10, :artist/startYear 1930,
:artist/endMonth 6, :artist/endYear 2004, :artist/startMonth 9,
:artist/gender :artist.gender/male}
So :artist/type is already converted into the :db/ident value and you can just pull it out of the map.
You can use specter on the result that the pull expression returns:
(->> pull-result
(sp/transform (sp/walker :db/ident) :db/ident))
The value of key :db/ident is extracted for every map that has that key.
Was quite easy to do with postwalk
for any pulled :db/ident you can transform with this function
(defn flatten-ident [coll]
(clojure.walk/postwalk
(fn [item] (get item :db/ident item)) coll))
I am trying to find latitudes which fall between two inputs. My query:
(defn- latlngs-within-new-bounds
[db a w]
(d/q '[:find ?lat
:in $ ?a ?w
:where
[ ?e :location/lat ?lat]
[(>= ?lat ?a)]
(not
[(>= ?lat ?w)])]
db a w))
My error:
3 Unhandled com.google.common.util.concurrent.UncheckedExecutionException
java.lang.RuntimeException: Unable to resolve symbol: ?lat in this
context
2 Caused by clojure.lang.Compiler$CompilerException
1 Caused by java.lang.RuntimeException
Unable to resolve symbol: ?lat in this context
Util.java: 221 clojure.lang.Util/runtimeException
Any help with understanding what's wrong with my query would be appreciated. Bonus points if you can also use Datomic rules to factor out the in-bounds part of each half.
Your code seems to work for me with datomic-free 0.9.5173:
(defn- latlngs-within-new-bounds
[db a w]
(d/q '[:find ?lat
:in $ ?a ?w
:where
[ ?e :location/lat ?lat]
[(>= ?lat ?a)]
(not
[(>= ?lat ?w)])]
db a w))
(latlngs-within-new-bounds
[[1 :location/lat 1]
[2 :location/lat 2]
[3 :location/lat 3]
[4 :location/lat 4]
[4 :location/lat 5]]
2 4)
=> #{[2] [3]}
I'm having trouble writing general datomic queries that I consider reusable.
For instance, following up from this post is there a canonical way to grab all idents from a particular datomic partition?, I have the following schema installed
{[63 :account/password]
[64 :account/firstName]
[65 :account/lastName]
[62 :account/username]
[69 :email/prority]
[68 :email/address]}
I want to have a function that shows only the attributes having a given namespace.
This function shows all the attributes in the ":account" namespace
(d/q '[:find ?e ?ident :where
[?e :db/ident ?ident]
[_ :db.install/attribute ?e]
[(.toString ?ident) ?val]
[(.startsWith ?val ":account")]] (d/db *conn*))
;; => [62 :account/username] [63 :account/password]
;; [64 :account/firstName] [65 :account/lastName]
however, when I want to write a function that can take an input, I have to put quotes everywhere in order to make it work.
(defn get-ns-attrs [?ns db]
(d/q [':find '?e '?ident ':where
['?e ':db/ident '?ident]
['_ ':db.install/attribute '?e]
[(list '.toString '?ident) '?val]
[(list '.startsWith '?val (str ":" ?ns))]] db))
(get-ns-attrs "account" (d/db *conn*))
;; => [62 :account/username] [63 :account/password]
;; [64 :account/firstName] [65 :account/lastName]
(get-ns-attrs "email" (d/db *conn*))
;; => [69 :email/prority] [68 :email/address]
Is there a better way to do this?
------ update -------
The full code for this for people to try is here:
(ns schema.start
(:require [datomic.api :as d])
(:use [clojure.pprint :only [pprint]]))
(def *uri* "datomic:mem://login-profile")
(d/create-database *uri*)
(def *conn* (d/connect *uri*))
(defn boolean? [x]
(instance? java.lang.Boolean x))
(defn db-pair [attr kns val f]
(list (keyword (str "db/" (name attr)))
(f val kns)))
(defn db-enum [val kns]
(keyword (str "db." (name kns) "/" (name val))))
(def DB-KNS
{:ident {:required true
:check keyword?}
:type {:required true
:check #{:keyword :string :boolean :long :bigint :float
:double :bigdec :ref :instant :uuid :uri :bytes}
:attr :valueType
:fn db-enum}
:cardinality {:required true
:check #{:one :many}
:fn db-enum}
:unique {:check #{:value :identity}
:fn db-enum}
:doc {:check string?}
:index {:check boolean?}
:fulltext {:check boolean?}
:component? {:check keyword?}
:no-history {:check boolean?}})
(defn process-kns [m kns params res]
(let [val (m kns)]
(cond (nil? val)
(if (:required params)
(throw (Exception. (str "key " kns " is a required key")))
res)
:else
(let [chk (or (:check params) (constantly true))
f (or (:fn params) (fn [x & xs] x))
attr (or (:attr params) kns)]
(if (chk val)
(apply assoc res (db-pair attr kns val f))
(throw (Exception. (str "value " val " failed check"))))))))
(defn schema [m]
(loop [db-kns# DB-KNS
output {}]
(if-let [entry (first db-kns#)]
(recur (rest db-kns#)
(process-kns m (first entry) (second entry) output))
(assoc output
:db.install/_attribute :db.part/db
:db/id (d/tempid :db.part/db)))))
(def account-schema
[(schema {:ident :account/username
:type :string
:cardinality :one
:unique :value
:doc "The username associated with the account"})
(schema {:ident :account/password
:type :string
:cardinality :one
:doc "The password associated with the account"})
(schema {:ident :account/firstName
:type :string
:cardinality :one
:doc "The first name of the user"})
(schema {:ident :account/lastName
:type :string
:cardinality :one
:doc "The first name of the user"})
(schema {:ident :account/otherEmails
:type :ref
:cardinality :many
:doc "Other email address of the user"})
(schema {:ident :account/primaryEmail
:type :ref
:cardinality :one
:doc "The primary email address of the user"})])
(def email-schema
[(schema {:ident :email/address
:type :string
:cardinality :one
:unique :value
:doc "An email address"})
(schema {:ident :email/priority
:type :long
:cardinality :one
:doc "An email address's priority"})])
(d/transact *conn* account-schema)
(d/transact *conn* email-schema)
(defn get-ns-attrs1 [?ns db]
(d/q [':find '?e '?ident ':where
['?e ':db/ident '?ident]
['_ ':db.install/attribute '?e]
[(list '.toString '?ident) '?val]
[(list '.startsWith '?val (str ":" ?ns))]] db))
(defn get-ns-attrs2 [?ns db]
(d/q '[:find ?e ?ident :where
[?e :db/ident ?ident]
[_ :db.install/attribute ?e]
[(.toString ?ident) ?val]
[(.startsWith ?val ~(str ":" ?ns))]] db))
(get-ns-attrs1 "account" (d/db *conn*))
(get-ns-attrs1 "email" (d/db *conn*))
(get-ns-attrs2 "account" (d/db *conn*))
(get-ns-attrs2 "email" (d/db *conn*))
After a bit more reading, I've figured out that the :in keyword is the key to all of this. Examples are given in the 'Advanced Queries' section of the Tutorial - http://docs.datomic.com/tutorial.html.
This is the equivalent query listing all attributes in the :account namespace
(d/q '[:find ?e ?ident ?ns :in $ ?ns :where
[?e :db/ident ?ident]
[_ :db.install/attribute ?e]
[(.toString ?ident) ?val]
[(.startsWith ?val ?ns)]]
(d/db *conn*)
"account")
;; => #<HashSet [[68 :account/firstName], [67 :account/password], [71 :account/primaryEmail], [66 :account/username], [69 :account/lastName], [70 :account/otherEmails]]>
This is the equivalent in a function
(defn get-ns-attrs [_ns db]
(d/q '[:find ?e ?ident :in $ ?ns :where
[?e :db/ident ?ident]
[_ :db.install/attribute ?e]
[(.toString ?ident) ?val]
[(.startsWith ?val ?ns) ]] db (str _ns)))
(get-ns-attrs :account (d/db *conn*))
;; => #<HashSet [[68 :account/firstName], [67 :account/password], [71 :account/primaryEmail], [66 :account/username], [69 :account/lastName], [70 :account/otherEmails]]>
If you require more modularity, the function can be further broken down using % to pass in a set of rules:
(def rule-nsAttrs
'[[nsAttrs ?e ?ident ?ns]
[?e :db/ident ?ident]
[_ :db.install/attribute ?e]
[(.toString ?ident) ?val]
[(.startsWith ?val ?ns)]])
(defn get-ns-attrs [_ns db]
(d/q '[:find ?e ?ident :in $ % ?ns :where
(nsAttrs ?e ?ident ?ns)]
(d/db *conn*)
[rule-nsAttrs]
(str _ns)))
(get-ns-attrs :account (d/db *conn*))
;; => #<HashSet [[68 :account/firstName], [67 :account/password], [71 :account/primaryEmail], [66 :account/username], [69 :account/lastName], [70 :account/otherEmails]]>
can be solved a bit simpler with Clojure's namespace:
(d/q '[:find ?name :in $ ?ns
:where [_ :db.install/attribute ?a]
[?a :db/ident ?name]
[(namespace ?name) ?attr-ns]
[(= ?attr-ns ?ns)]] (d/db *conn*) "account")
which returns:
#{[:account/password]
[:account/firstName]
[:account/lastName]
[:account/username]}