Datalog equivalent of simple query: `{{query [[ML]]}}`

I’m trying to understand advanced queries. So I thought I’d start with something simple. What’s the equivalent of

{{query [[ML]]}}

Also, I wanted to see how to create a query that would not print the “Live query” comment and Query builder UI. So the #+BEGIN_QUERY and #+END_QUERY syntax seems to do that. But it didn’t work with the simple query.

I asked GPT-4, but its several attempts didn’t return any results. The simple one above does.

Questions:

  1. What does this actually do? It appears to return the page named “ML” and pages that reference it, whether with “#ML” or “[[ML]]”. Great.

  2. Is there a way to see the Datalog source for the simple query?

  3. Back to the main question: How to write the equivalent of {{query [[ML]]}} in the advanced syntax?

Thanks!

You might want to check Using dynamic variables in queries - #2 by FlorianF and a plethora of other posts scattered accross this forum, like Is there any documentation on Simple Queries inside Advanced Queries? for example.

1 Like

Welcome. Should read the documentation and the Additional Links. For your specific question, check Example 1 - Find a Tag

1 Like

Thanks @FlorianF and @mentaloid for those links. I find that inserting the simple query into an advanced query, per this link works as expected:

#+BEGIN_QUERY
{ :title "ML"
  :query [[ML]] 
  :collapsed? false
}
#+END_QUERY

It returns a link to the ML page and a list of references that mention it.

This is great as it answers one of my questions above: How to write a simple query but not show the “Live query” annotation and Query Builder UI.

However, the full advanced query from Example 1 - Find a Tag, does not work.

#+BEGIN_QUERY
{ :title "Find tag: ML"
  :query [
    :find (pull ?b [*])
    :where
      [?b :block/ref-pages ?p]
      [?p :block/name "ML"]
  ]
}
#+END_QUERY

It returns “No matched result”. Is the example incorrect? Has something changed?

Yes, it has changed. Check also the schema.

Success! The following returns both the ML page and the references to it, like {{query [[ML]]}}. Note that the matching text must be written in lowercase.

#+BEGIN_QUERY
{ :title "Find tag: ML"
  :query [
      :find (pull ?b [*])
      :where
        (or (and [?b :block/refs ?p] [?p :block/name "ml"])
            (and [?b :block/page ?p] [?p :page/name "ml"])
        )
  ]
}
#+END_QUERY

If there’s a more concise way to write it, please let me know.

Could be shortened like this:

#+BEGIN_QUERY
{ :title "Find tag: ML"
  :query [
      :find (pull ?b [*])
      :where
        [?p :block/name "ml"]
        (or [?b :block/refs ?p]
            [?b :block/page ?p]
        )
  ]
}
#+END_QUERY

Ah, I see it! Thanks!