Ejemplo n.º 1
0
def collection_entities(collection, depth=2, filter_schema=None):
    config = get_loom_config()
    # FIXME: this is a performance nightmare. Think about how to fix it.
    results = []
    q = config.entities.subjects(schema=filter_schema,
                                 collection_id=collection.id,
                                 right=authz.entity_right())
    for (subject, schema) in q:
        data = config.entities.get(subject, schema=schema, depth=depth,
                                   right=authz.entity_right())
        results.append(result_entity(data))
    return results
Ejemplo n.º 2
0
def collection_entities(collection, depth=2, filter_schema=None):
    config = get_loom_config()
    # FIXME: this is a performance nightmare. Think about how to fix it.
    results = []
    q = config.entities.subjects(schema=filter_schema,
                                 collection_id=collection.id,
                                 right=authz.entity_right())
    for (subject, schema) in q:
        data = config.entities.get(subject,
                                   schema=schema,
                                   depth=depth,
                                   right=authz.entity_right())
        results.append(result_entity(data))
    return results
Ejemplo n.º 3
0
def execute_query(args, q, facets):
    """ Execute the query and return a set of results. """
    result = get_es().search(index=get_es_index(), body=q)
    hits = result.get('hits', {})
    output = {
        'status': 'ok',
        'results': [],
        'offset': q['from'],
        'limit': q['size'],
        'took': result.get('took'),
        'total': hits.get('total'),
        'next': None,
        'facets': {}
    }
    next_offset = output['offset'] + output['limit']
    if output['total'] > next_offset:
        params = {'offset': next_offset}
        for k, v in args.iterlists():
            if k in ['facet', 'offset']:
                continue
            params[k] = v
        output['next'] = url_for('base.search', **params)

    for doc in hits.get('hits', []):
        hlt = doc.get('highlight', {}).get('$text', None)
        doc = result_entity(doc)
        if hlt is not None:
            doc['$highlight'] = hlt
        output['results'].append(doc)

    # traverse and get all facets.
    aggs = result.get('aggregations')
    for facet in facets:
        scoped = aggs.get('scoped').get(facet, {})
        value = aggs.get(facet, scoped.get(facet, {}))
        data = {
            'total': scoped.get('doc_count', hits.get('total')),
            'values': value.get('buckets', [])
        }
        output['facets'][facet] = data

    return output
Ejemplo n.º 4
0
def execute_query(args, q, facets):
    """ Execute the query and return a set of results. """
    result = get_es().search(index=get_es_index(), body=q)
    hits = result.get('hits', {})
    output = {
        'status': 'ok',
        'results': [],
        'offset': q['from'],
        'limit': q['size'],
        'took': result.get('took'),
        'total': hits.get('total'),
        'next': None,
        'facets': {}
    }
    next_offset = output['offset'] + output['limit']
    if output['total'] > next_offset:
        params = {'offset': next_offset}
        for k, v in args.iterlists():
            if k in ['facet', 'offset']:
                continue
            params[k] = v
        output['next'] = url_for('base.search', **params)

    for doc in hits.get('hits', []):
        hlt = doc.get('highlight', {}).get('$text', None)
        doc = result_entity(doc)
        if hlt is not None:
            doc['$highlight'] = hlt
        output['results'].append(doc)

    # traverse and get all facets.
    aggs = result.get('aggregations')
    for facet in facets:
        scoped = aggs.get('scoped').get(facet, {})
        value = aggs.get(facet, scoped.get(facet, {}))
        data = {
            'total': scoped.get('doc_count', hits.get('total')),
            'values': value.get('buckets', [])
        }
        output['facets'][facet] = data

    return output
Ejemplo n.º 5
0
def more_like_this(entity):
    query = {
        'query': {
            'more_like_this': {
                'fields': ['name', '$text', '$latin'],
                'docs': [
                    {'_id': entity['_id'], '_type': entity['_type']}
                ],
            },
        },
        '_source': {
            'excludes': ['$text', '$latin', '*.*']
        }
    }
    query = authz_filter(query)
    results = get_es().search(index=get_es_index(), body=query)
    similar = {'status': 'ok', 'results': []}
    for result in results.get('hits', {}).get('hits', []):
        similar['results'].append(result_entity(result))
    return similar
Ejemplo n.º 6
0
def view(id):
    entities = get_loom_config().entities
    data = obj_or_404(entities.get(id, depth=get_depth(3),
                                   right=authz.entity_right()))
    return jsonify({'status': 'ok', 'data': result_entity(data)})
Ejemplo n.º 7
0
def view(id):
    entities = get_loom_config().entities
    data = obj_or_404(
        entities.get(id, depth=get_depth(3), right=authz.entity_right()))
    return jsonify({'status': 'ok', 'data': result_entity(data)})