Repository navigation
Commit c711136
feat(search): OpenSearch relevance with typos, prefixes, phrases and recency (#9864)
## Summary
Stacked on #9863; #9838, #9862 and #9947 are merged. The earlier PRs
fill an OpenSearch message index and add a relay-wide switch with a
shadow mode. Until now the OpenSearch query was a plain word match, so
shadow results could not be judged. This PR adds the relevance model:
typos, partial words, rare words, phrases and recency. Search for users
is unchanged while the flag stays at 0.
**Mapping version 2 (`buzz-messages-v2`).** Message text is indexed
three ways: whole words (with accents folded and compound words split),
stems, and prefixes of 2 to 10 characters. A partial word longer than 10
characters is cut to 10 at query time, so it still matches.
**Query.** It lives in `crates/buzz-search/src/opensearch/relevance.rs`:
- Exact words score highest. A stem match counts 0.4 and a typo match
0.3. Typos keep the first letter fixed. In prefix mode the unfinished
last word also matches with a typo once it has 4 characters; a shorter
one is a single edit from too many other words.
- Newest-first sort matches no typos and computes no scores. For a short
word most typo matches are other words (`log` and `lag`), and with time
as the only order they pushed exact matches out: on a million messages
`log` matched 390 messages exactly and 1,623 with typos. Scoring made a
newest-first search for a common word about six times slower (13 ms
against 2 ms).
- The query is split at `_`, `.` and case changes like the indexed text,
so part of an identifier finds it (`snake_case` of `snake_case_name`,
`OutboxBatch` of `SearchOutboxBatch`, `BUZZ_OPEN` of a longer variable
name). `foo_bar` therefore also finds a message holding `foo` and `bar`
apart.
- Sorted by relevance, a query of three or more words needs most of
them, not all (`2<75%`). Sorted by newest it needs every word, and in
prefix mode the word being typed is always required.
- A query keeps its words up to 64 parts (identifiers split at `_`, `.`
and case changes) and matches typos only up to 16, so a pasted line
stays under OpenSearch's 1,024-clause limit.
- Full-text searches with a `"phrase"`, a `-word` or `or` stay on
Postgres (`unsupported_syntax`).
- The top 1,000 hits are rescored. A phrase or near-phrase match gets a
bonus, and the score is scaled by age: `0.2 + 0.8 × 0.5^(age / 30
days)`. An old message keeps at least a fifth of its score.
- Relevance results end at the rescore window. Paging past it would
repeat hits, so the window is tied at compile time to the largest page
the relay scans (1,000).
**Upgrade.** `ensure()` refuses to start indexing when the alias points
at an index with another mapping version, and searches fail rather than
read it. Rows stay queued. The fix is in
`docs/opensearch-search-deployment.md`: with the flag at 0 and every
relay upgraded, delete the old index, and the relay creates the new one,
clears the backfill progress and rebuilds it. The same doc now covers
edits, the refresh delay and lag after an outage.
Worth knowing:
- The prefix field is the main cost. On 1 million synthetic messages the
index was about 1.6 KB per message, and prefixes were about half of it.
That is why the cap is 10 characters and not 20. OpenSearch's built-in
`index_prefixes` came out larger than edge n-grams.
- Bulk indexing on this mapping was about 3.8k documents per second on a
laptop, roughly a quarter of the rate without the prefix field. Rebuild
time is the number to watch.
- A full rebuild of 1 million messages through the relay ran at about
1.3k, 2.1k, 2.9k and 3.0k documents per second at 1, 2, 4 and 8 workers.
Scaling stops at 4 because the backfill and the drain take turns. Live
messages were indexed with a median lag under 1 second during the
rebuild.
- The index is created with the default single shard.
- The weights are first values. Shadow mode exists to tune them.
### Related issue
N/A. Stacked on #9863; only the top commit is this PR.
### Testing
On a laptop with local Postgres and OpenSearch 2.19.3 (512 MiB heap), 1
million synthetic messages:
- Query p50 was 4, 6 and 10 ms for queries that match 10k, 100k and 790k
messages.
- A real relay with the flag at 1 ran prefix and typo searches; each
wrote a shadow comparison line with OpenSearch hits.
- A leftover version 1 index was refused and rows stayed queued. The
documented rebuild brought the index up at version 2.
- `relevance_pages_tile_one_order_up_to_the_rescore_window` checks that
pages do not repeat or skip hits.
Not measured: Amazon OpenSearch Serverless, production-shaped data, and
relevance quality against real queries.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Signed-off-by: Salman Mohammed <smohammed@squareup.com>
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>1 parent 13bd36a commit c711136
10 files changed
Lines changed: 1567 additions & 192 deletions
File tree
- crates
- buzz-relay/src/handlers
- buzz-search
- src
- opensearch
- tests
- search_suite
- docs
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
885 | 885 | | |
886 | 886 | | |
887 | 887 | | |
888 | | - | |
889 | | - | |
890 | | - | |
891 | | - | |
892 | | - | |
893 | | - | |
894 | | - | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
895 | 892 | | |
896 | | - | |
897 | | - | |
| 893 | + | |
| 894 | + | |
898 | 895 | | |
899 | 896 | | |
900 | 897 | | |
901 | | - | |
902 | | - | |
| 898 | + | |
903 | 899 | | |
904 | | - | |
905 | | - | |
906 | | - | |
907 | | - | |
908 | | - | |
909 | | - | |
910 | | - | |
911 | | - | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
912 | 908 | | |
913 | | - | |
| 909 | + | |
914 | 910 | | |
915 | 911 | | |
916 | 912 | | |
917 | 913 | | |
918 | 914 | | |
919 | 915 | | |
920 | 916 | | |
921 | | - | |
| 917 | + | |
922 | 918 | | |
923 | 919 | | |
924 | | - | |
925 | | - | |
926 | | - | |
927 | | - | |
928 | | - | |
929 | | - | |
930 | | - | |
931 | | - | |
932 | | - | |
933 | | - | |
934 | | - | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
935 | 923 | | |
936 | | - | |
937 | | - | |
938 | | - | |
939 | | - | |
940 | | - | |
941 | | - | |
942 | | - | |
943 | | - | |
944 | | - | |
945 | | - | |
946 | | - | |
947 | | - | |
948 | | - | |
949 | | - | |
950 | | - | |
951 | | - | |
952 | | - | |
953 | | - | |
954 | | - | |
955 | | - | |
956 | | - | |
957 | | - | |
958 | | - | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
| 935 | + | |
| 936 | + | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
| 949 | + | |
959 | 950 | | |
960 | 951 | | |
961 | | - | |
962 | | - | |
963 | | - | |
964 | | - | |
965 | | - | |
966 | | - | |
967 | | - | |
968 | | - | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
969 | 960 | | |
970 | 961 | | |
971 | 962 | | |
| |||
987 | 978 | | |
988 | 979 | | |
989 | 980 | | |
990 | | - | |
991 | | - | |
992 | | - | |
993 | | - | |
| 981 | + | |
994 | 982 | | |
995 | 983 | | |
996 | 984 | | |
| |||
1018 | 1006 | | |
1019 | 1007 | | |
1020 | 1008 | | |
1021 | | - | |
1022 | 1009 | | |
1023 | 1010 | | |
1024 | 1011 | | |
1025 | | - | |
1026 | | - | |
1027 | | - | |
1028 | | - | |
1029 | | - | |
1030 | | - | |
1031 | 1012 | | |
1032 | 1013 | | |
1033 | 1014 | | |
1034 | 1015 | | |
| 1016 | + | |
| 1017 | + | |
1035 | 1018 | | |
1036 | | - | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
1037 | 1032 | | |
1038 | 1033 | | |
1039 | 1034 | | |
1040 | 1035 | | |
1041 | | - | |
| 1036 | + | |
1042 | 1037 | | |
1043 | 1038 | | |
1044 | | - | |
| 1039 | + | |
1045 | 1040 | | |
1046 | 1041 | | |
| 1042 | + | |
1047 | 1043 | | |
1048 | | - | |
1049 | | - | |
1050 | | - | |
1051 | | - | |
| 1044 | + | |
| 1045 | + | |
| 1046 | + | |
1052 | 1047 | | |
1053 | | - | |
| 1048 | + | |
1054 | 1049 | | |
1055 | 1050 | | |
1056 | 1051 | | |
1057 | 1052 | | |
1058 | 1053 | | |
1059 | | - | |
| 1054 | + | |
1060 | 1055 | | |
1061 | 1056 | | |
1062 | 1057 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| |||
162 | 163 | | |
163 | 164 | | |
164 | 165 | | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
165 | 173 | | |
166 | 174 | | |
167 | 175 | | |
168 | 176 | | |
169 | 177 | | |
170 | 178 | | |
171 | | - | |
172 | | - | |
173 | | - | |
174 | | - | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
| 179 | + | |
| 180 | + | |
181 | 181 | | |
182 | 182 | | |
183 | 183 | | |
| |||
204 | 204 | | |
205 | 205 | | |
206 | 206 | | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
207 | 218 | | |
208 | | - | |
| 219 | + | |
209 | 220 | | |
210 | 221 | | |
211 | | - | |
212 | | - | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
213 | 225 | | |
214 | 226 | | |
215 | 227 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
53 | | - | |
54 | | - | |
| 53 | + | |
| 54 | + | |
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
| |||
162 | 162 | | |
163 | 163 | | |
164 | 164 | | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
165 | 179 | | |
166 | 180 | | |
167 | 181 | | |
| |||
0 commit comments