diff options
author | Erik Johnston <erik@matrix.org> | 2017-05-31 14:00:01 +0100 |
---|---|---|
committer | Erik Johnston <erik@matrix.org> | 2017-05-31 14:00:01 +0100 |
commit | 3b5f22ca40303392e45c8407952ecf3ee15785f6 (patch) | |
tree | a2f4f6efcee5be6e5f2648d390dcad7d7fbb7485 /synapse/storage | |
parent | Update room column when room becomes unpublic (diff) | |
download | synapse-3b5f22ca40303392e45c8407952ecf3ee15785f6.tar.xz |
Add search
Diffstat (limited to 'synapse/storage')
-rw-r--r-- | synapse/storage/user_directory.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/synapse/storage/user_directory.py b/synapse/storage/user_directory.py index d72b93b585..650c49982d 100644 --- a/synapse/storage/user_directory.py +++ b/synapse/storage/user_directory.py @@ -153,3 +153,38 @@ class UserDirectoryStore(SQLBaseStore): return self._execute( "get_current_state_deltas", self.cursor_to_dict, sql, prev_stream_id ) + + @defer.inlineCallbacks + def search_user_dir(self, search_term, limit): + if isinstance(self.database_engine, PostgresEngine): + sql = """ + SELECT user_id, display_name, avatar_url + FROM user_directory + WHERE vector @@ to_tsquery('english', ?) + ORDER BY ts_rank_cd(vector, to_tsquery('english', ?)) DESC + LIMIT ? + """ + args = (search_term, search_term, limit + 1,) + elif isinstance(self.database_engine, Sqlite3Engine): + sql = """ + SELECT user_id, display_name, avatar_url + FROM user_directory + WHERE value MATCH ? + ORDER BY rank(matchinfo(user_directory)) DESC + LIMIT ? + """ + args = (search_term, limit + 1) + else: + # This should be unreachable. + raise Exception("Unrecognized database engine") + + results = yield self._execute( + "search_user_dir", self.cursor_to_dict, sql, *args + ) + + limited = len(results) > limit + + defer.returnValue({ + "limited": limited, + "results": results, + }) |