I’m new to logseq and have been trying to learn how to write advanced queries. They seem to be were one gets the greatest power. The following gives me the error below:
#+BEGIN_QUERY
{
:title [:h3 "References"]
:query [:find (pull ?b [*])
:where
[?b :block/properties ?props]
[(get ?props :links) ?links]
[(get ?props :authors) ?authors]
[(clojure.string/includes? ?links "Local library")]
[(clojure.string/includes? ?authors "author-name")]
]
}
#+END_QUERY
It works fine without the author part (and lists all zotero uploads).
Any suggestion as to what the issue is?
Welcome. Most probably property authors::
is not plain text. Try either:
- converting it to string with
[(str ?authors) ?str-authors]
- using
contains?
, e.g. [(contains? ?authors "author-name")]
Thanks! The string conversion works. Pretty subtle. Could not get not the “contains” to work.
The final tweak I’m looking for is for the query to pick the author name from the property “last-name” in the current page. Not working yet.
Thanks again, help appreciated
Should generally use these pieces:
:inputs [:current-page]
:in $ ?cur-name
[?cur :block/name ?cur-name]
[?cur :block/properties ?cur-props]
[(get ?cur-props :last-name) ?author-name]
Wow! Nice. This is the final form (for now). I put this query in the page of authors which includes a property ::last-name
#+BEGIN_QUERY
{
:title [:h3 "References"]
:inputs [:current-page]
:query [:find (pull ?b [*])
:in $ ?cur-name
:where
[?b :block/properties ?props]
[(get ?props :links) ?links]
[(get ?props :authors) ?authors]
[(str ?authors) ?str-authors]
[(clojure.string/includes? ?links "Local library")]
[?cur :block/name ?cur-name]
[?cur :block/properties ?cur-props]
[(get ?cur-props :last-name) ?author-name]
[(clojure.string/includes? ?str-authors ?author-name)]
]
}
#+END_QUERY
P.S. Tagging on the following hoping it would sort the output by property original-title
does not seem to work
:result-transform
(fn [result]
(sort-by (fn [r] (get-in r [:block/original-title]))
result))
Point taken. I did search though, just not successfully. The interaction here helped me progress in understanding the queries significantly. So very grateful!
OK. Yes followed the link and indeed this was clearly pointed out already, sorry.
In case this helps others, here is the current version of the query.
#+BEGIN_QUERY
{
:title [:h3 "Bibliography"]
:inputs [:current-page]
:query [:find (pull ?b [*])
:in $ ?cur-name
:where
[?b :block/properties ?props]
[(get ?props :links) ?links]
[(get ?props :authors) ?authors]
[(str ?authors) ?str-authors]
[(clojure.string/includes? ?links "Local library")]
[?cur :block/name ?cur-name]
[?cur :block/properties ?cur-props]
[(get ?cur-props :last-name) ?author-name]
[(clojure.string/includes? ?str-authors ?author-name)]
]
:result-transform
(fn [result]
(sort-by (fn [r] (get-in r
[:block/properties :original-title]))
result))
}
#+END_QUERY