数据日志中的参数化和不区分大小写的查询(数据)
Parameterized and case insensitive query in datalog (datomic)
我想编写一个以名字作为输入参数和 returns 所有匹配记录的查询。匹配应该不区分大小写。例如,我想提取所有名为 Douglass 的人。参数化,但区分大小写,这将是:
(d/q '[:find (pull ?e [*])
:in $ ?par
:where
[?e :person/firstname ?par]
] db "Douglass")
无论大小写如何,以下查询都会产生所有匹配项,但未进行参数化(尚未使用 ?par,而是参数化查询的占位符):
(d/q '[:find (pull ?e [*])
:in $ ?par
:where
[?e :person/firstname ?bfn]
[(re-find (re-pattern "(?i)DouGLASS") ?bfn)]
] db "")
但我无法将它们组合起来。一个 - 可能是天真的 - 方法是抛出 Unable to resolve symbol: ?par in this context
:
(d/q '[:find (pull ?e [*])
:in $ ?par
:where
[?e :person/firstname ?bfn]
[ (re-find (re-pattern (str "(?i)" ?par)) ?bfn)]
] db "Douglass")
那么:如何为这种情况传递名字?
如 here, the issue is that function expressions in Datomic Datalog do not nest. You could break it out like this (using a tested query against the mbrainz 数据库所述)。
(d/q '[:find ?name ?year
:in $ ?match
:where [(str "(?i)" ?match) ?matcher]
[(re-pattern ?matcher) ?regex]
[(re-find ?regex ?aname)]
[?a :artist/name ?aname]
[?r :release/artists ?a]
[?r :release/name ?name]
[?r :release/year ?year]]
(d/db conn) "pink floyd")
Returns:
#{["Point Me at the Sky" 1972]
["Any Colour You Like" 1972]
["The Dark Side of the Moon" 1973]
["Obscured by Clouds" 1972]
...
您也可以编写自己的函数并从 Datalog 中调用它。
我想编写一个以名字作为输入参数和 returns 所有匹配记录的查询。匹配应该不区分大小写。例如,我想提取所有名为 Douglass 的人。参数化,但区分大小写,这将是:
(d/q '[:find (pull ?e [*])
:in $ ?par
:where
[?e :person/firstname ?par]
] db "Douglass")
无论大小写如何,以下查询都会产生所有匹配项,但未进行参数化(尚未使用 ?par,而是参数化查询的占位符):
(d/q '[:find (pull ?e [*])
:in $ ?par
:where
[?e :person/firstname ?bfn]
[(re-find (re-pattern "(?i)DouGLASS") ?bfn)]
] db "")
但我无法将它们组合起来。一个 - 可能是天真的 - 方法是抛出 Unable to resolve symbol: ?par in this context
:
(d/q '[:find (pull ?e [*])
:in $ ?par
:where
[?e :person/firstname ?bfn]
[ (re-find (re-pattern (str "(?i)" ?par)) ?bfn)]
] db "Douglass")
那么:如何为这种情况传递名字?
如 here, the issue is that function expressions in Datomic Datalog do not nest. You could break it out like this (using a tested query against the mbrainz 数据库所述)。
(d/q '[:find ?name ?year
:in $ ?match
:where [(str "(?i)" ?match) ?matcher]
[(re-pattern ?matcher) ?regex]
[(re-find ?regex ?aname)]
[?a :artist/name ?aname]
[?r :release/artists ?a]
[?r :release/name ?name]
[?r :release/year ?year]]
(d/db conn) "pink floyd")
Returns:
#{["Point Me at the Sky" 1972]
["Any Colour You Like" 1972]
["The Dark Side of the Moon" 1973]
["Obscured by Clouds" 1972]
...
您也可以编写自己的函数并从 Datalog 中调用它。