Add more advanced filtering methods
This commit is contained in:
parent
54c57e085a
commit
47d255a3e8
2 changed files with 48 additions and 8 deletions
|
|
@ -294,9 +294,11 @@
|
||||||
:direction :desc})
|
:direction :desc})
|
||||||
refresh-id (now)]
|
refresh-id (now)]
|
||||||
(swap! state update-in [:section/posts :loading] conj refresh-id)
|
(swap! state update-in [:section/posts :loading] conj refresh-id)
|
||||||
|
(js/console.log :start-query (.toISOString (js/Date.)))
|
||||||
(. (promise-all [(db/count ::db/posts)
|
(. (promise-all [(db/count ::db/posts)
|
||||||
(db/transduce-cursor xform posts-cursor)])
|
(db/transduce-cursor xform posts-cursor)])
|
||||||
(then (fn [[total displayed-posts]]
|
(then (fn [[total displayed-posts]]
|
||||||
|
(js/console.log :end-query (.toISOString (js/Date.)))
|
||||||
(swap! state update :section/posts #(-> (assoc % :total total)
|
(swap! state update :section/posts #(-> (assoc % :total total)
|
||||||
(assoc :displayed-posts displayed-posts)
|
(assoc :displayed-posts displayed-posts)
|
||||||
(update :loading disj refresh-id))))))))
|
(update :loading disj refresh-id))))))))
|
||||||
|
|
@ -382,20 +384,25 @@
|
||||||
|
|
||||||
(comment
|
(comment
|
||||||
; query current results
|
; 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
|
; run and time a query on the database
|
||||||
(do
|
(do
|
||||||
(js/console.log :start (.toISOString (js/Date.)))
|
(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)))
|
#_(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)]
|
posts-cursor (db/open-cursor ::db/posts ::db/all)]
|
||||||
(.. (db/transduce-cursor xform conj! (transient #{}) posts-cursor)
|
(.. (db/transduce-cursor xform conj! (transient #{}) posts-cursor)
|
||||||
(then persistent!)
|
(then persistent!)
|
||||||
(then (fn [result]
|
(then (fn [result]
|
||||||
(js/console.log :end (.toISOString (js/Date.)))
|
(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]}]
|
(defn post [{:keys [post]}]
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
(ns computersandblues.lodestone.match
|
(ns computersandblues.lodestone.match
|
||||||
(:require [applied-science.js-interop :as j]))
|
(:require [applied-science.js-interop :as j]
|
||||||
|
[clojure.string :as str]))
|
||||||
|
|
||||||
(defn- ->regex
|
(defn- ->regex
|
||||||
"Does its best to compila a stirng into a regular expression; will fall back
|
"Does its best to compila a stirng into a regular expression; will fall back
|
||||||
|
|
@ -14,17 +15,49 @@
|
||||||
(->> (re-seq #"\"([^\"]+)\"|([^\s]+)" query)
|
(->> (re-seq #"\"([^\"]+)\"|([^\s]+)" query)
|
||||||
(mapv (fn [[_ a b]] (or a b)))))
|
(mapv (fn [[_ a b]] (or a b)))))
|
||||||
|
|
||||||
(defn text-matcher [token]
|
;; TODO document more advanced matching for users
|
||||||
(let [match? (partial re-find (->regex token))]
|
|
||||||
|
(defn text-matcher [term]
|
||||||
|
(let [match? (partial re-find (->regex term))]
|
||||||
(filter (fn [post]
|
(filter (fn [post]
|
||||||
(or (match? (j/get post :content))
|
(or (match? (j/get post :content))
|
||||||
(match? (j/get-in post [:account :acct])) ; search for url + username of poster
|
(match? (j/get-in post [:account :acct])) ; search for url + username of poster
|
||||||
(some #(match? (j/get % :username)) (j/get post :mentions))
|
(some #(match? (j/get % :username)) (j/get post :mentions))
|
||||||
(some #(some-> (j/get % :description) match?) (j/get post :media_attachments))))))) ; search in alt text
|
(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]
|
(defn query->matching-xform [query]
|
||||||
(if query
|
(if query
|
||||||
(->> (query->tokens 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))
|
(reduce comp))
|
||||||
(filter (constantly true))))
|
(filter (constantly true))))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue