implement reindex()

This commit is contained in:
Miroslav Suchý
2015-10-05 15:46:34 +02:00
parent 9e72c8efbd
commit 9a93b6ed23
3 changed files with 41 additions and 0 deletions
+13
View File
@@ -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+
+14
View File
@@ -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_<model>() 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()
+14
View File
@@ -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):