weiming,
@weiming@mapstodon.space avatar
Deebster,
@Deebster@programming.dev avatar

I’m no expert in advanced queries, but just to note that you could make things simpler (well, shorter at least) by using a regex to handle all those starts-with lines.

This selects all pages that don’t start with 0-9 or @:


<span style="color:#323232;">#+BEGIN_QUERY
</span><span style="color:#323232;">{
</span><span style="color:#323232;">  :title [:h2 "Query Results"]
</span><span style="color:#323232;">  :query [
</span><span style="color:#323232;">    :find (pull ?p [</span><span style="font-weight:bold;color:#a71d5d;">*</span><span style="color:#323232;">])
</span><span style="color:#323232;">    :where
</span><span style="color:#323232;">    [?p :page/name ?page_name]
</span><span style="color:#323232;">    [(</span><span style="color:#62a35c;">re-pattern </span><span style="color:#183691;">"^[^0-9@].*"</span><span style="color:#323232;">) ?regex]
</span><span style="color:#323232;">    [(</span><span style="color:#62a35c;">re-matches </span><span style="color:#323232;">?regex ?page_name)]
</span><span style="color:#323232;">  ]
</span><span style="color:#323232;">}
</span><span style="color:#323232;">#+END_QUERY
</span>

You could also extend the regex to handle the “includes _ or -” bit too:


<span style="color:#323232;">    [(</span><span style="color:#62a35c;">re-pattern </span><span style="color:#183691;">"^[^0-9@_-][^_-]*"</span><span style="color:#323232;">) ?regex]
</span>
weiming,
@weiming@mapstodon.space avatar

@Deebster Very interesting. While I was reading, I always thought whether I could use regex. Now here it is. Thanks a lot!

weiming,
@weiming@mapstodon.space avatar

@Deebster I also found this website pretty cool to help me visualize what a regex does. https://regex-vis.com/

Deebster,
@Deebster@programming.dev avatar

@weiming looks good! I am fluent in regex and SQL and I know some Clojure, but these datalog queries are still a bit of mystery to me… that’s the thing I need to visualise!

autokludge, (edited )
@autokludge@programming.dev avatar

I did a lot of tinkering around recently to get an advanced query working for me which ended up being quite tricky to work through. I have Project pages (eg [[12335]] ) and on journal pages I have job note blocks for specific jobs ie #12335 Notes with a :job property so the block title can change if needed. There are multiple levels of notes / subnotes / tasks here and I was attempting to do the below query before I learned or-join, but the query was fragile & failing if tasks weren’t at a specific indent level. I ended up spending a Sunday afternoon deep diving into this stuff to figure this out.


  • As I understand it, the datomic data model is just a HUUGE list of ‘datoms’ which are super basic [element-id|attribute|value] rows for everything.
  • There is some concept of ‘unifying’ which is a variable that appears twice in a :where represents the same value across all clauses.
  • Something like (or-join) allows you to control this unification to selected sub items.
    • My visualization on the query is a graph of conditions
    • The :find (?task) element is absolutely required
    • There are ‘facts’ you want to satisfy [(get ?prop :job) ?job] [(contains? #{“TODO” “WAITING” “DOING”} ?marker)].
    • ?task → ?prop (through or-join) → ?prop must contain :job with value :current-page
    • . ↳ ?marker -> must be one of TODO / WAITING / DOING

<span style="color:#323232;">#+BEGIN_QUERY
</span><span style="color:#323232;">{
</span><span style="color:#323232;">  :title [:h3 "📅 Outstanding Tasks"]
</span><span style="color:#323232;">  :inputs [:current-page]
</span><span style="color:#323232;">  :query [
</span><span style="color:#323232;">    :find (pull ?task [</span><span style="font-weight:bold;color:#a71d5d;">*</span><span style="color:#323232;">])
</span><span style="color:#323232;">    :in $ ?job
</span><span style="color:#323232;">    :where
</span><span style="color:#323232;">      (or-join [?task ?prop]        </span><span style="font-style:italic;color:#969896;">; only care that ?task and ?prop are 'unified' with rest of clauses
</span><span style="color:#323232;">       (</span><span style="color:#62a35c;">and
</span><span style="color:#323232;">        [?task </span><span style="font-weight:bold;color:#a71d5d;">:</span><span style="color:#323232;">block</span><span style="font-weight:bold;color:#a71d5d;">/</span><span style="color:#323232;">page ?page]
</span><span style="color:#323232;">        [?page </span><span style="font-weight:bold;color:#a71d5d;">:</span><span style="color:#323232;">block</span><span style="font-weight:bold;color:#a71d5d;">/</span><span style="color:#323232;">properties-text-values ?prop]    </span><span style="font-style:italic;color:#969896;">; does page have :job property?
</span><span style="color:#323232;">        )
</span><span style="color:#323232;">       (</span><span style="color:#62a35c;">and
</span><span style="color:#323232;">        [?task </span><span style="font-weight:bold;color:#a71d5d;">:</span><span style="color:#323232;">block</span><span style="font-weight:bold;color:#a71d5d;">/</span><span style="color:#323232;">parent ?tp]
</span><span style="color:#323232;">        [?tp </span><span style="font-weight:bold;color:#a71d5d;">:</span><span style="color:#323232;">block</span><span style="font-weight:bold;color:#a71d5d;">/</span><span style="color:#323232;">properties-text-values ?prop]    </span><span style="font-style:italic;color:#969896;">; does task parent have :job property?
</span><span style="color:#323232;">        )
</span><span style="color:#323232;">       (</span><span style="color:#62a35c;">and
</span><span style="color:#323232;">        [?task </span><span style="font-weight:bold;color:#a71d5d;">:</span><span style="color:#323232;">block</span><span style="font-weight:bold;color:#a71d5d;">/</span><span style="color:#323232;">parent ?tp]
</span><span style="color:#323232;">        [?tp </span><span style="font-weight:bold;color:#a71d5d;">:</span><span style="color:#323232;">block</span><span style="font-weight:bold;color:#a71d5d;">/</span><span style="color:#323232;">parent ?tpp]
</span><span style="color:#323232;">        [?tpp </span><span style="font-weight:bold;color:#a71d5d;">:</span><span style="color:#323232;">block</span><span style="font-weight:bold;color:#a71d5d;">/</span><span style="color:#323232;">properties-text-values ?prop]    </span><span style="font-style:italic;color:#969896;">; does task grand-parent contain :job prop?
</span><span style="color:#323232;">        )
</span><span style="color:#323232;">       (</span><span style="color:#62a35c;">and
</span><span style="color:#323232;">        [?task </span><span style="font-weight:bold;color:#a71d5d;">:</span><span style="color:#323232;">block</span><span style="font-weight:bold;color:#a71d5d;">/</span><span style="color:#323232;">parent ?tp]
</span><span style="color:#323232;">        [?tp </span><span style="font-weight:bold;color:#a71d5d;">:</span><span style="color:#323232;">block</span><span style="font-weight:bold;color:#a71d5d;">/</span><span style="color:#323232;">parent ?tpp]
</span><span style="color:#323232;">        [?tpp </span><span style="font-weight:bold;color:#a71d5d;">:</span><span style="color:#323232;">block</span><span style="font-weight:bold;color:#a71d5d;">/</span><span style="color:#323232;">parent ?tppp]
</span><span style="color:#323232;">        [?tppp </span><span style="font-weight:bold;color:#a71d5d;">:</span><span style="color:#323232;">block</span><span style="font-weight:bold;color:#a71d5d;">/</span><span style="color:#323232;">properties-text-values ?prop]    </span><span style="font-style:italic;color:#969896;">; does task great-grand-parent contain :job prop?
</span><span style="color:#323232;">        )
</span><span style="color:#323232;">      )
</span><span style="color:#323232;">      [(</span><span style="color:#62a35c;">get </span><span style="color:#323232;">?prop </span><span style="font-weight:bold;color:#a71d5d;">:</span><span style="color:#323232;">job) ?job]                           </span><span style="font-style:italic;color:#969896;">; does one-of ?props from above contain :job <%current page%>?
</span><span style="color:#323232;">      [?task :block/marker ?marker]                                   
</span><span style="color:#323232;">      [(</span><span style="color:#62a35c;">contains</span><span style="font-weight:bold;color:#a71d5d;">? </span><span style="color:#323232;">#{</span><span style="color:#183691;">"TODO" "WAITING" "DOING"</span><span style="color:#323232;">} ?marker)]  </span><span style="font-style:italic;color:#969896;">; ?task:block/marker must match one of these
</span><span style="color:#323232;">  ]
</span><span style="color:#323232;">  :table-view? false
</span><span style="color:#323232;">  :result-transform (</span><span style="font-weight:bold;color:#a71d5d;">fn </span><span style="color:#323232;">[result]
</span><span style="color:#323232;">      (</span><span style="color:#62a35c;">sort-by </span><span style="color:#323232;">(</span><span style="font-weight:bold;color:#a71d5d;">fn </span><span style="color:#323232;">[m]
</span><span style="color:#323232;">                (</span><span style="color:#62a35c;">get </span><span style="color:#323232;">m </span><span style="font-weight:bold;color:#a71d5d;">:</span><span style="color:#323232;">block</span><span style="font-weight:bold;color:#a71d5d;">/</span><span style="color:#323232;">marker)) </span><span style="font-weight:bold;color:#a71d5d;">> </span><span style="color:#323232;">result
</span><span style="color:#323232;">      )
</span><span style="color:#323232;">      )
</span><span style="color:#323232;">  :breadcrumb-show? false
</span><span style="color:#323232;">  :collapsed? false
</span><span style="color:#323232;">}
</span><span style="color:#323232;">#+END_QUERY
</span>
  • All
  • Subscribed
  • Moderated
  • Favorites
  • Logseq
  • kavyap
  • thenastyranch
  • GTA5RPClips
  • tester
  • InstantRegret
  • DreamBathrooms
  • ngwrru68w68
  • magazineikmin
  • everett
  • Youngstown
  • mdbf
  • slotface
  • rosin
  • cisconetworking
  • megavids
  • khanakhh
  • normalnudes
  • osvaldo12
  • cubers
  • tacticalgear
  • Durango
  • ethstaker
  • modclub
  • anitta
  • provamag3
  • Leos
  • JUstTest
  • lostlight
  • All magazines