Skip to content

Commit

Permalink
global: fix repeated indices prefix
Browse files Browse the repository at this point in the history
 * prefix are not added multiple times to indices anymore
 * partially addresses #129
  • Loading branch information
ntarocco committed Feb 18, 2019
1 parent 30d6551 commit 85c0ef8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 19 deletions.
20 changes: 8 additions & 12 deletions invenio_search/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,14 @@ class Meta:

def __init__(self, **kwargs):
"""Use Meta to set kwargs defaults."""
kwargs.setdefault('index', getattr(self.Meta, 'index', None))
# elasticsearch_dsl might pass a list or string
if isinstance(kwargs['index'], (tuple, list)):
kwargs['index'] = [
prefix_index(app=current_app, index=index)
for index in kwargs['index']
]
elif kwargs['index'] is not None:
kwargs['index'] = prefix_index(
app=current_app,
index=kwargs['index'],
)
# at object instantiation, kwargs['index'] is not defined.
# Elasticsearch-dsl-py re-instantiated the object at each search
# by cloning it and passing as kwargs the list of indices
# kwargs['index'] = ['index-name1', 'index-name2']
if not kwargs.get('index') and getattr(self.Meta, 'index', None):
_index_name = prefix_index(app=current_app,
index=getattr(self.Meta, 'index', None))
kwargs.setdefault('index', _index_name)

kwargs.setdefault('doc_type', getattr(self.Meta, 'doc_types', None))
kwargs.setdefault('using', current_search_client)
Expand Down
4 changes: 2 additions & 2 deletions invenio_search/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

from . import config
from .proxies import current_search_client
from .utils import build_index_name, prefix_index
from .utils import build_index_name


def _get_indices(tree_or_filename):
Expand Down Expand Up @@ -72,7 +72,7 @@ def templates(self):

for template in result:
for name, path in template.items():
templates[prefix_index(self.app, name)] = path
templates[name] = path

return templates

Expand Down
6 changes: 1 addition & 5 deletions invenio_search/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@ def prefix_index(app, index):
:param app: Flask app to get the config from.
:param index: Name of the index to prefix.
:returns: A string with the new index name prefixed in needed.
:returns: A string with the new index name prefixed if needed.
"""
index_prefix = app.config['SEARCH_INDEX_PREFIX'] or ''
# avoid to add the prefix multiple times
if index.startswith(index_prefix):
return index

return index_prefix + index


Expand Down

0 comments on commit 85c0ef8

Please sign in to comment.