Fork me on GitHub
Miguel Rivas14:02:50

Hello there, need some help on querying composite keys in xtdb, my xt/id are composed of say {:attr-1 "foo", :attr-2 :bar} , say query:

{:find [?f]
 :where [[?e :my/type :foo-bar]
         [?e :xt/id ?f]]}
#{... [{:attr-1 "foo" :attr-2 :bar}] ...}
And I need to filter/subquery these by their :attr-1 in :xt/id , I've been adding a where [?f :attr-1 ?g] and pulling ?g in find:
{:find [?g]
 :in [?lookup-attr-1]
 :where [[?e :my/type :foo-bar]
         [?e :xt/id ?f]
         [?g :attr-1 ?lookup-attr-1]]}
but this results in an empty set, there aren't many docs on destructuring that I could find so this became kind of a pain 😕


Did not try, I think last clause needs change. The format is for entity/attribute/value triple, your key is not an entity. You need use a function to check if id has :attr-1 as specified value

👍 1

You can get at the attributes in the map key by binding them - nested maps are not indexed:

XTDB only indexes top-level attributes in a document, so to avoid indexing certain attributes, one can currently move them down into a nested map, as nested values aren't indexed
{:find '[?f first-attr]
 :where '[[?e :my/type :foo-bar]
         [?e :xt/id ?f]
         [(get ?f :attr-1) first-attr]}

👀 1
✔️ 2
Miguel Rivas15:02:58

Ah, this makes sense, will give it a try! thank you 🙂

😎 1