Skip to content

Commit a92973c

Browse files
committed
search: remove useless translated facets
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]>
1 parent d08189d commit a92973c

File tree

5 files changed

+71
-1026
lines changed

5 files changed

+71
-1026
lines changed

rero_ils/config.py

+20-16
Original file line numberDiff line numberDiff line change
@@ -1340,29 +1340,33 @@ def _(x):
13401340
'documents', RERO_ILS_DEFAULT_AGGREGATION_SIZE)
13411341
RECORDS_REST_FACETS = dict(
13421342
documents=dict(
1343+
i18n_aggs=dict(
1344+
author=dict(
1345+
en=dict(
1346+
terms=dict(field='facet_contribution_en',
1347+
size=DOCUMENTS_AGGREGATION_SIZE)
1348+
),
1349+
fr=dict(
1350+
terms=dict(field='facet_contribution_fr',
1351+
size=DOCUMENTS_AGGREGATION_SIZE)
1352+
),
1353+
de=dict(
1354+
terms=dict(field='facet_contribution_de',
1355+
size=DOCUMENTS_AGGREGATION_SIZE)
1356+
),
1357+
it=dict(
1358+
terms=dict(field='facet_contribution_it',
1359+
size=DOCUMENTS_AGGREGATION_SIZE)
1360+
),
1361+
),
1362+
),
13431363
aggs=dict(
13441364
# The organisation or library facet is defined
13451365
# dynamically during the query (query.py)
13461366
document_type=dict(
13471367
terms=dict(field='type',
13481368
size=DOCUMENTS_AGGREGATION_SIZE)
13491369
),
1350-
contribution__en=dict(
1351-
terms=dict(field='facet_contribution_en',
1352-
size=DOCUMENTS_AGGREGATION_SIZE)
1353-
),
1354-
contribution__fr=dict(
1355-
terms=dict(field='facet_contribution_fr',
1356-
size=DOCUMENTS_AGGREGATION_SIZE)
1357-
),
1358-
contribution__de=dict(
1359-
terms=dict(field='facet_contribution_de',
1360-
size=DOCUMENTS_AGGREGATION_SIZE)
1361-
),
1362-
contribution__it=dict(
1363-
terms=dict(field='facet_contribution_it',
1364-
size=DOCUMENTS_AGGREGATION_SIZE)
1365-
),
13661370
language=dict(
13671371
terms=dict(field='language.value',
13681372
size=DOCUMENTS_AGGREGATION_SIZE)

rero_ils/facets.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# RERO ILS
4+
# Copyright (C) 2019 RERO
5+
# Copyright (C) 2020 UCLOUVAIN
6+
#
7+
# This program is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU Affero General Public License as published by
9+
# the Free Software Foundation, version 3 of the License.
10+
#
11+
# This program is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Affero General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Affero General Public License
17+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
19+
"""Facets and factories for result aggregation."""
20+
21+
from __future__ import absolute_import, print_function
22+
23+
from flask import current_app, request
24+
from invenio_i18n.ext import current_i18n
25+
26+
27+
def i18n_facets_factory(search, index):
28+
"""Add a i18n facets to search query.
29+
30+
It's possible to select facets which should be added to query
31+
by passing their name in `facets` parameter.
32+
33+
:param search: Basic search object.
34+
:param index: Index name.
35+
:returns: the new search object.
36+
"""
37+
facets_config = current_app.config['RECORDS_REST_FACETS'].get(index, {})
38+
# i18n Aggregations.
39+
for name, agg in facets_config.get("i18n_aggs", {}).items():
40+
i18n_agg = agg.get(
41+
request.args.get("lang", current_i18n.language),
42+
agg.get(current_app.config.get('BABEL_DEFAULT_LANGUAGE'))
43+
)
44+
search.aggs[name] = i18n_agg if not callable(i18n_agg) \
45+
else i18n_agg()
46+
return search

rero_ils/query.py

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#
33
# RERO ILS
44
# Copyright (C) 2019 RERO
5+
# Copyright (C) 2020 UCLOUVAIN
56
#
67
# This program is free software: you can redistribute it and/or modify
78
# it under the terms of the GNU Affero General Public License as published by
@@ -25,6 +26,7 @@
2526
from flask import current_app, request
2627
from invenio_records_rest.errors import InvalidQueryRESTError
2728

29+
from .facets import i18n_facets_factory
2830
from .modules.organisations.api import Organisation, current_organisation
2931
from .modules.patrons.api import current_patron
3032

@@ -270,6 +272,8 @@ def _boosting_parser(query_boosting, search_index):
270272
raise InvalidQueryRESTError()
271273

272274
search, urlkwargs = default_facets_factory(search, search_index)
275+
# i18n translated facets
276+
search = i18n_facets_factory(search, search_index)
273277
search, sortkwargs = default_sorter_factory(search, search_index)
274278
for key, value in sortkwargs.items():
275279
urlkwargs.add(key, value)

tests/api/documents/test_documents_rest.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,7 @@ def test_documents_facets(
173173
aggs = data['aggregations']
174174
# check all facets are present
175175
for facet in [
176-
'document_type', 'contribution__en', 'contribution__fr',
177-
'contribution__de', 'contribution__it', 'language', 'subject', 'status'
176+
'document_type', 'author', 'language', 'subject', 'status'
178177
]:
179178
assert aggs[facet]
180179

0 commit comments

Comments
 (0)