Skip to content
This repository has been archived by the owner on Dec 7, 2020. It is now read-only.

Commit

Permalink
examples: search engine integration
Browse files Browse the repository at this point in the history
Signed-off-by: Leonardo Rossi <leonardo.r@cern.ch>
  • Loading branch information
Leonardo Rossi committed Feb 25, 2016
1 parent d5a38fe commit 99f9607
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 15 deletions.
74 changes: 66 additions & 8 deletions examples/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,25 @@
Usage:
1. Install oauthclient:
1. Install elasticsearch:
.. code-block:: console
wget -qO - https://packages.elastic.co/GPG-KEY-elasticsearch \
| apt-key add -
echo "deb http://packages.elastic.co/elasticsearch/2.x/debian stable \
main" | tee -a /etc/apt/sources.list.d/elasticsearch-2.x.list
apt-get update && apt-get install -y elasticsearch default-jdk
service elasticsearch start
2. Install invenio-marc21:
.. code-block:: console
cdvirtualenv src/invenio-marc21
pip install -e .[all]
2. Create database and tables:
3. Create database and tables:
.. code-block:: console
Expand All @@ -44,7 +55,7 @@
You can find the database in `examples/app.db`.
3. Load demo records from invenio-records (see
4. Load demo records from invenio-records (see
invenio_records/data/marc21/bibliographic.xml):
.. code-block:: console
Expand All @@ -55,45 +66,61 @@
flask -a app.py records create
$ flask -a app.py fixtures records
4. Run the development server:
5. Run the development server:
.. code-block:: console
flask -a app.py run -h 0.0.0.0 -p 5000
5. Open in a browser the page `http://0.0.0.0:5000/example/[:pid]`.
6a. Open in a browser the page `http://0.0.0.0:5000/example/[:pid]`.
E.g. open `http://0.0.0.0:5000/example/1`
6b. Open in a browser the page `http://0.0.0.0:5000/search?q=[:query]`.
E.g. open `http://0.0.0.0:5000/search?q=control_number:1`
"""

from __future__ import absolute_import, print_function

import os

from flask import Blueprint, Flask, render_template
from flask import Blueprint, Flask, render_template, request
from flask_babelex import Babel
from flask_cli import FlaskCLI
from invenio_db import InvenioDB, db
from invenio_pidstore.minters import recid_minter
from invenio_pidstore.models import PersistentIdentifier
from invenio_records import InvenioRecords
from invenio_records.models import RecordMetadata
from invenio_search import InvenioSearch
from invenio_records.api import Record
from invenio_search import InvenioSearch, current_search, \
current_search_client, Query
from invenio_search.utils import schema_to_index

from invenio_marc21 import InvenioMARC21

from invenio_indexer.api import RecordIndexer


# Create Flask application
app = Flask(__name__)
app.config.update(
ELASTICSEARCH_HOST="localhost:9200",
SEARCH_ELASTIC_HOSTS="localhost:9200",
SEARCH_ELASTIC_KEYWORD_MAPPING={None: ['_all']},
SQLALCHEMY_DATABASE_URI=os.getenv('SQLALCHEMY_DATABASE_URI',
'sqlite:///app.db'),
)
app.debug = True
Babel(app)
FlaskCLI(app)
InvenioDB(app)
InvenioRecords(app)
search = InvenioSearch(app)
InvenioSearch(app)
InvenioMARC21(app)

Expand All @@ -111,8 +138,29 @@ def example(index):
"""Index page."""
pid = PersistentIdentifier.query.filter_by(id=index).one()
record = RecordMetadata.query.filter_by(id=pid.object_uuid).first()
record.json['control_number'] = pid.pid_value
return render_template("app/detail.html", record=record.json,
title="Demosite Invenio Org")


return render_template("app/detail.html", record=record.json, pid=pid,
@app.route('/search', methods=['GET', 'POST'])
def search():
"""Query Elasticsearch using Invenio query syntax."""
index_names = current_search.mappings.keys()
schema = "mappings/marc21_holdings.json"
index, doc_type = schema_to_index(schema, index_names=index_names)
page = request.values.get('page', 1, type=int)
# size = request.values.get('size', 1, type=int)
size = 1
query = Query(request.values.get('q', ''))[(page-1)*size:page*size]
response = current_search_client.search(
index=index,
doc_type=doc_type,
body=query.body,
)
# return jsonify(**response)
record = response['hits']['hits'][0]['_source']
return render_template("app/detail.html", record=record,
title="Demosite Invenio Org")


Expand All @@ -124,7 +172,17 @@ def fixtures():
@fixtures.command()
def records():
"""Load fixtures."""
# clean indices
current_search_client.indices.delete_alias(
'_all', '_all', ignore=[400, 404])
current_search_client.indices.delete('*')
# mint and index record
with db.session.begin_nested():
indexer = RecordIndexer()
for record in RecordMetadata.query.all():
recid_minter(record.id, record.json)
record = Record(record.json, model=record)
record['$schema'] = "mappings/marc21_holdings.json"
indexer.index(record)
db.session.merge(record.model)
db.session.commit()
18 changes: 15 additions & 3 deletions examples/templates/app/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,21 @@
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.
-#}
<!DOCTYPE html>
<html lang="en" ng-app="eshApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/static/app/css/app.css" />
</head>
<body>

<link rel="stylesheet" href="/static/app/css/app.css"></link>
<h1 class=title>{{ title }}</h1>

<h1 class=title>{{ title }}</h1>
{%- extends "app/detail_base.html" -%}

</body>
</html>

{%- extends "app/detail_base.html" -%}
8 changes: 4 additions & 4 deletions examples/templates/app/detail_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,20 @@
{%- block page_body %}
<div class="container">
<div class="extra-info">
{%- if pid %}
{%- if 'control_number' in record %}
{%- block record_control_number %}
<b>{{ _('control number:') }}</b>
{{ pid.pid_type }} {{pid.pid_value}}
{{ record['control_number'] }}
{%- endblock %}
{% endif %}
</br>
<br>
{%- if 'publication_distribution_imprint' in record %}
{%- block record_publication_date %}
<b>{{ _('Publication date:') }}</b>
{{ record['publication_distribution_imprint'][0]['date_of_publication_distribution'][0] }}
{%- endblock %}
{% endif %}
</br>
<br>
{%- if 'other_standard_identifier' in record %}
{%- block record_other_standard_identifier %}
<b>{{ _('OAI Id:') }}</b>
Expand Down

0 comments on commit 99f9607

Please sign in to comment.