Hello, I have a hard time understanding Logseq complex queries and I need one especially. My usecase is that I have all sorts of review meetings that are on random days, 2-3 weeks apart and I would need to find all the events tagged (ex) #tobediscussed that happened between last time the meeting occurred and today. The meetings can be found by namespace or by the text of the block, which is “ProjectX Meeting Nr.Y”. I don’t know if REGEX can be done with Logseq, but a pattern of ‘ProjectX\s+Meeting\s+Nr.\s+\d+’ would catch that. I would take the date of the last occurrence as a variable and do a search for all blocks tagged both #ProjectX and #tobediscussed from that date until today.
Can you query gurus help we with this please?
Should be possible.
Do you have some example data I can play around with? That would help me build the query.
Not sure how can I provide an example. I will try. So I have this journal block 6d ago:
- [[BuildingAssoc]] Meeting Nr. 23
- Has been discussed: X, Y Z;
- Have been implemented:
- A;
- B;
Then, 3d ago I made a journal entry:
- fence down on N due to wind; #tobediscussed #BuildingAssoc
- the gate is damaged but was fixed temporarily;
- the fence boards have been removed and placed aside to avoid injuries;
- we need 3 offers from repair companies for the job;
Today I have a new meeting with the association and I want to get only the #tobediscussed #buildingAssoc tagged blocks that were written since last meeting. I would like to have the results collapsed (these notes can be quite lengthy and can also have child blocks so I would only want the top block as a result) and also no need for breadcrumbs, just one line per result, I can click on it during the meeting to get details.
Is this something you are looking for?
Not quite doable after all?
I have two querries at the moment to help mw with it:
- {{query (and [[BuildingAssoc]] “Meeting Nr.” ) }}, which gives me a list of meetings and I take note of the date of the last meeting;
- {{query (and #tobediscussed #buildingassoc (between [[“Noted Date”]] <%today%>) ) }}
If doing a complex query to have into a variable the last occurence of the “[[BuildingAssoc]] Meeting Nr.” Query and perform the second’s query above with that date as the start date, I would like to optimize a bit my current system:
a) how can I do the first query and only show the last search result?
b) how can I alter the second query to give me the results collapsed?
Thank you.
Ah no sorry I hadn’t gotten back yet.
I had tried asking someone else, but hadn’t heard back yet.
But yeah the problem is that I can’t get the two step part to work.
I can definitely do two queries though!
Here’s what I was at I could combine everything with everything, but I couldn’t grab the result I needed from the result set. My clojure knowledge and expertise is too limited.
So here’s how to do it with 2 queries.
First query
#+BEGIN_QUERY
{:title [:b "last meeting"]
:query [:find (max ?day)
:where
[?a :block/name "buildingassoc"]
[?m :block/refs ?a]
[?m :block/content ?c]
[(clojure.string/includes? ?c "Meeting")]
[?m :block/page ?j]
[?j :block/journal-day ?day]
]
}
#+END_QUERY
Second query
#+BEGIN_QUERY
{:title [:b "past notes"]
:query [:find (pull ?b [*])
:in $ ?parent
:where
[?parent :block/refs ?j]
[?a :block/name "buildingassoc"]
[?d :block/name "tobediscussed"]
[?b :block/refs ?a]
[?b :block/refs ?d]
[?b :block/page ?p]
[?p :block/journal-day ?noteday]
[?j :block/journal-day ?meetday]
[(> ?noteday ?meetday)]
]
:result-transform (fn [r] (sort r))
:inputs [:parent-block]
}
#+END_QUERY
(Remove the result-transform if you need to group your results by page)
I’m trying to use this query to bring first, the last day of my meeting called “Sprint Planning”, but the modified query I made, is not working… any advise?
#+BEGIN_QUERY
{:title [:b “last meeting”]
:query [:find (max ?day)
:where
[?a :block/name “Sprint Planning”]
[?m :block/refs ?a]
[?m :block/page ?j]
[?j :block/journal-day ?day]
]
}
#+END_QUERY
:block/name
is always lower case.
So should be [?a :block/name "sprint planning"]
Alternatively you can use :block/original-name
for the actual page name.
Thanks as always Siferiax!
I’m getting a “Block Render Error”, do you know if this is something related to my query? I tried using the original-name, and also other pages, but I get the same error.
I don’t know if this is a forum thing only or if this is also in your code.
Make sure to use the proper quotes.
So "
and not “
Which is why I got a block render error when copying your code.
Otherwise maybe a refresh or re-index would help.
I’ve seen the occasional block render error myself when switching devices.
Sometimes it also solved itself. Sometimes going to edit mode in the query and then back out solved it.
Hard to say from over here of course
But the query itself (aside from the quotation marks) works for me.
Wow, thank you! I changed the quote marks and now it works as expected.
Thanks for your time.
Hey, I find useul the first querry even if I am not able, yet, to get that dat in a variable and use it in the second query.
There is, though, a hindrance regarding copying it. Whenever I click on it to copy, I am drawn into query editing mode, which means I can only type it in the second query. Is there a way to not click-to-edit a query but let me select text from the result?
And another question would be if there is a way to show the date in a different format, perhaps the same with the general Logseq date that I can choose from Settings > Editor ? I am using “EEEE, dd.MM.yyyy” and the date f the query looks like “20230705”.
Thanks.
No that’s not how you use it. Please refer to the screenshots I have attached in the original post.
You make a bullet with journal page reference.
You can use command /date picker
for it.
Then you make a nested bullet under the one with the journal reference and paste the second query there.
This way you can change the journal page reference in the parent bullet at any time to change the second query output.
I hope that makes sense.
- parent block with reference
- #+BEGIN_QUERY
{:title [:b "past notes"]
:query [:find (pull ?b [*])
:in $ ?parent
:where
[?parent :block/refs ?j]
[?a :block/name "buildingassoc"]
[?d :block/name "tobediscussed"]
[?b :block/refs ?a]
[?b :block/refs ?d]
[?b :block/page ?p]
[?p :block/journal-day ?noteday]
[?j :block/journal-day ?meetday]
[(> ?noteday ?meetday)]
]
:result-transform (fn [r] (sort r))
:inputs [:parent-block]
}
#+END_QUERY
I may have misunderstood the question.
Though I guess my extra explanation may be helpful for someone.
Yes. Unfortunately
Not something I have a solution for.
Not easily… As we return the max of an integer. The page title of the journal date can’t be used for that. It wouldn’t return the proper result.
So the only option for that is to do the conversion ourselves, but that has its limits as we only know about the integer.
So we can only display the numbers differently, we can’t add information like day name.
I dislike giving these answers so here’s a working query that gives you a link to the journal page with its proper name.
#+BEGIN_QUERY
{:title [:b "last meeting"]
:query [:find ?day ?daynm
:keys day daynm
:where
[?a :block/name "buildingassoc"]
[?m :block/refs ?a]
[?m :block/content ?c]
[(clojure.string/includes? ?c "Meeting")]
[?m :block/page ?j]
[?j :block/journal-day ?day]
[?j :block/original-name ?daynm]
]
:result-transform (fn [result]
(take 1 (sort-by (fn [r] (get r :day)) > result) ) )
:view (fn [result] (for [r result] [:a.tag.mr-1 {:href (str "#/page/" (get r :daynm) )} (get r :daynm) ] ) )
}
#+END_QUERY
Didn’t have time to return to this until now.
So this works and I get the Date I am after.
Can’t this be used in a child Query to get the notes between this date and today?
I don’t exactly know what I am doing but this doesn’t throw any results:
Ah no, the block content is actually the query text unfortunately, not the query result.
Otherwise your query would work.
Here’s the block data:
{:block/uuid #uuid "64a50271-caa7-4e59-865d-6c19230c850a",
:block/left {:db/id 1409},
:block/format :markdown,
:block/content
"#+BEGIN_QUERY\n{:title [:b \"last meeting\"]\n :query [:find ?day ?daynm\n :keys day daynm\n :where\n [?a :block/name \"buildingassoc\"]\n [?m :block/refs ?a]\n [?m :block/content ?c]\n [(clojure.string/includes? ?c \"Meeting\")]\n [?m :block/page ?j]\n [?j :block/journal-day ?day]\n [?j :block/original-name ?daynm]\n ]\n :result-transform (fn [result] \n (take 1 (sort-by (fn [r] (get r :day)) > result) ) )\n :view (fn [result] (for [r result] [:a.tag.mr-1 {:href (str \"#/page/\" (get r :daynm) )} (get r :daynm) ] ) )\n}\n#+END_QUERY",
:db/id 1410,
:block/path-refs [{:db/id 1407}],
:block/parent {:db/id 1407},
:block/unordered true,
:block/page {:db/id 1407}}