def get_artists_by_ids(db, ids, include): if not ids: return {} artists = query_artist(db, include).filter(Artist.id.in_(ids)).all() if include.areas: load_areas(db, artists, include.areas) if include.relationships: load_links(db, artists, include.relationships) results = {} for artist in artists: results[artist.id] = serialize_artist(artist, include) return results
def handle_get(): gid = get_param('id', type='uuid', required=True) include = get_param('include', type='enum+', container=ArtistIncludes.parse) artist = get_artist_by_gid(query_artist(g.db, include), gid) if artist is None: abort(response_error(NOT_FOUND_ERROR, 'artist not found')) if include.areas: load_areas(g.db, [artist], include.areas) if include.relationships: load_links(g.db, [artist], include.relationships) return response_ok(artist=serialize_artist(artist, include))
def handle_search(): query = get_param('query', type='text') include = get_param('include', type='enum+', container=ArtistIncludes.parse) page = get_search_params() options = prepare_search_options(page, fields='name^1.6 sort_name^1.1 alias') search_results = g.solr.select(query, fq='kind:artist', **options) # http://127.0.0.1:8983/solr/musicbrainz/select?q=gender:male&group=true&group.field=kind&group.limit=3&defType=edismax&qf=name&uf=* artist_ids = [] scores = {} for result in search_results: id = int(result['id'].split(':', 1)[1]) artist_ids.append(id) scores[id] = result['score'] artists = query_artist(g.db, include).filter(Artist.id.in_(artist_ids)) artist_by_id = {} for artist in artists: artist_by_id[artist.id] = artist if include.areas: load_areas(g.db, artist_by_id.values(), include.areas) data = [] for id in artist_ids: data.append({ 'score': scores[id], 'artist': serialize_artist(artist_by_id[id], include), }) return response_ok(results=data, page_info=prepare_page_info(search_results, page))