diff --git a/README.md b/README.md index 8081818..928c4c9 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,19 @@ Now you can search join queries like this: Entry.query.join(User).whooshee_search('chuck norris').order_by(Entry.id.desc()).all() ``` +### Reindex + +Available since v0.0.9. + +If you lost your whooshee data and you need to recreate it, you can run inside Flask application context: + +``` +from flask.ext.whooshee import Whooshee +w = Whooshee(app) +w.reindex() +``` + + Project is in early alpha stage, documentation and more functionality will be landing soon. Licensed under GPLv2+ diff --git a/flask_whooshee.py b/flask_whooshee.py index bded68c..e8b8250 100644 --- a/flask_whooshee.py +++ b/flask_whooshee.py @@ -254,3 +254,17 @@ class Whooshee(object): def camel_to_snake(self, s): """Constructs nice dir name from class name, e.g. FooBar => foo_bar.""" return self._underscore_re2.sub(r'\1_\2', self._underscore_re1.sub(r'\1_\2', s)).lower() + + def reindex(self): + """ Reindex all data + + This method retrieve all data from registered models and call + update_() function for every instance of such model. + """ + for wh in self.__class__.whoosheers: + writer = wh.index.writer(timeout=self.writer_timeout) + for model in wh.models: + method_name = "update_{0}".format(model.__name__.lower()) + for item in model.query.all(): + getattr(wh, method_name)(writer, item) + writer.commit() diff --git a/test.py b/test.py index aa82ff6..a9f5942 100644 --- a/test.py +++ b/test.py @@ -144,6 +144,20 @@ class BaseTestCases(object): found = self.Entry.query.whooshee_search('foobar').all() assert len(found) == expected_count + def test_reindex(self): + self.db.session.add_all(self.all_inst) + self.db.session.commit() + # generall reindex + self.wh.reindex() + # put stallone directly in db and find him only after reindex + result = self.db.session.execute("INSERT INTO entry VALUES (100, 'rambo', 'pack of one two and three', {0})".format(self.u3.id)) + self.db.session.commit() + found = self.Entry.query.join(self.User).whooshee_search('rambo').all() + self.assertEqual(len(found), 0) + self.wh.reindex() + found = self.Entry.query.join(self.User).whooshee_search('rambo').all() + self.assertEqual(len(found), 1) + # TODO: more :) class TestsWithApp(BaseTestCases.BaseTest):