Aggregating pages by path-refs of referent blocks

Not sure if that title makes any sense, but essentially what I want to do is take a bunch of data about various baldur’s gate 3 builds that I’m storing in journal pages, here’s an example so you can see exactly what kinda data I’m working with:

- [[baldur's gate 3]]
	- #builds
		- [[sorlock]] [[sorcerer]] [[warlock]] [youtube link]({{video https://www.youtube.com/watch?v=6qwZLSOJz0c}})
			- [[equipment]]
				- [[Act 1]]
					- [[The Spellsparkler]]
					- [[Hunting Shortbow]]
					- [[Safeguard Shield]]
					- [[Adamantine Shield]]
					- [[Circlet of Blasting]]
					- [[Spidersilk Armour]]
					- [[Missile Snaring Gloves]]
					- [[Daredevil Gloves]]
					- [[Boots of Stormy Clamour]]
					- [[Disintegrating Night Walkers]]
					- [[Amulet of Misty Step]]
					- [[Ring of Absolute Force]]
					- [[Fetish of Callarduran Smoothhands]]
		- #Monk #Rogue Open Hand Tavern Brawler Monk
		  collapsed:: true
			- https://www.youtube.com/watch?v=2SS9mVJveAc
			- General
				- 8 / 17 / 15 / 8 / 16 / 8
				- Feat: Tavern Brawler (+1 Con)
				- [[equipment]]
					- [[Act 1]]
						- Haste Helm
						- Armour of Uninhibited Kushigo
						- Gloves of Cinder and Sizzle
						- Sentient Amulet
						- Deathstalker Mantle
		- #[[Barbarian/Berserker]] #[[Rogue/Thief]] #[[Fighter/Champion]] TB Throw Build
			- Tavern Brawler
			- [[equipment]]
				- [[Act 1]]
					- [[Everburn Blade]]
					- [[Returning Pike]]
					- [[Bow of Awareness]]
					- [[Adamantine Scale Mail]]
					- [[Gloves of Kushigo]]
					- [[Speedy Lightfeet]]
					- [[Absolute's Talisman]]
					- [[Amulet of Branding]]
					- [[Ring of Fling]]
					- [[Caustic Band]]
				- [[Act 2]]
					- [[Returning Pike]]
					- [[Lightning Jabber]] x2
					- [[Bow of Awareness]]
					- [[Covert Cowl]]
					- [[Dark Justiciar Helmet]]
					- [[Adamantine Scale Mail]]
					- [[Cloak of Elemental Absorption]]
					- [[Gloves of Kushigo]]
					- [[Speedy Lightfeet]]
					- [[Surgeon Amulet]]
					- [[Ring of Fling]]
					- [[Callous Glow Ring]]
					- [[Risky Ring]]
				- [[Act 3]]
					- [[Nyrulna]]
					- [[Lightning Jabber]] x2
					- [[Dwarven Thrower]]
					- [[The Dead Shot]]
					- [[Sarevok's Horned Helmet]]
					- [[Flame Enamelled Armour]]
					- [[Shade Slayer]]
					- [[Gloves of Kushigo]]
					- [[Speedy Lightfeet]]
					- [[Unflinching Protector]]
					- [[Ring of Fling]]
					- [[Callous Glow Ring]]
					- [[Risky Ring]]
					- [[Viconia's Walking Fortress]]

where each of those blocks under the equipment are page references with a bunch of relevant properties about each item.

And here’s an example of what one of those item pages looks like

link:: https://bg3.wiki/wiki/Amulet_of_the_Devout
slot:: [[Amulets]] 
location:: [[Stormshore Tabernacle]] 
act:: [[Act 3]]
traits:: [[equipment]]

I should also mention that many of those pages are actually empty still, so I don’t want the query to rely on finding pages with specific properties, instead I want the query to help me identify which items haven’t had their properties populated yet. (also, as another aside, if anyone knows of an easy way to integrate external wikis with logseq that would be immensely helpful as well)

I want to write a query that goes through and aggregates all of the items mentioned in any of the builds and creates a table of the page properties of those items so I can sort them by where they’re obtained or by what kind they are.

Here’s what I’ve been able to figure out so far

#+BEGIN_QUERY
{
:title "Relevant items"
:query [
:find (pull ?b [*])
:where
[?b :block/parent ?parent]
[?parent :block/refs ?act]
[?act :block/name "act 1"]
[?b :block/path-refs [:block/name "builds"]]
[?b :block/path-refs [:block/name "equipment"]]
]
:group-by-page? false
}
#+END_QUERY

So this one kinda works but it’s only searching for the blocks themselves, not the page properties of the page the blocks reference, it doesn’t handle duplicates, and its only looking at one act at a time (as an aside, I tried handling this with a collection input pattern as described in Learn Datalog Today! and couldn’t get it to work at all).

Oh, also if there’s an easy way to take the list that is generated and use it to generate todo lists for specific runs where I can check items off that would be amazing as well.

cc @Siferiax tagging you for help with this advanced query :heart:

1 Like

Here’s a solution.

#+BEGIN_QUERY
{:title "Relevant items"
 :query [:find (pull ?p [*]) ;find the item pages
  :where
   [?g :block/name "baldur's gate 3"] ;we are looking for blocks related to this specific game
   [?e :block/name "equipment"] ;of this specific type
   [?b :block/path-refs ?g] ;block references the game
   [?b :block/path-refs ?e] ;and the type
   [?b :block/refs ?p] ;block references the item's page
   (not ;the page is not an act page.
     [?p :block/name ?act]
     [(clojure.string/starts-with? ?act "act")]
   )
 ]
}
#+END_QUERY

Alternatively if you structure that pages are always directly under equipment you wouldn’t need that (not) part. But that is up to you of course.

Result would look like this: