ddavo
September 25, 2023, 9:28am
1
I want to query all journal blocks that reference the current page.
#+BEGIN_QUERY
{:title "Diario de clases"
:query [:find (pull ?h [*])
:in $ ?current
:where
[?h :block/page ?p]
[?p :page/journal? true]
[?h :block/refs ?current]]
:inputs [:current-page]
:collapsed? false}
#+END_QUERY
This doesn’t get any results, what am I missing? I also tried deleting the “in a journal” constraint and it also doesn’t work
Similar: Query to get a list of pages that refer to the current page?
Should probably replace this:
[?h :block/refs ?current]
with something like this:
[?h :block/refs ?r]
[?r :block/name ?current]
1 Like
ddavo
September 25, 2023, 10:03am
3
It worked, thank you very much!
Final query for those interested:
#+BEGIN_QUERY
{:title [:h2 "Diario de clases"]
:query [:find (pull ?h [*])
:in $ ?current
:where
[?h :block/page ?p]
[?p :page/journal? true]
[?h :block/refs ?r]
[?r :block/name ?current]]
:inputs [:current-page]
:collapsed? false}
#+END_QUERY
But it doesn’t retrieve pages that reference an alias
ddavo
September 25, 2023, 10:19am
4
I’m realizing that aliasing is more difficult than I think, in the meantime I just used an OR clause and manually put the alias. Btw, the name MUST be in lowercase!!
Concerning the case , consider also :block/original-name
ddavo
September 25, 2023, 11:46am
6
I tried getting also the aliases, based on:
Tasks by current page & it’s aliases
This uses a more complex or
statement. This is basically a logical or. it is needed here as variable ?a
is only for use in the or
. [?b ?p]
means these two variables are bound with the rest of the query.
More information: Datomic Queries and Rules | Datomic
#+BEGIN_QUERY
{:title ["Query by page & alias"]
:query [:find (pull ?b [*])
:in $ ?page
:where
[?b :block/marker "TODO"]
[?p :block/name ?page]
(or-join [?b ?p]
[?b :block/refs ?p]
(and [?p :block/alias ?a] ; this attribute is available only when alias:: has been specified as a page property.
[?b :block/refs ?a])
)
]
:result-transform :sort-by-priority
:table-view? false
:inputs [:current-page] ; alternatively use :query-page to use the page the query is on, rather than the page you are viewing
}
#+END_QUERY
My query is now this:
#+BEGIN_QUERY
{:title [:h2 "Diario de clases con alias"]
:query [:find (pull ?b [*])
:in $ ?current
:where
[?b :block/page ?p]
[?p :page/journal? true]
[?b :block/refs ?r]
[?r :block/name ?current]
(or-join [?b ?r] ; needed because ?a might not unify
[?b :block/refs ?r]
(and [?r :block/alias ?a] [?b :block/refs ?a])
)
]
:inputs [:query-page]
:collapsed? false}
#+END_QUERY
But it doesn’t return the blocks that use the aliased version
It’s because you still have [?b :block/refs ?r]
outside of the or
statement.
ddavo
September 25, 2023, 3:00pm
8
Oh, I see, because I (accidentally) repeated that clause, it was unified outside the or.
Thank you very much! I love your sample queries, they are a great resource to learn this language.
May I ask what’s the difference between this query and the normal reference section at the end of the page in logseq? Thanks
ddavo
September 29, 2023, 6:31am
10
The references section shows ALL references. This query filters out the ones not in a journal, and using a query allows me to customize the display format.
1 Like