How do I write an advanced query with format A or B

Hello to anyone with a bit of spare time and knowledge.

I’ve been writing advanced queries for quite a while now in Logseq, but I really cannot understand how to something pretty basic.

Problem: I am trying to write a query (specifically for config.edn :default-queries {}) to show me queries that:
• bullet contains markers TODO or NOW
OR
• scheduled today or has deadline today

I am very confused because I am getting errors, although my logic seems correct.

Attempt 1:

#+BEGIN_QUERY
{
:query [:find (pull ?block [*])
        :in $ ?start ?next
        :where
        (or
          (and
            [?block :block/marker ?marker]
            [(contains? #{"NOW" "DOING"} ?marker)])
          (and
            [?block :block/scheduled ?d]
            [(> ?d ?start)]
            [(< ?d ?next)])
          (and
            [?block :block/deadline ?d]
            [(> ?d ?start)]
            [(< ?d ?next)]))]
:inputs [:1d-before :1d-after]
:group-by-page? false
}
#+END_QUERY

Query failed: All clauses ‘or’ must use same set of free vars […]

Attempt 2:

#+BEGIN_QUERY
{
:query [:find (pull ?block [*])
        :in $ ?start ?next
        :where
        (or-join
          [(and
            [?block :block/marker ?marker]
            [(contains? #{"NOW" "DOING"} ?marker)])]
          [(and
            [?block :block/scheduled ?d]
            [(> ?d ?start)]
            [(< ?d ?next)])]
          [(and
            [?block :block/deadline ?d]
            [(> ?d ?start)]
            [(< ?d ?next)])])]
:inputs [:1d-before :1d-after]
:group-by-page? false
}
#+END_QUERY

Query failed: Cannot parse var, expected symbol starting with ?, got: and

I assume that my attempt 2 does not work because perhaps Logseq doesn’t support or-join?

Thanks in advance!

Welcome.

  • These are essentially syntax errors.
    • They don’t care if the logic is perfect or not.
  • Logseq supports or-join clauses, but they need to be passed variables.
    • In your case, probably [?block ?start ?next]
    • Check the syntax here
    • Search the forum for or-join, as it contains plenty of examples.

Also and clauses shouldn’t be inside square brackets.

1 Like

Hey! Thank you so much for this!

  • I removed the square brackets from the [(and ...)]s
  • I searched the forum and found an example of or-join. Turns out that yes, as you said, I needed to add the passed variables (or-join [?block ?start ?next])

This is my final code, fixed:

#+BEGIN_QUERY
{
:query [:find (pull ?block [*])
        :in $ ?start ?next
        :where
        (or-join [?block ?start ?next]
          (and
            [?block :block/marker ?marker]
            [(contains? #{"NOW" "DOING"} ?marker)])
          (and
            (or [?block :block/deadline ?d]
            [?block :block/scheduled ?d])
            [(> ?d ?start)]
            [(< ?d ?next)])
          )]
:inputs [:1d-before :1d-after]
:group-by-page? false
}
#+END_QUERY

Thanks again!