summary refs log tree commit diff
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2015-12-02 13:09:37 +0000
committerErik Johnston <erik@matrix.org>2015-12-02 13:09:37 +0000
commit7dd6e5efca99fc17fa13225ed3d235931da315c9 (patch)
tree7cd199a83a07d6699f02e9a849a7acef185611ec
parentSearch: Add prefix matching support (diff)
downloadsynapse-7dd6e5efca99fc17fa13225ed3d235931da315c9.tar.xz
Remove deuplication. Add comment about regex.
-rw-r--r--synapse/storage/search.py32
1 files changed, 11 insertions, 21 deletions
diff --git a/synapse/storage/search.py b/synapse/storage/search.py
index 0dfd7b9fb5..4738bdd503 100644
--- a/synapse/storage/search.py
+++ b/synapse/storage/search.py
@@ -140,10 +140,7 @@ class SearchStore(BackgroundUpdateStore):
             list of dicts
         """
         clauses = []
-        if isinstance(self.database_engine, PostgresEngine):
-            args = [_postgres_parse_query(search_term)]
-        else:
-            args = [_sqlite_parse_query(search_term)]
+        args = [_parse_query(self.database_engine, search_term)]
 
         # Make sure we don't explode because the person is in too many rooms.
         # We filter the results below regardless.
@@ -230,10 +227,7 @@ class SearchStore(BackgroundUpdateStore):
         """
         clauses = []
 
-        if isinstance(self.database_engine, PostgresEngine):
-            args = [_postgres_parse_query(search_term)]
-        else:
-            args = [_sqlite_parse_query(search_term)]
+        args = [_parse_query(self.database_engine, search_term)]
 
         # Make sure we don't explode because the person is in too many rooms.
         # We filter the results below regardless.
@@ -408,21 +402,17 @@ def _to_postgres_options(options_dict):
     )
 
 
-def _postgres_parse_query(search_term):
+def _parse_query(database_engine, search_term):
     """Takes a plain unicode string from the user and converts it into a form
-    that can be passed to `to_tsquery(..)` postgres func. We use this so that
-    we can add prefix matching, which isn't something `plainto_tsquery` supports.
+    that can be passed to database.
+    We use this so that we can add prefix matching, which isn't something
+    that is supported by default.
     """
-    results = re.findall(r"([\w\-]+)", search_term, re.UNICODE)
 
-    return " & ".join(result + ":*" for result in results)
-
-
-def _sqlite_parse_query(search_term):
-    """Takes a plain unicode string from the user and converts it into a form
-    that can be passed to sqlite `MATCH`. We use this so that we can do prefix
-    matching.
-    """
+    # Pull out the individual words, discarding any non-word characters.
     results = re.findall(r"([\w\-]+)", search_term, re.UNICODE)
 
-    return " & ".join(result + "*" for result in results)
+    if isinstance(database_engine, PostgresEngine):
+        return " & ".join(result + ":*" for result in results)
+    else:
+        return " & ".join(result + "*" for result in results)