Fork me on GitHub
#xtdb
<
2022-07-14
>
Ivan Koz10:07:21

do we index nested maps in documents? {:xt/id "123" :stats {:attr 123}"

refset11:07:04

Hey again, nope, the nested map is only hashed and that hashed gets indexed (so equality can be determined quickly at query time)

refset11:07:35

If you need to index nested things, then you probably want to 'shred' your document into additional entities and join across them

refset11:07:12

In some circumstances you may not need the speed of having the data indexed though, in which case it might be good enough to simply use Clojure functions inside the query: [(get-in ?stats [:attr]) ?attr-v]

clojure-spin 2
Ivan Koz19:07:04

yes that'll work, but i'll go with shredding this time, ty

🙏 1
plins15:07:42

hello everyone, I have this query

(db-core/q
  db
  '{:find [(pull ?user
                 [(:xt/id {:as :id})
                   :user/name
                   :user/email
                   :organization/id])]
    :in [?org-id]
    :where [[?user :organization/id ?org-id]
            [?user :user/status :user.status/active]]}
  "org-id")
and I would like to sort the results by the user name, I tried to use order-by without too much success is it possible to sort results using pull?

jarohen15:07:40

hey @U3QUAHZJ6 👋 you could try returning the name as a separate field in the result (i.e. :find [?username (pull ?user ...)]), and then ordering by ?username?

plins15:07:02

let me try that

plins15:07:55

maybe Im missing something basic but

(db-core/q
   db
   '{:find [?username
            (pull
              ?user
              [(:xt/id {:as :id})
               :auth0/id
               :user/name
               :user/email
               :organization/id])]
     :in [?org-id]
     :where [[?user :organization/id ?org-id]
             [?user :user/status :user.status/active]
             [?username :user/name ?user]]
     :order-by [[?username :asc]]
     }
   org-id)
this now returns me nothing I tried with [?user :user/name ?username] just for the sake of it but it didnt worked

refset16:07:00

I would guess [?user :user/name ?username] is the correct orientation of the triple clause, so should work :thinking_face:

refset16:07:48

Do you get a vector returned? Or a set? If it's a vector, is there no evidence of ordering at all? Is ?username always a ~short string?

plins16:07:27

my repl state was borked somehow, it changes the shape of the result, but it works, thanks =D

🙌 1
richiardiandrea18:07:14

Do I remember incorrectly that pull-ing would not work against map keys? I will also try it out...sorry about the lazy question 😄

refset21:07:40

Hey, it seems not 🙂

(with-open [n (xt/start-node {})]
    (->> [[::xt/put {:xt/id :foo :att {{:map-key true} :bar}}]]
         (xt/submit-tx n)
         (xt/await-tx n))
    (xt/q (xt/db n) '{:find [(pull e [{:map-key true}])]
                      :where [[e :xt/id :foo]]}))
In fairness though XT doesn't really support non-keyword keys in general (but will happily store them in nested values, thanks to Nippy)

richiardiandrea04:07:24

I see thank you for confirming that

🙏 1