Reference to named page gives "No matched result"

For context:

  • I have a page “DEMO PROJECT”
  • I have two blocks: “TODO Task A #[[DEMO PROJECT]” and “TODO Task B #[[DEMO PROJECT]]”
  • “TODO Task A #[[DEMO PROJECT]” is inline referenced in “DEMO PROJECT” while the other is not.
  • I want the query to show “TODO Task B #[[DEMO PROJECT]]”.

In other words, I need the query to show blocks with the following conditions:

  1. It has a TODO, DOING, or DONE and;
  2. It should have an inline tag #[[DEMO PROJECT]] and;
  3. It should not be inline referenced in the “DEMO PROJECT” page.

So this is the code I got with the help of LLMs:

#+BEGIN_QUERY
{
  :title "Unreferenced Tasks with DEMO PROJECT Tag"
  :query [:find (pull ?b [*])
          :where
          ;; Condition 1: Block must have a marker (TODO, DOING, DONE)
          [?b :block/marker ?marker]
          [(contains? #{"TODO" "DOING" "DONE"} ?marker)]
          ;; Condition 2: Block must contain the inline tag #[[DEMO PROJECT]]
          [?b :block/content ?content]
          [(clojure.string/includes? ?content "[[DEMO PROJECT]]")]
          ;; Condition 3: Block must not be inline-referenced in the DEMO PROJECT page
          (not [?b :block/refs ?p])
          [?p :block/name "DEMO PROJECT"]]
  :collapsed? false
  :breadcrumb-show? false
}
#+END_QUERY

However, when I applied the query it always gave “No matched results” no matter how many times I “troubleshooted” the query with the LLMs to the point that it already gave a bunch of errors (hence, why I asked here). Can someone help me fix this query? Anyway, thanks for helping.

Edit: Condition 4: I want the query to exclude also tasks written in the DEMO PROJECT page itself.

Ok so I got it to work. However, I don’t know if its validity is a 100% (someone please check).

Here is how I got it to work; I first started with the baseline code that works:

#+BEGIN_QUERY
{
  :query [
    :find (pull ?b [*])
    :where
    [?b :block/marker ?marker]
    [(contains? #{"TODO" "DOING" "DONE"} ?marker)]
    [?b :block/content ?content]
    [(clojure.string/includes? ?content "[[DEMO PROJECT]]")]]
}
#+END_QUERY

Then I told the LLM to rewrite the query so that it looks at the blocks in the DEMO PROJECT page and then if the block already exists, exclude it in the query result.

Here’s the result:

#+BEGIN_QUERY
{
  :query [
    :find (pull ?b [*])
    :where
    ;; Condition 1: Block must have a marker (TODO, DOING, DONE)
    [?b :block/marker ?marker]
    [(contains? #{"TODO" "DOING" "DONE"} ?marker)]
    
    ;; Condition 2: Block must contain the inline tag #[[DEMO PROJECT]]
    [?b :block/content ?content]
    [(clojure.string/includes? ?content "[[DEMO PROJECT]]")]
    
    ;; Condition 3: Exclude blocks that are already listed in the "DEMO PROJECT" page
    (not [?project :block/refs ?b])
  ]
}
#+END_QUERY

And here is the ‘test’ in logseq:

  • These produced lines:
    (not [?b :block/refs ?p])
    [?p :block/original-name "DEMO PROJECT"]
    
    translate as “Block ?b doesn’t reference page ?p DEMO PROJECT”
  • For the desired “Page ?p DEMO PROJECT doesn’t (contain any block ?other that) reference block ?b”, the lines should rather be:
    [?p :block/original-name "DEMO PROJECT"]
    (not
      [?other :block/refs ?b]
      [?other :block/page ?p]
    )
    
  • For Condition 4, should also add:
    (not [?b :block/page ?p])
    
#+BEGIN_QUERY
{
  :title "UNSORTED V2"
  :query [
    :find (pull ?b [*])
    :where
    ;; CONDITION 1: Match blocks with TODO, DOING, or DONE markers
    [?b :block/marker ?marker]
    [(contains? #{"TODO" "DOING" "DONE"} ?marker)]

    ;; CONDITION 2: Match blocks with the inline tag #[[DEMO PROJECT]]
    [?b :block/content ?content]
    [(clojure.string/includes? ?content "[[DEMO PROJECT]]")]

    ;; CONDITION 3: Exclude blocks that are referenced in the "DEMO PROJECT" page
    [?p :block/name "DEMO PROJECT"]
    (not
       [?other :block/refs ?b]
       [?other :block/page ?p]
     )

    ;; CONDITION 4: Exclude blocks written directly in the "DEMO PROJECT" page
    (not [?b :block/page ?p])


  
  ]
  :result-transform (fn [results]
                      (sort-by (fn [h]
                                 (get h :block/priority "Z")) results))
  :collapsed? false
  :breadcrumb-show? false
}
#+END_QUERY

I tried the code but I get no matched result:

I tried to find which line of code gave trouble and here are the results:

  1. I isolated the code so that only conditions 1 and 2 will run and the query returned what I wanted.
  2. I isolated the code so that only conditions 1, 2, and 3 will run and the query gave “no matched results”.
  3. I isolated the code so that only conditions 1, 2, and 4 will run and the query gave “no matched results”.

The ‘test’ showed that there are likely problems with codes pertaining to condition 3 and 4 but idk why.

In contrast to :block/original-name (which accepts mixed case), :block/name expects only lower-case text, i.e. either of:

  • [?p :block/name "demo project"]
  • [?p :block/original-name "DEMO PROJECT"]
1 Like