def schema_prefix(schema): """Get index prefix for a given schema.""" if not schema: return None index, doctype = schema_to_index( schema, index_names=current_search.mappings.keys()) return index.split('-')[0]
def record_to_index(record): """Get index/doc_type given a record. It tries to extract from `record['$schema']` the index and doc_type. If it fails, return the default values. :param record: The record object. :return: Tuple (index, doc_type). """ index_names = current_search.mappings.keys() schema = record.get('$schema', '') if isinstance(schema, dict): schema = schema.get('$ref', '') # authorities specific transformation if re.search(r'/mef/', schema): schema = re.sub(r'/mef/', '/contributions/', schema) schema = re.sub(r'mef-contribution', 'contribution', schema) index, doc_type = schema_to_index(schema, index_names=index_names) if index and doc_type: return index, doc_type else: return (current_app.config['INDEXER_DEFAULT_INDEX'], current_app.config['INDEXER_DEFAULT_DOC_TYPE'])
def test_schema_to_index_with_names(app): """Test that prefix is added to the index when creating it.""" result = schema_to_index('default.json', index_names=['default']) assert result == ( 'default', '_doc' if ES_VERSION[0] >= 7 else 'default' )
def _get_percolator_doc_type(index): es_ver = ES_VERSION[0] if es_ver == 2: return '.percolator' elif es_ver == 5: return 'percolators' elif es_ver in (6, 7): mapping_path = current_search.mappings[index] _, doc_type = schema_to_index(mapping_path) return doc_type
def _collect_mappings(self): index_names = current_search.mappings.keys() for rec in self.managed_records: published_record = rec.published.record_class if issubclass(published_record, AllowedSchemaMixin): published_record._prepare_schemas() for json_schema in published_record.ALLOWED_SCHEMAS: index_name = schema_to_index(json_schema, index_names=index_names)[0] rec.published.set_index(json_schema, index_name) rec.draft.set_index(json_schema, index_name)
def record_from_index(record): """Get index/doc_type given a record. It tries to extract from `record['$schema']` the index and doc_type. If it fails, return the default values. :param record: The record object. :returns: Tuple (index, doc_type). """ index_names = current_search.mappings.keys() schema = record.get('metadata').get('$schema', '') if isinstance(schema, dict): schema = schema.get('$ref', '') index, doc_type = schema_to_index(schema, index_names=index_names) if index and doc_type: return index, doc_type else: return (current_app.config['INDEXER_DEFAULT_INDEX'], current_app.config['INDEXER_DEFAULT_DOC_TYPE'])
def record_to_index(record): """Get index/doc_type given a record. It tries to extract from `record['$schema']` the index and doc_type. If it fails, return the default values. :param record: The record object. :returns: Tuple (index, doc_type). """ index_names = current_search.mappings.keys() schema = record.get('$schema', '') if isinstance(schema, dict): schema = schema.get('$ref', '') endpoint: RecordEndpointConfiguration = current_drafts.endpoint_for_record( record) if endpoint: return endpoint.get_index(schema), '_doc' index = schema_to_index(schema, index_names=index_names)[0] return index, '_doc'
def test_schema_to_index(schema, expected, index_names, app): """Test the expected value of schema to index.""" result = schema_to_index(schema, index_names=index_names) if ES_VERSION[0] >= 7 and expected[0]: expected = (expected[0], '_doc') assert result == expected
def test_schema_to_index_with_names(app): """Test that prefix is added to the index when creating it.""" result = schema_to_index("default.json", index_names=["default"]) assert result == ("default", "_doc" if ES_VERSION[0] >= 7 else "default")