Skip to content

Instantly share code, notes, and snippets.

@coyotesqrl
Created May 3, 2023 00:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coyotesqrl/f5d72c7b940b9a02e0fbc667b29d8909 to your computer and use it in GitHub Desktop.
Save coyotesqrl/f5d72c7b940b9a02e0fbc667b29d8909 to your computer and use it in GitHub Desktop.
Example XTDB queries with tag inclusions and exclusions
(ns scratch
(:require [xtdb.api :as xt])
(:import (java.util UUID)))
(def node (xt/start-node {}))
(defn- put-articles []
(for [lev ["intro" "intermediate" "advanced"]
lan ["java" "clojure" "python"]
:let [url (str "http://foo.bar/" lev "-" lan)
article {:xt/id (UUID/randomUUID)
:article/url url
:article/tags [lev lan]}]]
(xt/submit-tx node [[::xt/put article]])))
(defn query-with-in [inclusions exclusions]
(let [query {:find '[(pull ?article [*])]
:where '[[?article :article/url]
[?article :article/tags in-tags]
(not [?article :article/tags out-tags])]
:in '[[in-tags ...] [out-tags ...]]}]
(xt/q (xt/db node) query inclusions exclusions)))
(defn query-manually [inclusions exclusions]
(let [query {:find '[(pull ?article [*])]
:where '[[?article :article/url]
[?article :article/tags in-tags]]
:in '[[in-tags ...]]}
query (update query :where into (mapv #(cons 'not [['?article :article/tags %]]) exclusions))]
(xt/q (xt/db node) query inclusions)))
(comment
(put-articles)
(query-with-in ["java" "python"] ["intro" "advanced"])
(query-manually ["java" "python"] ["intro" "advanced"]))
@coyotesqrl
Copy link
Author

Simple example of the include/exclude logic I'm trying to implement.

The query-with-in fn is how I'd like to write it; the query-manually fn gives the behavior I expect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment