Commit 4a59fe2c authored by Pablo Panero's avatar Pablo Panero
Browse files

Use custom record_to_index function to comply with ES central service

parent 94e98863
......@@ -62,6 +62,7 @@ JSONSCHEMAS_REGISTER_ENDPOINTS_UI = True
# TODO use ES central service. Change INDEXER_RECORD_TO_INDEX = 'invenio_indexer.utils.default_record_to_index'
INDEX_PREFIX = os.getenv('CERN_SEARCH_INDEX_PREFIX', 'cernsearch')
INDEXER_DEFAULT_DOC_TYPE = os.getenv('CERN_SEARCH_DEFAULT_DOC_TYPE', 'doc_v0.0.1')
INDEXER_DEFAULT_INDEX = os.getenv('CERN_SEARCH_DEFAULT_INDEX', 'cernsearch-test-doc_v0.0.1')
......
......@@ -3,10 +3,9 @@
from flask_security import current_user
from flask import request, current_app
from invenio_indexer.utils import default_record_to_index
from invenio_search import current_search_client
from cern_search_rest_api.modules.cernsearch.utils import get_user_provides
from cern_search_rest_api.modules.cernsearch.utils import get_user_provides, cern_search_record_to_index
"""Access control for CERN Search."""
......@@ -107,7 +106,7 @@ def has_owner_permission(user, record=None):
def get_index_from_request(record=None):
if record is not None and record.get('$schema', '') is not None:
return default_record_to_index(record)
return cern_search_record_to_index(record)
current_app.logger.debug('get_index_from_schema() No record or no $schema in it, using defaults')
return (current_app.config['INDEXER_DEFAULT_INDEX'],
current_app.config['INDEXER_DEFAULT_DOC_TYPE'])
......
......@@ -6,7 +6,7 @@
from flask import g
from flask import current_app
from invenio_search import current_search
from invenio_search.utils import schema_to_index
from invenio_search.utils import schema_to_index, build_index_name
def get_user_provides():
......@@ -23,27 +23,28 @@ def cern_search_record_to_index(record):
:param record: The record object.
:returns: Tuple (index, doc_type).
"""
INDEX_PREFIX = current_app.config['CERN_SEARCH_DEFAULT_INDEX_PREFIX']
prefix = current_app.config['INDEX_PREFIX']
index_names = current_search.mappings.keys()
schema = record.get('$schema', '')
if isinstance(schema, dict):
schema = schema.get('$ref', '')
aux = current_app.config['CERN_SEARCH_INDEX_PREFIX']
if aux:
INDEX_PREFIX = aux
index, doc_type = schema_to_index(schema, index_names=index_names)
parts = schema.split('/')
if index_names:
for start in range(len(parts)):
index_name = build_index_name(*parts[start:])
if index_name in index_names:
if index_name.startswith(prefix) and len(index_name) > len(prefix) + 2:
return index_name, index_name[len(prefix) + 1:]
if index and doc_type:
current_app.logger.debug('Index {0}{1} - Doc {2}'.format(INDEX_PREFIX, index, doc_type))
return '{0}{1}'.format(INDEX_PREFIX, index), doc_type
else:
current_app.logger.debug('Index {0}{1} - Doc {2}'.format(
current_app.config['CERN_SEARCH_DEFAULT_INDEX_PREFIX'],
current_app.config['CERN_SEARCH_INDEX_PREFIX'],
current_app.config['INDEXER_DEFAULT_INDEX'],
current_app.config['INDEXER_DEFAULT_DOC_TYPE'])
)
return ('{0}{1}'.format(current_app.config['CERN_SEARCH_DEFAULT_INDEX_PREFIX'],
return ('{0}{1}'.format(current_app.config['CERN_SEARCH_INDEX_PREFIX'],
current_app.config['INDEXER_DEFAULT_INDEX']),
current_app.config['INDEXER_DEFAULT_DOC_TYPE'])
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment