2

I want to compose a query that takes a firstname as input parameter and returns all matching records. A match should be case insensitive. As an example I'd like to extract all the people named Douglass. Parameterized, but case sensitive this would be :

(d/q '[:find (pull ?e [*])
       :in $ ?par
       :where
       [?e :person/firstname ?par]
       ] db "Douglass")

The following query will yield all matches regardless the case, but is not parameterized (the ?par is not used yet, but a placeholder for the parameterized query) :

(d/q '[:find (pull ?e [*])
       :in $ ?par
       :where
       [?e :person/firstname ?bfn]
       [(re-find (re-pattern "(?i)DouGLASS") ?bfn)]
       ] db "")

But I'm not able to combine them. A - probably naive - approach is throwing 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")

So : how to pass the firstname for this case?

4

1 回答 1

1

如此处所述,问题在于 Datomic Datalog 中的函数表达式不嵌套。您可以像这样分解它(使用针对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")

回报:

#{["Point Me at the Sky" 1972]
  ["Any Colour You Like" 1972]
  ["The Dark Side of the Moon" 1973]
  ["Obscured by Clouds" 1972]
  ...

您也可以编写自己的函数并从 Datalog 中调用它。

于 2015-09-01T03:36:34.477 回答