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 }
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
def query(args): """ Parse a user query string, compose and execute a query. """ if not isinstance(args, MultiDict): args = MultiDict(args) q = text_query(args.get('q', '')) q = authz_filter(q) # Extract filters, given in the form: &filter:foo_field=bla_value filters = [] for key in args.keys(): for value in args.getlist(key): if not key.startswith('filter:'): continue _, field = key.split(':', 1) filters.append((field, value)) facets = args.getlist('facet') aggs = aggregate(q, facets, filters) sort = ['_score'] if args.get('sort') == 'linkcount': sort.insert(0, {'$linkcount': 'desc'}) q = { 'sort': sort, 'query': filter_query(q, filters), 'aggregations': aggs, '_source': DEFAULT_FIELDS } if args.get('hl'): q["highlight"] = { "pre_tags": ["<em>"], "post_tags": ["</em>"], "fields": { "$text": {} } } q = paginate(q, args.get('limit'), args.get('offset')) return execute_query(args, q, facets)
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}