Created
May 3, 2023 00:02
-
-
Save coyotesqrl/f5d72c7b940b9a02e0fbc667b29d8909 to your computer and use it in GitHub Desktop.
Example XTDB queries with tag inclusions and exclusions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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"])) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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; thequery-manually
fn gives the behavior I expect.