Prepare more complex matching
This commit is contained in:
parent
aff235b9a9
commit
ce56fb65c6
2 changed files with 20 additions and 11 deletions
|
|
@ -4,7 +4,7 @@
|
||||||
[clojure.string :as str]
|
[clojure.string :as str]
|
||||||
[clojure.pprint :as pprint]
|
[clojure.pprint :as pprint]
|
||||||
[computersandblues.lodestone.database :as db]
|
[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]))
|
[applied-science.js-interop :as j]))
|
||||||
|
|
||||||
(def posts-init-state
|
(def posts-init-state
|
||||||
|
|
@ -293,7 +293,7 @@
|
||||||
; posts in the database and returns a result that will be rendered
|
; posts in the database and returns a result that will be rendered
|
||||||
; by the `post` component below.
|
; by the `post` component below.
|
||||||
xform (comp
|
xform (comp
|
||||||
(filter (query->matching-fn query))
|
(query->matching-xform query)
|
||||||
(take per-page)
|
(take per-page)
|
||||||
(map #(js->clj % :keywordize-keys true)))
|
(map #(js->clj % :keywordize-keys true)))
|
||||||
posts-cursor (db/open-cursor ::db/posts
|
posts-cursor (db/open-cursor ::db/posts
|
||||||
|
|
@ -320,6 +320,10 @@
|
||||||
(let [inputs @search-result-inputs]
|
(let [inputs @search-result-inputs]
|
||||||
(debounced-refresh! 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]}]
|
(defn search [{:keys [query]}]
|
||||||
[:input {:placeholder "Start typing to search…"
|
[:input {:placeholder "Start typing to search…"
|
||||||
:type "search"
|
:type "search"
|
||||||
|
|
|
||||||
|
|
@ -14,12 +14,17 @@
|
||||||
(->> (re-seq #"\"([^\"]+)\"|([^\s]+)" query)
|
(->> (re-seq #"\"([^\"]+)\"|([^\s]+)" query)
|
||||||
(mapv (fn [[_ a b]] (or a b)))))
|
(mapv (fn [[_ a b]] (or a b)))))
|
||||||
|
|
||||||
(defn query->matching-fn [query]
|
(defn text-matcher [token]
|
||||||
(let [match? (if query
|
(let [match? (partial re-find (->regex token))]
|
||||||
(partial re-find (->regex query))
|
(filter (fn [post]
|
||||||
(constantly true))]
|
|
||||||
(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 #(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))))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue