Skip to content
Snippets Groups Projects
Unverified Commit d5af9a22 authored by Chris Truter's avatar Chris Truter Committed by GitHub
Browse files

Basic batching for search index population (#47892)

parent 8486a9f9
No related branches found
No related tags found
No related merge requests found
...@@ -78,24 +78,27 @@ ...@@ -78,24 +78,27 @@
(drop-table! retired-table) (drop-table! retired-table)
true)) true))
(defn- entity->entry [entity]
(-> entity
(select-keys
[:model
:model_rank
:collection_id
:database_id
:table_id
:archived])
(assoc
:model_id (:id entity)
:search_vector [:to_tsvector
[:inline tsv-language]
[:cast
(:searchable_text entity)
:text]])))
(defn update! (defn update!
"Create or update the given search index trny" "Create the given search index entries"
[entity] [entity]
(let [entry (-> entity (let [entry (entity->entry entity)]
(select-keys
[:model
:model_rank
:collection_id
:database_id
:table_id
:archived])
(assoc
:model_id (:id entity)
:search_vector [:to_tsvector
[:inline tsv-language]
[:cast
(:searchable_text entity)
:text]]))]
(when @initialized? (when @initialized?
(t2/insert! active-table entry)) (t2/insert! active-table entry))
(when @reindexing? (when @reindexing?
...@@ -150,6 +153,15 @@ ...@@ -150,6 +153,15 @@
(str/join " | ") (str/join " | ")
maybe-complete))) maybe-complete)))
(defn batch-update!
"Create the given search index entries in bulk"
[entities]
(let [entries (map entity->entry entities)]
(when @initialized?
(t2/insert! active-table entries))
(when @reindexing?
(t2/insert! pending-table entries))))
(defn search-query (defn search-query
"Query fragment for all models corresponding to a query paramter `:search-term`." "Query fragment for all models corresponding to a query paramter `:search-term`."
[search-term] [search-term]
......
...@@ -11,6 +11,8 @@ ...@@ -11,6 +11,8 @@
[toucan2.core :as t2] [toucan2.core :as t2]
[toucan2.realize :as t2.realize])) [toucan2.realize :as t2.realize]))
(def ^:private insert-batch-size 50)
(def ^:private model-rankings (def ^:private model-rankings
(zipmap search.config/models-search-order (range))) (zipmap search.config/models-search-order (range)))
...@@ -40,7 +42,7 @@ ...@@ -40,7 +42,7 @@
(defn- search-items-reducible [] (defn- search-items-reducible []
(-> {:search-string nil (-> {:search-string nil
:models search.config/all-models :models (disj search.config/all-models "indexed-entity")
;; we want to see everything ;; we want to see everything
:is-superuser? true :is-superuser? true
;; irrelevant, as we're acting as a super user ;; irrelevant, as we're acting as a super user
...@@ -62,5 +64,6 @@ ...@@ -62,5 +64,6 @@
(eduction (eduction
(comp (comp
(map t2.realize/realize) (map t2.realize/realize)
(map ->entry))) (map ->entry)
(run! search.index/update!))) (partition-all insert-batch-size)))
(run! search.index/batch-update!)))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment