def query(self, **kwargs): return Type.find_fuzzy(self.q or '*:*', filters=self.filters, facet_type=True, rows=self.limit, start=self.offset, **kwargs)
def prefix_search(args): start = int(args.get('start', 0)) limit = int(args.get('limit', 20)) fq = [] if 'type' in args: type_ = args.get('type').strip().lstrip('/') fq.append('+__type__:"%s"' % type_) qs = args.get('prefix', '').strip() qs = qs + "^%d" % (len(qs)/2) results = Type.find(qs, fq, start=start, rows=limit) matches = [] for result in results.get('response', {}).get('docs', []): id = url_for('entity', type=result.get('__type__'), key=result.get('__key__')) matches.append({ 'name': result.get('title'), 'n:type': { 'id': '/' + result.get('__type__'), 'name': result.get('__type__') }, 'r:score': result.get('score'), 'id': id, 'type': ['foo'] }) return { "code" : "/api/status/ok", "status" : "200 OK", "prefix" : args.get('prefix'), "result" : matches }
def index(type_name): """ Index all entities of a given type. """ type_ = Type.by_name(type_name) if type_ is None: print "No such type: %s" % type_name else: type_.index()
def prefix_search(args): start = int(args.get('start', 0)) limit = int(args.get('limit', 20)) fq = [] if 'type' in args: type_ = args.get('type').strip().lstrip('/') fq.append('+__type__:"%s"' % type_) qs = args.get('prefix', '').strip() qs = qs + "^%d" % (len(qs) / 2) results = Type.find(qs, fq, start=start, rows=limit) matches = [] for result in results.get('response', {}).get('docs', []): id = url_for('entity', type=result.get('__type__'), key=result.get('__key__')) matches.append({ 'name': result.get('title'), 'n:type': { 'id': '/' + result.get('__type__'), 'name': result.get('__type__') }, 'r:score': result.get('score'), 'id': id, 'type': ['foo'] }) return { "code": "/api/status/ok", "status": "200 OK", "prefix": args.get('prefix'), "result": matches }
def reconcile(): """ Reconciliation API, emulates Google Refine API. See: http://code.google.com/p/google-refine/wiki/ReconciliationServiceApi """ # TODO: Add proper support for types and namespacing. data = request.args.copy() data.update(request.form.copy()) if 'query' in data: # single q = data.get('query') if q.startswith('{'): try: q = json.loads(q) except ValueError: abort(400) return jsonify(match(q)) elif 'queries' in data: # multiple requests in one query qs = data.get('queries') try: qs = json.loads(qs) except ValueError: abort(400) queries = {} for k, q in qs.items(): queries[k] = match(q) return jsonify(queries) else: domain = url_for('index', _external=True).strip('/') urlp = domain + '{{id}}' types = [{'name': t.name, 'id': '/' + t.name} for t in Type.types()] meta = { 'name': app.config['TITLE'], 'identifierSpace': 'http://rdf.freebase.com/ns/type.object.id', 'schemaSpace': 'http://rdf.freebase.com/ns/type.object.id', 'view': { 'url': urlp }, 'suggest': { 'entity': { 'service_url': domain, 'service_path': '/suggest', 'flyout_service_path': '/flyout' } }, 'preview': { 'url': urlp + '?preview=true', 'width': 600, 'height': 300 }, 'defaultTypes': types } return jsonify(meta)
def reconcile(): """ Reconciliation API, emulates Google Refine API. See: http://code.google.com/p/google-refine/wiki/ReconciliationServiceApi """ # TODO: Add proper support for types and namespacing. data = request.args.copy() data.update(request.form.copy()) if 'query' in data: # single q = data.get('query') if q.startswith('{'): try: q = json.loads(q) except ValueError: abort(400) return jsonify(match(q)) elif 'queries' in data: # multiple requests in one query qs = data.get('queries') try: qs = json.loads(qs) except ValueError: abort(400) queries = {} for k, q in qs.items(): queries[k] = match(q) return jsonify(queries) else: domain = url_for('index', _external=True).strip('/') urlp = domain + '{{id}}' types = [{'name': t.name, 'id': '/' + t.name} for t in Type.types()] meta = { 'name': app.config['TITLE'], 'identifierSpace': 'http://rdf.freebase.com/ns/type.object.id', 'schemaSpace': 'http://rdf.freebase.com/ns/type.object.id', 'view': {'url': urlp}, 'suggest': { 'entity': { 'service_url': domain, 'service_path': '/suggest', 'flyout_service_path': '/flyout' } }, 'preview': { 'url': urlp + '?preview=true', 'width': 600, 'height': 300 }, 'defaultTypes': types } return jsonify(meta)
def match(q): if isinstance(q, basestring): q = {'query': q} try: limit = max(1, min(100, int(q.get('limit')))) except ValueError: limit = 5 except TypeError: limit = 5 filters = [(p.get('pid'), p.get('v', '*')) for p in \ q.get('properties', []) if 'pid' in p] type_ = q.get('type') if type_ is not None: if isinstance(type_, basestring): type_ = [type_] types = map(lambda t: t.strip().lstrip('/'), type_) filters.extend([('__type__', t) for t in types]) # todo: implement types_strict results = Type.find_fuzzy(q.get('query'), filters=filters, rows=limit) matches = [] for result in results.get('response', {}).get('docs', []): id = url_for('entity', type=result.get('__type__'), key=result.get('__key__')) uri = url_for('entity', type=result.get('__type__'), key=result.get('__key__'), _external=True) matches.append({ 'name': result.get('title'), 'score': result.get('score') * 10, 'type': [{ 'id': '/' + result.get('__type__'), 'name': result.get('__type__') }], 'id': id, 'uri': uri, 'match': result.get('title') == q.get('query') }) return {'result': matches, 'num': results.get('response').get('numFound')}
def entity(type, key, format=None): type_ = Type.by_name(type) if type_ is None: abort(404) entity = type_.by_key(key) if entity is None: abort(404) if '__id__' in entity: del entity['__id__'] url = url_for('entity', type=type, key=key, _external=True) format = request_format(format) if format == 'json': entity['_url'] = url return jsonify(entity) #if 'redirect_url' in entity: # return redirect(entity.get('redirect_url'), # code=303) return render_template('view.tmpl', entity=entity, url=url)
def match(q): if isinstance(q, basestring): q = {'query': q} try: limit = max(1, min(100, int(q.get('limit')))) except ValueError: limit = 5 except TypeError: limit = 5 filters = [(p.get('pid'), p.get('v', '*')) for p in \ q.get('properties', []) if 'pid' in p] type_ = q.get('type') if type_ is not None: if isinstance(type_, basestring): type_ = [type_] types = map(lambda t: t.strip().lstrip('/'), type_) filters.extend([('__type__', t) for t in types]) # todo: implement types_strict results = Type.find_fuzzy(q.get('query'), filters=filters, rows=limit) matches = [] for result in results.get('response', {}).get('docs', []): id = url_for('entity', type=result.get('__type__'), key=result.get('__key__')) uri = url_for('entity', type=result.get('__type__'), key=result.get('__key__'), _external=True) matches.append({ 'name': result.get('title'), 'score': result.get('score') * 10, 'type': [{ 'id': '/' + result.get('__type__'), 'name': result.get('__type__') }], 'id': id, 'uri': uri, 'match': result.get('title') == q.get('query') }) return { 'result': matches, 'num': results.get('response').get('numFound') }
def manager_edit_save(type): type_ = Type.update(type, request.form) return render_template('manage.tmpl', type=type_)
def manager_edit(type): type_ = Type.by_name(type) return render_template('edit.tmpl', type=type_)
def manager_new_save(): type_ = Type.create(request.form) return render_template('manage.tmpl', type=type_)
def manage(type): type_ = Type.by_name(type) return render_template('manage.tmpl', type=type_)
def manager(): types = Type.types() return render_template('manager.tmpl', types=types)