perf: speed up one-word chat search (#32268)

Chat search checks the whole query and each word in it separately. With a single word those are the same check, so every chat got searched twice. It is now searched once, with the same results. On PostgreSQL with 2164 chats (533 MB of chat data) a one-word search is about 40% faster:

| search | before | after |
|---|---|---|
| proxy | 6.0 s | 3.6 s |
| opnsense | 6.1 s | 3.8 s |
| no match | 5.4 s | 3.0 s |

Refs #32266
This commit is contained in:
Classic298 2026-10-12 21:32:52 +02:00 • committed by GitHub
parent 80ae7f620a
commit 113a64ad1a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2180,7 +2180,8 @@ class ChatTable:
search_text_words = [word for word in search_text_words if not word.startswith(CHAT_SEARCH_FILTER_PREFIXES)]
phrase_query = ' '.join(search_text_words).strip()
search_terms = chat_search_terms(phrase_query)
# A single-word query is already fully covered by the phrase match
search_terms = [term for term in chat_search_terms(phrase_query) if term != phrase_query]
async with get_async_db_context(db) as session:
stmt = select(Chat).filter(Chat.user_id == user_id)