Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit f2783fc

Browse files
authored
Use the simple dictionary in full text search for the user directory (#8959)
* Use the simple dictionary in fts for the user directory * Clarify naming
1 parent c070223 commit f2783fc

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

changelog.d/8959.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a bug causing common English words to not be considered for a user directory search.

synapse/storage/databases/main/user_directory.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,9 @@ def _update_profile_in_user_dir_txn(txn):
396396
sql = """
397397
INSERT INTO user_directory_search(user_id, vector)
398398
VALUES (?,
399-
setweight(to_tsvector('english', ?), 'A')
400-
|| setweight(to_tsvector('english', ?), 'D')
401-
|| setweight(to_tsvector('english', COALESCE(?, '')), 'B')
399+
setweight(to_tsvector('simple', ?), 'A')
400+
|| setweight(to_tsvector('simple', ?), 'D')
401+
|| setweight(to_tsvector('simple', COALESCE(?, '')), 'B')
402402
) ON CONFLICT (user_id) DO UPDATE SET vector=EXCLUDED.vector
403403
"""
404404
txn.execute(
@@ -418,9 +418,9 @@ def _update_profile_in_user_dir_txn(txn):
418418
sql = """
419419
INSERT INTO user_directory_search(user_id, vector)
420420
VALUES (?,
421-
setweight(to_tsvector('english', ?), 'A')
422-
|| setweight(to_tsvector('english', ?), 'D')
423-
|| setweight(to_tsvector('english', COALESCE(?, '')), 'B')
421+
setweight(to_tsvector('simple', ?), 'A')
422+
|| setweight(to_tsvector('simple', ?), 'D')
423+
|| setweight(to_tsvector('simple', COALESCE(?, '')), 'B')
424424
)
425425
"""
426426
txn.execute(
@@ -435,9 +435,9 @@ def _update_profile_in_user_dir_txn(txn):
435435
elif new_entry is False:
436436
sql = """
437437
UPDATE user_directory_search
438-
SET vector = setweight(to_tsvector('english', ?), 'A')
439-
|| setweight(to_tsvector('english', ?), 'D')
440-
|| setweight(to_tsvector('english', COALESCE(?, '')), 'B')
438+
SET vector = setweight(to_tsvector('simple', ?), 'A')
439+
|| setweight(to_tsvector('simple', ?), 'D')
440+
|| setweight(to_tsvector('simple', COALESCE(?, '')), 'B')
441441
WHERE user_id = ?
442442
"""
443443
txn.execute(
@@ -764,7 +764,7 @@ async def search_user_dir(self, user_id, search_term, limit):
764764
INNER JOIN user_directory AS d USING (user_id)
765765
WHERE
766766
%s
767-
AND vector @@ to_tsquery('english', ?)
767+
AND vector @@ to_tsquery('simple', ?)
768768
ORDER BY
769769
(CASE WHEN d.user_id IS NOT NULL THEN 4.0 ELSE 1.0 END)
770770
* (CASE WHEN display_name IS NOT NULL THEN 1.2 ELSE 1.0 END)
@@ -773,13 +773,13 @@ async def search_user_dir(self, user_id, search_term, limit):
773773
3 * ts_rank_cd(
774774
'{0.1, 0.1, 0.9, 1.0}',
775775
vector,
776-
to_tsquery('english', ?),
776+
to_tsquery('simple', ?),
777777
8
778778
)
779779
+ ts_rank_cd(
780780
'{0.1, 0.1, 0.9, 1.0}',
781781
vector,
782-
to_tsquery('english', ?),
782+
to_tsquery('simple', ?),
783783
8
784784
)
785785
)

tests/storage/test_user_directory.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
ALICE = "@alice:a"
2222
BOB = "@bob:b"
2323
BOBBY = "@bobby:a"
24+
# The localpart isn't 'Bela' on purpose so we can test looking up display names.
25+
BELA = "@somenickname:a"
2426

2527

2628
class UserDirectoryStoreTestCase(unittest.TestCase):
@@ -40,6 +42,9 @@ def setUp(self):
4042
yield defer.ensureDeferred(
4143
self.store.update_profile_in_user_dir(BOBBY, "bobby", None)
4244
)
45+
yield defer.ensureDeferred(
46+
self.store.update_profile_in_user_dir(BELA, "Bela", None)
47+
)
4348
yield defer.ensureDeferred(
4449
self.store.add_users_in_public_rooms("!room:id", (ALICE, BOB))
4550
)
@@ -72,3 +77,21 @@ def test_search_user_dir_all_users(self):
7277
)
7378
finally:
7479
self.hs.config.user_directory_search_all_users = False
80+
81+
@defer.inlineCallbacks
82+
def test_search_user_dir_stop_words(self):
83+
"""Tests that a user can look up another user by searching for the start if its
84+
display name even if that name happens to be a common English word that would
85+
usually be ignored in full text searches.
86+
"""
87+
self.hs.config.user_directory_search_all_users = True
88+
try:
89+
r = yield defer.ensureDeferred(self.store.search_user_dir(ALICE, "be", 10))
90+
self.assertFalse(r["limited"])
91+
self.assertEqual(1, len(r["results"]))
92+
self.assertDictEqual(
93+
r["results"][0],
94+
{"user_id": BELA, "display_name": "Bela", "avatar_url": None},
95+
)
96+
finally:
97+
self.hs.config.user_directory_search_all_users = False

0 commit comments

Comments
 (0)