Skip to content

Commit 06e6ab3

Browse files
committed
Fix: Merge conflicts
2 parents 72f3938 + 192c6c9 commit 06e6ab3

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

search/implement_elasticseach.md

+10-7
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ We are going to search data generated by users in an app. We, therefore, need to
8484
![Sample project](/images/elasticsearch/sample_project.png)
8585

8686
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)
8888
- A form that allows users to post something
8989
- A page that displays all the data users posted in the app
9090

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.
9292

9393

9494
## Understanding Elasticsearch
@@ -255,7 +255,7 @@ def add_to_index(index, model):
255255
return
256256
payload = {}
257257
# Add the fields to be searched to a payload after looping through
258-
# the selected fields of the model
258+
# The selected fields of the model
259259
for field in model.__searchable__:
260260
payload[field] = getattr(model, field)
261261
app.elasticsearch.index(index=index, id=model.id, body=payload)
@@ -419,12 +419,13 @@ class SearchableMixin(object):
419419
session._changes = None
420420
```
421421

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.
423423

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`.
425425

426426
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.
427427

428+
428429
```python
429430
class SearchabelMixin(object):
430431
# ...
@@ -435,6 +436,7 @@ class SearchabelMixin(object):
435436
add_to_index(cls.__tablename__, obj)
436437
```
437438

439+
438440
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.
439441

440442
```python
@@ -456,7 +458,8 @@ class Post(SearchableMixin, db.Model):
456458
# ...
457459
```
458460

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+
460463

461464
```python
462465
>>> Post.reindex()
@@ -643,4 +646,4 @@ Pagination is cleverly handled using the number of pages available depending on
643646

644647
You should see this:
645648

646-
![Search results](/images/elasticsearch/search_results.png)
649+
![Search results](/images/elasticsearch/search_results.png)

0 commit comments

Comments
 (0)