Add search

This commit is contained in:
Erik Johnston 2017-05-31 14:00:01 +01:00
parent b5db4ed5f6
commit 3b5f22ca40
2 changed files with 38 additions and 0 deletions

View File

@ -40,6 +40,9 @@ class UserDirectoyHandler(object):
self.clock.call_later(0, self.notify_new_event)
def search_users(self, search_term, limit):
return self.store.search_user_dir(search_term, limit)
@defer.inlineCallbacks
def notify_new_event(self):
if self._is_processing:

View File

@ -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,
})