Line data Source code
1 4 : (ns matchscore.search.events
2 : "The `events` search index: public events (native and imported from PSA, ESF,
3 : FSU-linked Rankedin) found by name, club or city, newest first among equally
4 : good matches. Unlisted and private events are never indexed.
5 :
6 : Without Meilisearch (or when it fails) the search falls back to a Postgres
7 : match (ILIKE) over name, linked club names and cities, without typo tolerance."
8 : (:require
9 : [clojure.string :as str]
10 : [matchscore.domain.player-follow.search :as pg-search]
11 : [matchscore.search.meilisearch :as meili]
12 : [matchscore.search.translit :as translit]
13 : [next.jdbc :as jdbc]
14 : [next.jdbc.result-set :as rs]
15 : [taoensso.telemere :as t]))
16 :
17 :
18 8 : (def index-name "events")
19 :
20 :
21 20 : (def watched-tables ["events" "clubs" "event_clubs"])
22 :
23 :
24 4 : (def settings
25 48 : {:searchableAttributes ["name" "aliases" "club_names" "cities"]
26 12 : :filterableAttributes ["source" "state"]
27 8 : :sortableAttributes ["datetime_start"]
28 28 : :rankingRules ["words" "typo" "proximity" "attribute" "exactness" "datetime_start:desc"]
29 8 : :displayedAttributes ["*"]
30 12 : :pagination {:maxTotalHits pg-search/max-limit}})
31 :
32 :
33 8 : (def ^:private linked-clubs
34 : "Clubs of event `e`: its own club_id and every event_clubs link."
35 : "(SELECT c.uuid, c.name, NULLIF(trim(c.city), '') AS city
36 : FROM clubs c
37 : WHERE c.uuid = e.club_id
38 : OR c.uuid IN (SELECT ec.club_id FROM event_clubs ec WHERE ec.event_id = e.uuid))")
39 :
40 :
41 4 : (def ^:private columns
42 : "Public events; club_names / cities list every linked club (the event's own club
43 : first)."
44 20 : (str
45 : "SELECT e.uuid::text AS id, e.public_id, e.name, e.source, e.state, e.datetime_start,
46 : (SELECT array_agg(lc.name ORDER BY (lc.uuid = e.club_id) DESC, lc.name)
47 : FILTER (WHERE NULLIF(trim(lc.name), '') IS NOT NULL)
48 : FROM "
49 4 : linked-clubs
50 : " lc) AS club_names,
51 : (SELECT array_agg(lc.city ORDER BY (lc.uuid = e.club_id) DESC, lc.name)
52 : FILTER (WHERE lc.city IS NOT NULL)
53 : FROM "
54 4 : linked-clubs
55 : " lc) AS cities
56 : FROM events e
57 : WHERE e.visibility = 'public' AND NULLIF(trim(e.name), '') IS NOT NULL"))
58 :
59 :
60 4 : (defn- ->vec
61 : [arr]
62 7 : (cond (nil? arr) []
63 7 : (instance? java.sql.Array arr) (vec (.getArray ^java.sql.Array arr))
64 0 : :else (vec arr)))
65 :
66 :
67 4 : (defn- ->instant
68 : ^java.time.Instant [ts]
69 6 : (cond (instance? java.sql.Timestamp ts) (.toInstant ^java.sql.Timestamp ts)
70 0 : (instance? java.time.OffsetDateTime ts) (.toInstant ^java.time.OffsetDateTime ts)
71 0 : (instance? java.time.Instant ts) ts
72 : :else nil))
73 :
74 :
75 4 : (defn- iso
76 : [ts]
77 8 : (some->
78 3 : (->instant ts)
79 1 : str))
80 :
81 :
82 4 : (defn- epoch-seconds
83 : [ts]
84 7 : (some->
85 3 : (->instant ts)
86 : .getEpochSecond))
87 :
88 :
89 4 : (defn row->document
90 : [{:keys [id public-id name source state datetime-start club-names cities]}]
91 4 : (let [club-names (->vec club-names)
92 3 : cities (->vec cities)]
93 14 : {:id id
94 1 : :public_id public-id
95 1 : :name name
96 3 : :aliases (translit/latin-aliases name)
97 1 : :source source
98 1 : :state state
99 7 : :datetime_start (or (epoch-seconds datetime-start) 0)
100 3 : :datetime_start_iso (iso datetime-start)
101 3 : :club_name (first club-names)
102 3 : :city (first cities)
103 1 : :club_names club-names
104 1 : :cities cities}))
105 :
106 :
107 4 : (defn documents
108 : [ds]
109 11 : (mapv row->document (jdbc/execute! ds [columns] {:builder-fn rs/as-unqualified-kebab-maps})))
110 :
111 :
112 4 : (defn- hit->event
113 : [{:keys [id public_id name source state datetime_start_iso club_name city]}]
114 10 : {:id id
115 1 : :public-id public_id
116 1 : :name name
117 1 : :source source
118 1 : :state state
119 1 : :datetime-start datetime_start_iso
120 1 : :club-name club_name
121 1 : :city city})
122 :
123 :
124 4 : (def ^:private term-filter
125 : "Per term: matches the event name or any linked club's name or city."
126 16 : (str
127 : " AND (e.name ILIKE '%' || ? || '%' OR EXISTS (SELECT 1 FROM "
128 4 : linked-clubs
129 : " lc WHERE lc.name ILIKE '%' || ? || '%' OR lc.city ILIKE '%' || ? || '%'))"))
130 :
131 :
132 4 : (defn- postgres-search
133 : [ds terms limit]
134 2 : (mapv
135 4 : (comp hit->event row->document)
136 2 : (jdbc/execute!
137 1 : ds
138 2 : (into
139 4 : [(str
140 1 : columns
141 8 : (apply str (repeat (count terms) term-filter))
142 : " ORDER BY e.datetime_start DESC NULLS LAST, e.uuid LIMIT ?")]
143 12 : (concat (mapcat #(repeat 3 %) terms) [limit]))
144 3 : {:builder-fn rs/as-unqualified-kebab-maps})))
145 :
146 :
147 4 : (defn- still-public
148 : "`hits` that are still public events in Postgres, in their order. The index lags
149 : behind visibility changes and deletions."
150 : [ds hits]
151 4 : (if (empty? hits)
152 1 : hits
153 1 : (let
154 : [ok
155 2 : (into
156 1 : #{}
157 3 : (map :id)
158 2 : (jdbc/execute!
159 1 : ds
160 2 : ["SELECT uuid::text AS id FROM events
161 : WHERE uuid = ANY(?::text[]::uuid[]) AND visibility = 'public'"
162 7 : (into-array String (map :id hits))]
163 3 : {:builder-fn rs/as-unqualified-maps}))]
164 7 : (filterv (comp ok :id) hits))))
165 :
166 :
167 4 : (defn search-events
168 : "Public events matching `raw-query`, best first, at most `limit`. Meilisearch when
169 : `client` is set, else (or when it fails) a Postgres name match."
170 : [ds client raw-query limit]
171 8 : (if-let [terms (pg-search/query-terms raw-query)]
172 4 : (let [limit (pg-search/clamp-limit limit)]
173 4 : (if-not client
174 5 : (postgres-search ds terms limit)
175 1 : (try
176 2 : (still-public
177 1 : ds
178 2 : (mapv
179 1 : hit->event
180 2 : (:hits
181 2 : (meili/search
182 1 : client
183 1 : index-name
184 9 : {:q (str/join " " terms)
185 1 : :limit limit
186 : :matchingStrategy "all"}))))
187 : (catch Exception e
188 0 : (when-not (= "index_not_found" (:code (ex-data e)))
189 0 : (t/log!
190 : :warn
191 0 : {:message "Meilisearch event search failed; searching Postgres"
192 0 : :exception e}))
193 0 : (postgres-search ds terms limit)))))
194 0 : []))
|