Skip to content

Commit

Permalink
search: remove useless translated facets
Browse files Browse the repository at this point in the history
Independently of Elasticsearch tuning, we need to remove the unnecessary translated facets
to increase the search performance.

* Adds i18n facets factory.
* Updates search factory.
* Adapts config.py.
* Removes duplicate tests on items rest.
* Renames contribution facet to author.

Co-Authored-by: Lauren-D <[email protected]>
  • Loading branch information
lauren-d committed Sep 14, 2020
1 parent d08189d commit a92973c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1,026 deletions.
36 changes: 20 additions & 16 deletions rero_ils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1340,29 +1340,33 @@ def _(x):
'documents', RERO_ILS_DEFAULT_AGGREGATION_SIZE)
RECORDS_REST_FACETS = dict(
documents=dict(
i18n_aggs=dict(
author=dict(
en=dict(
terms=dict(field='facet_contribution_en',
size=DOCUMENTS_AGGREGATION_SIZE)
),
fr=dict(
terms=dict(field='facet_contribution_fr',
size=DOCUMENTS_AGGREGATION_SIZE)
),
de=dict(
terms=dict(field='facet_contribution_de',
size=DOCUMENTS_AGGREGATION_SIZE)
),
it=dict(
terms=dict(field='facet_contribution_it',
size=DOCUMENTS_AGGREGATION_SIZE)
),
),
),
aggs=dict(
# The organisation or library facet is defined
# dynamically during the query (query.py)
document_type=dict(
terms=dict(field='type',
size=DOCUMENTS_AGGREGATION_SIZE)
),
contribution__en=dict(
terms=dict(field='facet_contribution_en',
size=DOCUMENTS_AGGREGATION_SIZE)
),
contribution__fr=dict(
terms=dict(field='facet_contribution_fr',
size=DOCUMENTS_AGGREGATION_SIZE)
),
contribution__de=dict(
terms=dict(field='facet_contribution_de',
size=DOCUMENTS_AGGREGATION_SIZE)
),
contribution__it=dict(
terms=dict(field='facet_contribution_it',
size=DOCUMENTS_AGGREGATION_SIZE)
),
language=dict(
terms=dict(field='language.value',
size=DOCUMENTS_AGGREGATION_SIZE)
Expand Down
46 changes: 46 additions & 0 deletions rero_ils/facets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-
#
# RERO ILS
# Copyright (C) 2019 RERO
# Copyright (C) 2020 UCLOUVAIN
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Facets and factories for result aggregation."""

from __future__ import absolute_import, print_function

from flask import current_app, request
from invenio_i18n.ext import current_i18n


def i18n_facets_factory(search, index):
"""Add a i18n facets to search query.
It's possible to select facets which should be added to query
by passing their name in `facets` parameter.
:param search: Basic search object.
:param index: Index name.
:returns: the new search object.
"""
facets_config = current_app.config['RECORDS_REST_FACETS'].get(index, {})
# i18n Aggregations.
for name, agg in facets_config.get("i18n_aggs", {}).items():
i18n_agg = agg.get(
request.args.get("lang", current_i18n.language),
agg.get(current_app.config.get('BABEL_DEFAULT_LANGUAGE'))
)
search.aggs[name] = i18n_agg if not callable(i18n_agg) \
else i18n_agg()
return search
4 changes: 4 additions & 0 deletions rero_ils/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#
# RERO ILS
# Copyright (C) 2019 RERO
# Copyright (C) 2020 UCLOUVAIN
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
Expand All @@ -25,6 +26,7 @@
from flask import current_app, request
from invenio_records_rest.errors import InvalidQueryRESTError

from .facets import i18n_facets_factory
from .modules.organisations.api import Organisation, current_organisation
from .modules.patrons.api import current_patron

Expand Down Expand Up @@ -270,6 +272,8 @@ def _boosting_parser(query_boosting, search_index):
raise InvalidQueryRESTError()

search, urlkwargs = default_facets_factory(search, search_index)
# i18n translated facets
search = i18n_facets_factory(search, search_index)
search, sortkwargs = default_sorter_factory(search, search_index)
for key, value in sortkwargs.items():
urlkwargs.add(key, value)
Expand Down
3 changes: 1 addition & 2 deletions tests/api/documents/test_documents_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ def test_documents_facets(
aggs = data['aggregations']
# check all facets are present
for facet in [
'document_type', 'contribution__en', 'contribution__fr',
'contribution__de', 'contribution__it', 'language', 'subject', 'status'
'document_type', 'author', 'language', 'subject', 'status'
]:
assert aggs[facet]

Expand Down
Loading

0 comments on commit a92973c

Please sign in to comment.