Add more advanced filtering methods

This commit is contained in:
arne 2025-11-22 09:22:06 +01:00
commit 47d255a3e8
2 changed files with 48 additions and 8 deletions

View file

@ -294,9 +294,11 @@
:direction :desc})
refresh-id (now)]
(swap! state update-in [:section/posts :loading] conj refresh-id)
(js/console.log :start-query (.toISOString (js/Date.)))
(. (promise-all [(db/count ::db/posts)
(db/transduce-cursor xform posts-cursor)])
(then (fn [[total displayed-posts]]
(js/console.log :end-query (.toISOString (js/Date.)))
(swap! state update :section/posts #(-> (assoc % :total total)
(assoc :displayed-posts displayed-posts)
(update :loading disj refresh-id))))))))
@ -382,20 +384,25 @@
(comment
; query current results
(-> @state :section/posts :displayed-posts)
(->> (-> @state :section/posts :displayed-posts) (map :id) (take 4) (into #{}))
; run and time a query on the database
(do
(js/console.log :start (.toISOString (js/Date.)))
(let [xform (comp (mapcat (j/get :tags))
(let [xform (comp #_(mapcat (j/get :tags))
#_(filter #(= "typescript" (j/get % :name)))
(take 10))
#_(take 10)
(filter (fn [_]
(< (rand) 0.1)) #_(comp #{"115534300206096276"} (j/get :id)))
(map (j/get :id)))
posts-cursor (db/open-cursor ::db/posts ::db/all)]
(.. (db/transduce-cursor xform conj! (transient #{}) posts-cursor)
(then persistent!)
(then (fn [result]
(js/console.log :end (.toISOString (js/Date.)))
(js/console.log :accts result))))))
(let [result (into (sorted-set) result)]
(js/console.log :sorted (.toISOString (js/Date.)))
(js/console.log :accts (count result))))))))
)
(defn post [{:keys [post]}]

View file

@ -1,5 +1,6 @@
(ns computersandblues.lodestone.match
(:require [applied-science.js-interop :as j]))
(:require [applied-science.js-interop :as j]
[clojure.string :as str]))
(defn- ->regex
"Does its best to compila a stirng into a regular expression; will fall back
@ -14,17 +15,49 @@
(->> (re-seq #"\"([^\"]+)\"|([^\s]+)" query)
(mapv (fn [[_ a b]] (or a b)))))
(defn text-matcher [token]
(let [match? (partial re-find (->regex token))]
;; TODO document more advanced matching for users
(defn text-matcher [term]
(let [match? (partial re-find (->regex term))]
(filter (fn [post]
(or (match? (j/get post :content))
(match? (j/get-in post [:account :acct])) ; search for url + username of poster
(some #(match? (j/get % :username)) (j/get post :mentions))
(some #(some-> (j/get % :description) match?) (j/get post :media_attachments))))))) ; search in alt text
(defn body-matcher [term]
(let [match? (partial re-find (->regex term))]
(filter (fn [post]
(or (match? (j/get post :content)) ; search in post body
(some #(some-> (j/get % :description) match?) (j/get post :media_attachments))))))) ; search in descriptions of attachments
(defn from-matcher [term]
(filter (fn [post]
(str/starts-with? (j/get-in post [:account :acct]) term))))
(defn mention-matcher [term]
(filter (fn [post]
(some #(str/starts-with? (j/get % :username) term) (j/get post :mentions)))))
(defn date-before-matcher [term]
(filter #(< (j/get % :created_at) term)))
(defn date-after-matcher [term]
(filter #(> (j/get % :created_at) term)))
(defn token->term [token]
(str/replace token #"^\w+:" ""))
(defn query->matching-xform [query]
(if query
(->> (query->tokens query)
(mapv #(text-matcher %))
(mapv (fn [token]
(cond
(str/starts-with? token "body:") (body-matcher (token->term token))
(str/starts-with? token "from:") (from-matcher (token->term token))
(str/starts-with? token "to:") (mention-matcher (token->term token))
(str/starts-with? token "before:") (date-before-matcher (token->term token))
(str/starts-with? token "after:") (date-after-matcher (token->term token))
:else (text-matcher token))))
(reduce comp))
(filter (constantly true))))