Query all blocks that mention certain word in current Journal page

Hello I’m new in Logseq. First, I spent a whole two days learning how to use advanced query, but I found out it is not as simple as I thought cuz I don’t have any background in programming languages. Though I will keep learning how to use it.
But right now I want to figure out how to create a query in the following situation.
I want to query all blocks that mention “practice” in current Journal page to my another page, it sounds easy but it took me 2 days and still no any progress, can anyone helps me figure it out?

I’m assuming you mean something like this. Let me know if this is what you had in mind.

#+BEGIN_QUERY
{:title [:h3 "Word search"]
 :inputs ["search term" :today]
 :query [:find (pull ?b [*])
  :in $ ?input ?day
  :where
   [(str "(?i)" ?input) ?pattern]
   [?p :block/journal-day ?day]
   [?b :block/page ?p]
   [?b :block/content ?c]
   [(re-pattern ?pattern) ?q]
   [(re-find ?q ?c)]
 ]
}
#+END_QUERY

Yes, absolutely that’s it. Athough I still couldn’t understand it, but I’ll try. Thank you so much!!

1 Like

It uses regular expressions so the search is case insensitive.
So we make a search pattern by combining the input with (?i) to make it case insensitive.
Then we want today’s journal page (putting the entity id in ?p)
We want blocks that are put on that page.
And we want to check the content of those blocks.
Then we make a regular expressions pattern with the re-pattern function.
And we check for that pattern with the re-find function.
Both functions are clojure functions. I’ve linked to their clojure docs pages.
Hope that helps!

1 Like

thanks for explain! I have another question that I query all tasks with certain tag, but how could I make it just search for current journal page, I means I want to query those tasks with certain tag just in the current Journal page?

#+BEGIN_QUERY
{:title "Task with tag"
 :query [:find (pull ?b [*])
         :where
         (task ?b #{"NOW" "LATER" "DOING" "TODO"})
         [?b :block/page ?p]
         [?r :block/name "tag"]
         (or [?b :block/path-refs ?r]
             [?p :block/tags ?r])]}
#+END_QUERY

and if it is possible to combine this query with the query you mentioned before?
I want to query all blocks that mention certain word and also the tasks with certain tag in current Journal page in same time.

*update
I realized I have no need to combine the 2 queries because I could just do that with a simple way below, and combining the two queries is too challenging for me right now.

#+BEGIN_QUERY
{:title [:h3 "Task&Word Search"]
:inputs [:today]
 :query [:find (pull ?b [*])
         :in $ ?day
         :where
         (task ?b #{"NOW" "LATER" "DOING" "TODO"})
         [?p :block/journal-day ?day] 
         [?b :block/page ?p]
         [?r :block/name "tag1"]
         (or [?b :block/path-refs ?r]
             [?p :block/tags ?r])]
}
#+END_QUERY
#+BEGIN_QUERY
{:inputs ["tag2" :today]
 :query [:find (pull ?b [*])
         :in $ ?input ?day
         :where
         [(str "(?i)" ?input) ?pattern]
         [?p :block/journal-day ?day]
         [?b :block/page ?p]
         [?b :block/content ?c]
         [(re-pattern ?pattern) ?q]
         [(re-find ?q ?c)]]
}
#+END_QUERY
1 Like