def get_object(source_id, object_id, doc_type=u'items'):
index_name = '%s_%s' % (current_app.config['DEFAULT_INDEX_PREFIX'],
source_id)
include_fields = [f.strip() for f in request.args.get('include_fields', '').split(',') if f.strip()]
excluded_fields = validate_included_fields(
include_fields=include_fields,
excluded_fields=current_app.config['EXCLUDED_FIELDS_DEFAULT'],
allowed_to_include=current_app.config['ALLOWED_INCLUDE_FIELDS_DEFAULT']
)
try:
obj = current_app.es.get(index=index_name, id=object_id,
doc_type=doc_type,
_source_exclude=excluded_fields)
except NotFoundError, e:
if e.error.startswith('IndexMissingException'):
message = 'Source \'%s\' does not exist' % source_id
else:
message = 'Document not found.'
raise OcdApiError(message, 404)
# Log a 'get_object' event if usage logging is enabled
if current_app.config['USAGE_LOGGING_ENABLED']:
tasks.log_event.delay(
user_agent=request.user_agent.string,
referer=request.headers.get('Referer', None),
user_ip=request.remote_addr,
created_at=datetime.utcnow(),
event_type='get_object',
source_id=source_id,
doc_type=doc_type,
object_id=object_id
)
for key in current_app.config['EXCLUDED_FIELDS_ALWAYS']:
try:
del obj['_source'][key]
except KeyError as e:
pass
return jsonify(obj['_source'])
评论列表
文章目录