forked from miguelgrinberg/microblog
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chapter 16: Full-Text Search (v0.16)
- Loading branch information
1 parent
64ed764
commit 25a2c47
Showing
11 changed files
with
177 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from flask import current_app | ||
|
||
|
||
def add_to_index(index, model): | ||
if not current_app.elasticsearch: | ||
return | ||
payload = {} | ||
for field in model.__searchable__: | ||
payload[field] = getattr(model, field) | ||
current_app.elasticsearch.index(index=index, doc_type=index, id=model.id, | ||
body=payload) | ||
|
||
|
||
def remove_from_index(index, model): | ||
if not current_app.elasticsearch: | ||
return | ||
current_app.elasticsearch.delete(index=index, doc_type=index, id=model.id) | ||
|
||
|
||
def query_index(index, query, page, per_page): | ||
if not current_app.elasticsearch: | ||
return [], 0 | ||
search = current_app.elasticsearch.search( | ||
index=index, doc_type=index, | ||
body={'query': {'multi_match': {'query': query, 'fields': ['*']}}, | ||
'from': (page - 1) * per_page, 'size': per_page}) | ||
ids = [int(hit['_id']) for hit in search['hits']['hits']] | ||
return ids, search['hits']['total'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{% extends "base.html" %} | ||
|
||
{% block app_content %} | ||
<h1>{{ _('Search Results') }}</h1> | ||
{% for post in posts %} | ||
{% include '_post.html' %} | ||
{% endfor %} | ||
<nav aria-label="..."> | ||
<ul class="pager"> | ||
<li class="previous{% if not prev_url %} disabled{% endif %}"> | ||
<a href="{{ prev_url or '#' }}"> | ||
<span aria-hidden="true">←</span> {{ _('Previous results') }} | ||
</a> | ||
</li> | ||
<li class="next{% if not next_url %} disabled{% endif %}"> | ||
<a href="{{ next_url or '#' }}"> | ||
{{ _('Next results') }} <span aria-hidden="true">→</span> | ||
</a> | ||
</li> | ||
</ul> | ||
</nav> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,5 @@ class Config(object): | |
ADMINS = ['[email protected]'] | ||
LANGUAGES = ['en', 'es'] | ||
MS_TRANSLATOR_KEY = os.environ.get('MS_TRANSLATOR_KEY') | ||
ELASTICSEARCH_URL = os.environ.get('ELASTICSEARCH_URL') | ||
POSTS_PER_PAGE = 25 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters