You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The key things that your application will need to have are:
87
-
- A model or models used to store user data (`Post` model has been used in the sample project)
87
+
- A model or models used to store user data (the `Post` model has been used in the sample project)
88
88
- A form that allows users to post something
89
89
- A page that displays all the data users posted in the app
90
90
91
-
Your navigation bar does not necessarily need to have a search form as seen in the image above. In a subsequent section below, we will learn how to add one such that it will available across all web pages of the app.
91
+
Your navigation bar does not necessarily need to have a search form as seen in the image above. In a subsequent section below, we will learn how to add one such that it will be available across all web pages of the app.
@@ -419,12 +419,13 @@ class SearchableMixin(object):
419
419
session._changes =None
420
420
```
421
421
422
-
Just before a session is committed, the `before_commit()` handler will allow us to check what object has been added, modified, or deleted through `session.new`, `session.dirty` and `session.delete` respectively. These objects are not going to be available anymore after a commit is made. `session._changes` dictionary allows us to save the objects and have them survive a commit since we shall be using them to update the Elasticsearch index.
422
+
Just before a session is committed, the `before_commit()` handler will allow us to check what object has been added, modified or deleted through `session.new`, `session.dirty` and `session.delete` respectively. These objects are not going to be available anymore after a commit is made. `session._changes` dictionary allows us to save the objects and have them survive a commit since we shall be using them to update the Elasticsearch index.
423
423
424
-
As soon as a session has been successfully committed, this is the proper time to make changes on the Elasticsearch side of things using `after_commit()`. We begin by iterating over what has been added, modified, or deleted and make corresponding calls to the indexing functions in the `search` module for objects with `SearchableMixin`.
424
+
As soon as a session has been successfully committed, this is the proper time to make changes on the Elasticsearch side of things using `after_commit()`. We begin by iterating over what has been added, modified or deleted and make corresponding calls to the indexing functions in the `search` module for objects with `SearchableMixin`.
425
425
426
426
We can include a simple `reindex()` helper method that can allow us to refresh an index with all the data on the relational side. You may experience instances where you have to manually update the Elasticsearch index to ensure the latest changes are applied.
427
427
428
+
428
429
```python
429
430
classSearchabelMixin(object):
430
431
# ...
@@ -435,6 +436,7 @@ class SearchabelMixin(object):
435
436
add_to_index(cls.__tablename__, obj)
436
437
```
437
438
439
+
438
440
Given that `reindex` is a class method, you can run `Model.reindex()` to update the Elasticsearch index. Finally, to ensure that SQLAlchemy listens to database change events, we can call the function `db.event.listen()` from SQLAlchemy. This function serves to call `before_commit` and `after_commit` methods before and after each commit respectively.
439
441
440
442
```python
@@ -456,7 +458,8 @@ class Post(SearchableMixin, db.Model):
456
458
# ...
457
459
```
458
460
459
-
With this minor change to the `Post` model, we can maintain a full text search for posts. Let us begin by initializing all the indexes from the posts currently in the database:
461
+
With this minor change to the `Post` model, we can maintain a full text-search for posts. Let us begin by initializing all the index from the posts currently in the database:
462
+
460
463
461
464
```python
462
465
>>> Post.reindex()
@@ -643,4 +646,4 @@ Pagination is cleverly handled using the number of pages available depending on
0 commit comments