Advanced Query Help - Before Today

Hello, I am trying to construct an advanced query and running into some syntax issues. I’m trying to pull the latest meeting from a contact with the person names [[John Smith]] and the tag [[One on One]]. Here is my current query:

#+BEGIN_QUERY
 {
 :title [ :b "💡 Previous One on One meeting with John Smith" ]
 :query (and [[One on One]] [[John Smith]])
  :result-transform (fn [result] (take 1 result) )
 }
 #+END_QUERY

My issue is that this pulls the CURRENT one and one meeting, the one that I’m currently having. I’d like to pull the LAST one on one. So the match for the same query BEFORE today. Is there a way someone can help me with that? I keep getting errors when trying to add this function.

Thank you so much!

E

Try applying the following clojurescript steps:

  • sort or sort-by the result
  • may or may not need to reverse it
  • assuming that there is only 1 meeting in the future, drop 1
  • take 1 for the most recent meeting

mentaloid, thank you for the response. I don’t know what I’m doing with the closure, but I get an invalid query error whenever I try to add these refinements. I don’t know if I’m applying them in the wrong order or what. Would you be able to walk me through it more?

I really appreciate it!

We don’t know what you are doing either, and we cannot guess either, until you provide your effort in more detail, so as we have something specific to help you with in more detail.

Here is my query so far:

 {:title [ :h3 "💡 Previous One on One meeting with Sarah Langville" ]
  :query [:find (pull ?b [*])
       :in $ ?start ?today ?tag ?tag2
       :where
       [?b :block/page ?p]
       [?p :page/journal-day ?d]
       [(>= ?d ?start)]
       [(< ?d ?today)]
       [?b :block/refs ?ref]
  
       (or [?ref :block/name ?tag]
           [?ref :block/name ?tag2])
  
       ]
  :inputs [:60d-before :today "[[Sarah Langville]]" "[[One on One]]"]}
  )

The first issue I’m having is that if I change from the or operator, to the and operator, I get a weird error about "Missing rules var ‘%’ in :in, but I haven’t changed my variables.

The second issue I get is when I attempt to sort. When I apply a sort of the journal day, like so:

:result-transform (fn [result]
          (sort-by (fn [h]
            (get-in h [:block/page :journal-day])) result)

This seems to give me pretty arbitrary results, when I would expect them to be in some order.

Could you help me understand these two issues?

Thanks!

E

  • Syntax :60d-before is deprecated.
    • Prefer :-60d
  • The and-clause at the top-level seems redundant, as all the top-level conditions are already implicitly inside a big outer and.
    • Maybe this causes the “weird error”.
    • Try instead two pairs at the same level:
      [?b :block/refs ?ref]
      [?ref :block/name ?tag]
      
      [?b :block/refs ?ref2]
      [?ref2 :block/name ?tag2]
      
  • Concerning sorting, check Sorting by journal-day

Okay thanks for that! I think I’ve got the sorting figured out, but my WHERE clause is still acting funny.

I seem to only be getting a single result, and I think I might be misunderstanding what I’m asking for. When I look at block/refs, does it include the child block as well?

The situation I have is something like this:

- Sarah and Evan #[[One on One]] Meeting
   - [[Sarah Langville]]
   - Notes 1
   - Notes 2

and then others like this:

- [[Sarah Langville]]
   tags: [[One on One]]

and neither are showing up. The only blocks I’m getting are when it’s all on one line, like so:

- Some Meeting
   - Mention to [[Sarah Langville]] at our next [[One on One]]

Almost like the AND is only applying to the single block WITHIN the block, not a block group.

Does that make any sense?

Thanks!

  • This is by design, so as to enable filtering-down individual blocks.
    • This is the most common case.
  • To instead treat a block as an extension of its ancestors, should use :block/path-refs.
    • Search for this to see examples of how to use it.

Awesome, awesome, awesome!

I’m almost there. I’ve got the sort working in reverse order, but my take 1 isn’t having any effect. I’m not sure if the take one needs to be appended to a different transform, or if I’m in the wrong order, or what. Here’s how my query stands now:

{
    :title [ :h3 "💡 Previous One on One meeting with Sarah Langville" ]
    :query [:find (pull ?b [*])
    :in $ ?start ?today ?tag ?tag2
    :where
        ;; filter by Journal Day
        [?b :block/page ?p]
        [?p :page/journal-day ?d]
        [(>= ?d ?start)]
        [(< ?d ?today)]
        
        ;; search for Tag and Tag2
        [?b :block/path-refs ?ref] [?ref :block/name ?tag]
        [?b :block/path-refs ?ref2] [?ref2 :block/name ?tag2]
    ]
    ;; reverse sort by day, take the first result
    :result-transform (fn [result]
        (take 1 
            (reverse 
                (sort-by (fn [h] (get h [:block/page :block/journal-day])))
            )
        ) 
    )
    :inputs [:-60d :today "[[Sarah Langville]]" "[[One on One]]"]
} 

Would appreciate any insight into what I’m doing wrong here!

Seems that you forgot to put the result inside sort-by

That is exactly it! Thank you very much for all the help on this query. For anyone else needing to do something similar, here is the result that I ended up with:

{
    :title [ :h3 "💡 Previous One on One meeting with Sarah Langville" ]
    :query [:find (pull ?b [*])
    :in $ ?start ?today ?tag ?tag2
    :where
        ;; filter by **Journal** **Day**
        [?b :block/page ?p]
        [?p :page/journal-day ?d]
        [(>= ?d ?start)]
        [(< ?d ?today)]
        
        ;; search **for** **Tag** **and** **Tag2**
        [?b :block/path-refs ?ref] [?ref :block/name ?tag]
        [?b :block/path-refs ?ref2] [?ref2 :block/name ?tag2]
    ]
    ;; sort by day desc, take the first result
    :result-transform (fn [result]
        (take 1 
                (sort-by (fn [h] (get h [:block/page :block/journal-day])) result)
        )
    )
    :inputs [:-60d :today "[[Sarah Langville]]" "[[One on One]]"]
}