What does this query do?
This query pulls all the undone tasks that reference the current page somewhere in their path OR reside on any-level namespace below it. It shows them all.
How is it meant to be used?
Imagine you have the following hierachy: Work/Company A/Project X. You could paste this query on all three of the pages (Work, Work/Company A, Work/Company A/Project X) and it would show you all tasks related to that page including children pages from that point. It does not matter whether you tag a page inline, you indent under it or you put it on the page itself, it collects all those things.
So besides putting it on the pages itself you could do
LATER From everywhere in Logseq, you can tag and it shows up in [[Work]]
[[Work/Company A]]
LATER Task also show up both on pages ‘Company A’ and ‘Work’ when tagged like this
[[Work/Company A/Project 1]]
We have a meeting here
Another bullet point
LATER This task is also found under ‘Work’, ‘Company A’ & ‘Project 1’ pages. As the depth does not matter
limitation:
Logseq doesn’t really handles aliases well. You’ll need to reference the entire namespace in order for the tasks to show up. So if you were to only tag [[Company A]], then it doesn’t show up (also not at page ‘Company A’, as it is really page ‘Work/Company A’. Logseq doesn’t internally link aliases like that. I really think this is a bug rather than a feature. Hope it changes sometime soon. Until then, we’re stuck with referencing the entire namespace always.
Edit: I just saw in the ‘Queries for Task Management’ thread that there is something like an (or-join) which could potentially solve this problem. I’ll leave that note here for now, I’m not gonna look into that myself for now.
Hope it helps anyone, cost me way to much time as ChatGPT doesn’t understand Datalog nearly as well as it does Python :')
Also I’ll grab this query and I’ll fiddle around and make the alias work (hopefully, that’s the plan at least)
One thing I do notice right away.
(or [?b :block/refs ?p]
[?b :block/path-refs ?p])
That or is redundant. :block/refs result is contained in :block/path-refs. So :block/refs is not needed.
Also you define the marker values twice, which is redundant as well. [task ?b #{"TODO" "LATER" "NOW" "DOING"}] isn’t necessary as you have [(contains? #{"NOW" "LATER" "TODO" "DOING"} ?marker)] already.
So ehm… I don’t wanna be mean or anything… butteh… what is the problem with aliases exactly?
Also your complex query can be made waaayyyyy easier… all the complex logic you did was redundant. (I expected as much, but wanted to test it first)
Here are some screenshots to better illustrate. So please test this out as well and let me know any problems you encounter so we can fix them!
At the top is the result from the query you posted and the query below is my simpler version.
It’s a hierarchy of Company/Project/Meeting. I’ve added one task tagged inline for every page, indented for every page and living on the pages itself. So 9 tasks in total (+2 alias-tagged tasks, I’ll come back to those at the end).
For the page: Company/Project/Meeting, our queries are the same:
All the additional namespace complexity was necessary to make the namespace search recursive. If i’m not mistaken, it’ll grab all the tasks of any depth hierarchical children.
I think I added the (or) around path and refs because I was under the impression that it would otherwise miss inline tags, but that doesn’t seem to be the case.
Lastly, as you can see, both our searches are missing the tasks that are indented beneath an alias-tag, which is a pitty. It tends to clutter my experience, but it’s not a biggie.
I did however, clean up my query based on your first two points of feedback. This is cleaner indeed.
@CappieXL
wow, this is great Thanks a lot for sharing this i was looking exactly for this, is it possible to use this query instead of the current page, can I give namespace name and get all tasks under the namespace. if it’s possible, I am planning to use it on my journal page where i can have project-wise tasks can listed on journal page
am a rookie on queries but just found it by trail & error
seems just I have to change the :inputs ["work"] on the query now I can query all tasks from work namespace
EDIT: I thought I came up with a solution for the aliases issue, but it doesn’t quite work. Removed the code I posted, but the essence was this: From @CappieXL’s updated code I added a third item to the (or ...) to capture aliases:
Changed the variable name to inputpage as that is the best description of what it is, regardless of whether you use :current-page, :query-page or a hardcoded name.
#+BEGIN_QUERY
; see https://discuss.logseq.com/t/advanced-query-that-pulls-all-reference-and-recursive-name-spaces/21275/8
{:title [:h3 "Recursive Namespace + Alias Tasks" ]
:query [:find (pull ?b [*])
:in $ ?inputpage %
:where
[?b :block/marker ?m]
[(contains? #{"NOW" "LATER" "TODO" "DOING"} ?m)]
(or-join [?b ?inputpage]
(and ; Is de page the task is on part of the namespace of the input page?
[?b :block/page ?p]
[?ns :block/name ?inputpage]
(check-ns ?ns ?p)
)
(and ; Does the task refer to the input page in its lineage?
[?p :block/name ?inputpage]
[?b :block/path-refs ?p]
)
(and ; Does the task refer to an alias in it's lineage, either of the input page or a page in its namespace?
(or-join [?p ?inputpage]
(and
[?ns :block/name ?inputpage]
(check-ns ?ns ?p)
)
[?p :block/name ?inputpage]
)
[?p :block/alias ?a]
[?b :block/path-refs ?a]
)
)
]
:result-transform (fn [result]
(let [sorted-result (sort-by (fn [h] (get h :block/marker)) > result)]
(map (fn [m] (assoc m :block/collapsed? true)) sorted-result)))
:group-by-page? false
:breadcrumb-show? false
:inputs [:current-page]
:collapsed? false
:rules [
[(check-ns ?ns ?p)
[?p :block/namespace ?ns]
]
[(check-ns ?ns ?p)
[?p :block/namespace ?t]
(check-ns ?ns ?t)
]
]
}
#+END_QUERY
@CappieXL Sorry for never getting back to it D: (or at least not sooner)
Thanks for this! Works perfectly. I think I understand how the second or-join you added is working. I’m happy that I was getting close with my attempt . Makes sense to change the variable name.
I saw that @CappieXL had changed their strategy. I wonder with this working, how people see the advantages and disadvantages to the two strategies?
Using query-page with this one means the query could be embedded anywhere and capture all tasks for the namespace, which is nice.
is it possible to modify it for journal pages instead of normal pages?
Let’s say i want to pull all blocks (instead of only tasks) that are tagged/referenced with journal pages of last week (any day from last 7 days). These blocks are shown as linked references on their respective journal pages.