Use more efficient query form

This commit is contained in:
Erik Johnston 2015-12-11 11:12:57 +00:00
parent 5577a61090
commit 51fb590c0e
1 changed files with 13 additions and 8 deletions

View File

@ -143,7 +143,7 @@ class SearchStore(BackgroundUpdateStore):
search_query = search_query = _parse_query(self.database_engine, search_term) search_query = search_query = _parse_query(self.database_engine, search_term)
args = [search_query] args = []
# Make sure we don't explode because the person is in too many rooms. # Make sure we don't explode because the person is in too many rooms.
# We filter the results below regardless. # We filter the results below regardless.
@ -164,16 +164,19 @@ class SearchStore(BackgroundUpdateStore):
if isinstance(self.database_engine, PostgresEngine): if isinstance(self.database_engine, PostgresEngine):
sql = ( sql = (
"SELECT ts_rank_cd(vector, query) AS rank, room_id, event_id" "SELECT ts_rank_cd(vector, to_tsquery('english', ?)) AS rank,"
" FROM to_tsquery('english', ?) as query, event_search" " room_id, event_id"
" WHERE vector @@ query" " FROM event_search"
" WHERE vector @@ to_tsquery('english', ?)"
) )
args = [search_query, search_query] + args
elif isinstance(self.database_engine, Sqlite3Engine): elif isinstance(self.database_engine, Sqlite3Engine):
sql = ( sql = (
"SELECT rank(matchinfo(event_search)) as rank, room_id, event_id" "SELECT rank(matchinfo(event_search)) as rank, room_id, event_id"
" FROM event_search" " FROM event_search"
" WHERE value MATCH ?" " WHERE value MATCH ?"
) )
args = [search_query] + args
else: else:
# This should be unreachable. # This should be unreachable.
raise Exception("Unrecognized database engine") raise Exception("Unrecognized database engine")
@ -232,7 +235,7 @@ class SearchStore(BackgroundUpdateStore):
search_query = search_query = _parse_query(self.database_engine, search_term) search_query = search_query = _parse_query(self.database_engine, search_term)
args = [search_query] args = []
# Make sure we don't explode because the person is in too many rooms. # Make sure we don't explode because the person is in too many rooms.
# We filter the results below regardless. # We filter the results below regardless.
@ -267,12 +270,13 @@ class SearchStore(BackgroundUpdateStore):
if isinstance(self.database_engine, PostgresEngine): if isinstance(self.database_engine, PostgresEngine):
sql = ( sql = (
"SELECT ts_rank_cd(vector, query) as rank," "SELECT ts_rank_cd(vector, to_tsquery('english', ?)) as rank,"
" origin_server_ts, stream_ordering, room_id, event_id" " origin_server_ts, stream_ordering, room_id, event_id"
" FROM to_tsquery('english', ?) as query, event_search" " FROM event_search"
" NATURAL JOIN events" " NATURAL JOIN events"
" WHERE vector @@ query AND " " WHERE vector @@ to_tsquery('english', ?) AND "
) )
args = [search_term, search_term] + args
elif isinstance(self.database_engine, Sqlite3Engine): elif isinstance(self.database_engine, Sqlite3Engine):
# We use CROSS JOIN here to ensure we use the right indexes. # We use CROSS JOIN here to ensure we use the right indexes.
# https://sqlite.org/optoverview.html#crossjoin # https://sqlite.org/optoverview.html#crossjoin
@ -292,6 +296,7 @@ class SearchStore(BackgroundUpdateStore):
" CROSS JOIN events USING (event_id)" " CROSS JOIN events USING (event_id)"
" WHERE " " WHERE "
) )
args = [search_term] + args
else: else:
# This should be unreachable. # This should be unreachable.
raise Exception("Unrecognized database engine") raise Exception("Unrecognized database engine")