Commit f5d96b72 authored by Pablo Panero's avatar Pablo Panero
Browse files

global: refactor to use invenio-search prefix

parent 1059d05b
......@@ -67,9 +67,6 @@ JSONSCHEMAS_REGISTER_ENDPOINTS_UI = True
# Indexer
# =======
# 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')
......
......@@ -10,9 +10,9 @@
from flask_security import current_user
from flask import request, current_app
from invenio_search import current_search_client
from invenio_indexer.utils import default_record_to_index
from cern_search_rest_api.modules.cernsearch.utils import get_user_provides, cern_search_record_to_index, \
get_index_from_request
from cern_search_rest_api.modules.cernsearch.utils import get_user_provides
"""Access control for CERN Search."""
......@@ -100,7 +100,7 @@ def has_owner_permission(user, record=None):
if user.is_authenticated:
# Allow based in the '_access' key
user_provides = get_user_provides()
es_index, doc = get_index_from_request(record)
es_index, doc = default_record_to_index(record)
current_app.logger.debug('Using index {idx} and doc {doc}'.format(idx=es_index, doc=doc))
if current_search_client.indices.exists([es_index]):
mapping = current_search_client.indices.get_mapping([es_index])
......
......@@ -10,56 +10,8 @@
"""Helper methods for CERN Search records."""
from flask import g
from flask import current_app
from invenio_search import current_search
from invenio_search.utils import schema_to_index, build_index_name
def get_user_provides():
"""Extract the user's provides from g."""
return [need.value for need in g.identity.provides]
def get_index_from_request(record=None):
if record is not None and record.get('$schema', '') is not None:
return cern_search_record_to_index(record)
current_app.logger.debug('get_index_from_schema(): Record {record} - $schema {schema}. Using defaults'.format(
record=record,
schema='No record' if record is None else record.get('$schema')
))
return (current_app.config['INDEXER_DEFAULT_INDEX'],
current_app.config['INDEXER_DEFAULT_DOC_TYPE'])
def cern_search_record_to_index(record):
"""Get index/doc_type given a record.
It tries to extract from `record['$schema']` the index and doc_type,
the index has `CERN_SEARCH_INDEX_PREFIX` as prefix or `CERN_SEARCH_DEFAULT_INDEX_PREFIX`
if it is not set up to be able to use the ES central service.
If it fails, return the default values. In this case the prefix is the default value.
:param record: The record object.
:returns: Tuple (index, doc_type).
"""
prefix = current_app.config['INDEX_PREFIX']
index_names = current_search.mappings.keys()
schema = record.get('$schema', '')
if isinstance(schema, dict):
schema = schema.get('$ref', '')
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:]
current_app.logger.debug('Index {0} - Doc {1}'.format(
current_app.config['INDEXER_DEFAULT_INDEX'],
current_app.config['INDEXER_DEFAULT_DOC_TYPE'])
)
return (current_app.config['INDEXER_DEFAULT_INDEX'],
current_app.config['INDEXER_DEFAULT_DOC_TYPE'])
\ No newline at end of file
......@@ -30,9 +30,9 @@ from flask import Blueprint, Response, json, url_for, request, make_response, cu
from invenio_records_rest.views import need_record_permission, pass_record
from invenio_rest import ContentNegotiatedMethodView
from invenio_search import current_search_client
from invenio_indexer.utils import default_record_to_index
from cern_search_rest_api.modules.cernsearch.search import RecordCERNSearch
from cern_search_rest_api.modules.cernsearch.utils import get_index_from_request
def create_error_handlers(blueprint):
......@@ -170,7 +170,7 @@ class UBQRecordResource(ContentNegotiatedMethodView):
# Perform ES API _updated_by_query
control_num_query = 'control_number:"{recid}"'.format(recid=record['control_number'])
script = data["ubq"]
index, doc = get_index_from_request(data)
index, doc = default_record_to_index(data)
es_response = current_search_client.update_by_query(
index=index,
......
Markdown is supported
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