示例#1
0
 def setUp(self):
     Role.create_defaults()
     self.config = self.app.loom_config
     if not BA_FIXTURES['resolver']:
         schema_dir = os.path.join(FIXTURES, 'schema')
         load_local_schema(self.config.resolver, schema_dir=schema_dir)
         BA_FIXTURES['resolver'] = self.config.resolver
     self.config._resolver = BA_FIXTURES['resolver']
     self.es = get_es()
     get_loom_indexer().configure()
示例#2
0
 def setUp(self):
     Role.create_defaults()
     self.config = self.app.loom_config
     if not BA_FIXTURES['resolver']:
         schema_dir = os.path.join(FIXTURES, 'schema')
         load_local_schema(self.config.resolver, schema_dir=schema_dir)
         BA_FIXTURES['resolver'] = self.config.resolver
     self.config._resolver = BA_FIXTURES['resolver']
     self.es = get_es()
     get_loom_indexer().configure()
示例#3
0
def suggest_entity(args):
    """ Auto-complete API. """
    config = get_loom_config()
    text = args.get('text')
    options = []
    if text is not None and len(text.strip()):
        q = authz_filter({'match': {'$suggest': text}})
        schema = args.get('$schema')
        if schema is not None:
            # Find matching sub-schemas as well.
            schemas = config.implied_schemas(schema)
            q = add_filter(q, {"terms": {"$schema": schemas}})
        boost_collection = args.get('boost_collection')
        if boost_collection is not None:
            q = {
                'function_score': {
                    'query': q,
                    'functions': [{
                        'boost_factor': 2,
                        'filter': {
                            'term': {
                                '$collections': boost_collection
                            }
                        }
                    }]
                }
            }
        exclude_collection = args.get('exclude_collection')
        if exclude_collection is not None:
            q = add_filter(q, {'not': {
                'term': {'$collections': exclude_collection}
            }})
        q = {
            'size': 5,
            'query': q,
            '_source': ['name', 'id', '$schema']
        }
        result = get_es().search(index=get_es_index(), body=q)
        for res in result.get('hits', {}).get('hits', []):
            options.append(res.get('_source'))
    return {
        'text': text,
        'options': options
    }
示例#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
示例#5
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
示例#6
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
示例#7
0
def suggest_entity(args):
    """ Auto-complete API. """
    config = get_loom_config()
    text = args.get('text')
    options = []
    if text is not None and len(text.strip()):
        q = authz_filter({'match': {'$suggest': text}})
        schema = args.get('$schema')
        if schema is not None:
            # Find matching sub-schemas as well.
            schemas = config.implied_schemas(schema)
            q = add_filter(q, {"terms": {"$schema": schemas}})
        boost_collection = args.get('boost_collection')
        if boost_collection is not None:
            q = {
                'function_score': {
                    'query':
                    q,
                    'functions': [{
                        'boost_factor': 2,
                        'filter': {
                            'term': {
                                '$collections': boost_collection
                            }
                        }
                    }]
                }
            }
        exclude_collection = args.get('exclude_collection')
        if exclude_collection is not None:
            q = add_filter(
                q, {'not': {
                    'term': {
                        '$collections': exclude_collection
                    }
                }})
        q = {'size': 5, 'query': q, '_source': ['name', 'id', '$schema']}
        result = get_es().search(index=get_es_index(), body=q)
        for res in result.get('hits', {}).get('hits', []):
            options.append(res.get('_source'))
    return {'text': text, 'options': options}
示例#8
0
def like(id):
    ent = get_es().get(index=get_es_index(), id=id, _source=False)
    return jsonify(more_like_this(ent))
示例#9
0
def like(id):
    ent = get_es().get(index=get_es_index(), id=id, _source=False)
    return jsonify(more_like_this(ent))