Passing parameter values into a Query

Good day. New LogSeq user and loving it!
I created a simple search query that works just fine. However, now I want to pass in the search term from outside the query. Something like this:

searchTermVariable = “Business Architecture”
{{query (and searchTermVariable (between -3y today))}}

How do I do it?

Did you check out Dynamic Variables like <% current page %>?

Otherwise I guess you need to switch to advanced queries, which can receive query inputs via :inputs and have some more block-scoped dynamic variables like :query-page.

As said you can use advanced queries with dynamic input.

Make a block with just the search term and put the following query in a sub block under it.

#+BEGIN_QUERY
{:query [:find (pull ?b [*])
   :in $ ?t ?start ?end
   :where
     [?t :block/content ?term]
     [?b :block/content ?c]
     [(!= ?t ?b)]
     [(clojure.string/includes? ?c ?term)]
     [?b :block/page ?p]
     [?p :block/journal-day ?d]
     [(<= ?start ?d ?end)]
 ]
 :inputs [:parent-block :-3y :today]
}
#+END_QUERY

Result:

2 Likes

This is great! Thank you for your help.

1 Like

Hi, just stumbled upon this and it really is exactly what I am looking for. I’ve been trying to adapt the query to my use case, but I’ve been failing so far.
So basically I am trying to find all blocks that include the parent-block in their tags and also are a specific type.

So in this case I want to find all blocks with the type “bill” which include “technology” (the parent-block) in their tags.

#+BEGIN_QUERY
  {:title ["Bills"]
   :query [:find (pull ?b [*])
   :in $ ?t
   :where
     [?t :block/content ?term]
     [?b :block/properties-text-values ?prop]
     [(get ?prop :tags) ?c]
     [(!= ?t ?b)]
     [(clojure.string/includes? ?c ?term)]
     [(get ?prop :type) ?type]
     [(contains? #{"bill"} ?type)]]
   :inputs [:parent-block]
  }
#+END_QUERY

To make things even smoother, instead of manually searching for “bill” as the :type I would like to search for the name of the page the query is on. So for example if the query is in a sub-block of “technology” on the page “bill” I want it to find all blocks that are tagged with “technology” and with the :type bill. I know there is an input for :query-page, but so far I haven’t really understood how to make it work. Any help would be much appreciated!

  1. Put :query-page inside :inputs
    • e.g. :inputs [:parent-block :query-page]
    • Pay attention to the order.
  2. Put a respective variable name in :in
    • e.g. :in $ ?t ?current
    • Pay attention to the order.
  3. Use the variable (e.g. ?current) as any other.

Thanks so much for your help so far! I followed your steps but so far still failing to get further. With the parent-block (e.g. “technology”) I can use ‘clojure.string/includes?’ to evaluate against :tags, since it’s multiple entries (e.g. “technology, camera, accessories”), but the :query-page (e.g. “Bill”) is exactly the :type I am looking for.

Here’s what I got so far when I put in “Bill” manually:

#+BEGIN_QUERY
  {:title ["Bills"]
   :query [:find (pull ?b [*])
   :in $ ?t ?current
   :where
     [?t :block/content ?term]
     [?b :block/properties-text-values ?prop]
     [(get ?prop :tags) ?c]
     [(clojure.string/includes? ?c ?term)]
     [(get ?prop :type) ?ty]
     [(= "bill" ?ty)]
]
   :inputs [:parent-block :query-page]
  }
#+END_QUERY

When I try to replace bill with the variable for :query-page I get no results. What am I doing wrong? Also, do I have to watch out for capitalization? I’m using it in German so most of my pages are capitalized.

#+BEGIN_QUERY
  {:title ["Bills"]
   :query [:find (pull ?b [*])
   :in $ ?t ?current
   :where
     [?t :block/content ?term]
     [?b :block/properties-text-values ?prop]
     [(get ?prop :tags) ?c]
     [(clojure.string/includes? ?c ?term)]
     [(get ?prop :type) ?ty]
     [(= ?current ?ty)]
]
   :inputs [:parent-block :query-page]
  }
#+END_QUERY
  • Use the first query.
    • i.e. the one with [(= "bill" ?ty)] that works
  • Add ?current in :find like this:
    :query [:find ?current (pull ?b [*])
    
  • Check the results for the exact value of ?current
    • if it is indeed bill or something else

Great, I could figure it out now! Seems to be the Gerrman language playing a role here with its capitalization :wink:

So basically :query-page returns the name of the page in all lowercase, so e.g. “Rechnung” turns into “rechnung”. Since the :type of the blocks I was looking for was “Rechnung” it didn’t return any results. Guess the easiest will be to just use all lowercase in :type from now on.

Or is there a way to transform :type to all lowercase in the query itself?

Thanks again!

  • Unfortunately, clojure.string/lower-case (like many other useful functions) is not currently available.
  • But you could use the name from the :query-page to get the page itself and then use :block/original-name
3 Likes