Prepare more complex matching

This commit is contained in:
arne 2025-11-22 00:25:56 +01:00
commit ce56fb65c6
2 changed files with 20 additions and 11 deletions

View file

@ -4,7 +4,7 @@
[clojure.string :as str]
[clojure.pprint :as pprint]
[computersandblues.lodestone.database :as db]
[computersandblues.lodestone.match :refer [query->matching-fn]]
[computersandblues.lodestone.match :refer [query->matching-xform]]
[applied-science.js-interop :as j]))
(def posts-init-state
@ -293,7 +293,7 @@
; posts in the database and returns a result that will be rendered
; by the `post` component below.
xform (comp
(filter (query->matching-fn query))
(query->matching-xform query)
(take per-page)
(map #(js->clj % :keywordize-keys true)))
posts-cursor (db/open-cursor ::db/posts
@ -320,6 +320,10 @@
(let [inputs @search-result-inputs]
(debounced-refresh! inputs)))))
#_(defonce refresh-id-tracker (r/track! (fn []
(let [refresh-ids @(r/cursor state [:section/posts :loading])]
(js/console.log :refresh-id-tracker (count refresh-ids) refresh-ids)))))
(defn search [{:keys [query]}]
[:input {:placeholder "Start typing to search…"
:type "search"

View file

@ -14,12 +14,17 @@
(->> (re-seq #"\"([^\"]+)\"|([^\s]+)" query)
(mapv (fn [[_ a b]] (or a b)))))
(defn query->matching-fn [query]
(let [match? (if query
(partial re-find (->regex query))
(constantly true))]
(fn [post]
(defn text-matcher [token]
(let [match? (partial re-find (->regex token))]
(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 #(when-let [desc (j/get % :description)] (match? desc)) (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 query->matching-xform [query]
(if query
(->> (query->tokens query)
(mapv #(text-matcher %))
(reduce comp))
(filter (constantly true))))