diff options
author | Bruno Wagner <bwagner@thoughtworks.com> | 2014-10-16 14:49:24 +0200 |
---|---|---|
committer | Bruno Wagner <bwagner@thoughtworks.com> | 2014-10-16 14:49:24 +0200 |
commit | 898c6049243ac93a5fffdb0e2809de9b454d6719 (patch) | |
tree | 1de47d0c2e7a90fdeebbbd855513dba798c5d938 | |
parent | bc237b646dccf490cbd97c45d0173dde8d3ae988 (diff) |
Created a way to search all mails with no pagination for indexing
-rw-r--r-- | service/pixelated/adapter/mailbox_indexer_listener.py | 2 | ||||
-rw-r--r-- | service/pixelated/adapter/search.py | 33 | ||||
-rw-r--r-- | service/test/unit/adapter/mailbox_indexer_listener_test.py | 2 |
3 files changed, 29 insertions, 8 deletions
diff --git a/service/pixelated/adapter/mailbox_indexer_listener.py b/service/pixelated/adapter/mailbox_indexer_listener.py index a7309118..d8e0f81e 100644 --- a/service/pixelated/adapter/mailbox_indexer_listener.py +++ b/service/pixelated/adapter/mailbox_indexer_listener.py @@ -31,7 +31,7 @@ class MailboxIndexerListener(object): self.querier = soledad_querier def newMessages(self, exists, recent): - indexed_idents = set(self.SEARCH_ENGINE.search('tag:' + self.mailbox_name.lower())) + indexed_idents = set(self.SEARCH_ENGINE.search('tag:' + self.mailbox_name.lower(), all_mails=True)) soledad_idents = self.querier.idents_by_mailbox(self.mailbox_name) missing_idents = soledad_idents.difference(indexed_idents) diff --git a/service/pixelated/adapter/search.py b/service/pixelated/adapter/search.py index eef97c56..cfb5aaa0 100644 --- a/service/pixelated/adapter/search.py +++ b/service/pixelated/adapter/search.py @@ -116,19 +116,40 @@ class SearchEngine(object): results = searcher.search(query, **options) return results - def search(self, query, window=25, page=1): + def search(self, query, window=25, page=1, all_mails=False): + query = self.prepare_query(query) + + if(all_mails): + return self._search_all_mails(query) + else: + return self._paginated_search_mails(query, window, page) + + + def _search_all_mails(self, query): + with self._index.searcher() as searcher: + results = searcher.search(query, limit=None) + return [mail['ident'] for mail in results] + + def _paginated_search_mails(self, query, window, page): page = int(page) if int(page) > 1 else 1 window = int(window) - query = query.replace('\"', '') - query = query.replace('-in:', 'AND NOT tag:') - query = query.replace('in:all', '*') - with self._index.searcher() as searcher: - query = QueryParser('raw', self._index.schema).parse(query) results = searcher.search_page(query, page, pagelen=window) return [mail['ident'] for mail in results] + + def prepare_query(self, query): + query = ( + query + .replace('\"', '') + .replace('-in:', 'AND NOT tag:') + .replace('in:all', '*') + ) + return QueryParser('raw', self._index.schema).parse(query) + + + def remove_from_index(self, mail_id): writer = self._index.writer() try: diff --git a/service/test/unit/adapter/mailbox_indexer_listener_test.py b/service/test/unit/adapter/mailbox_indexer_listener_test.py index 1f28ec7b..291739e0 100644 --- a/service/test/unit/adapter/mailbox_indexer_listener_test.py +++ b/service/test/unit/adapter/mailbox_indexer_listener_test.py @@ -40,7 +40,7 @@ class MailboxListenerTest(unittest.TestCase): def test_reindex_missing_idents(self): search_engine = mock() - when(search_engine).search('tag:inbox').thenReturn(['ident1', 'ident2']) + when(search_engine).search('tag:inbox', all_mails=True).thenReturn(['ident1', 'ident2']) MailboxIndexerListener.SEARCH_ENGINE = search_engine |