def search_all_services_with_specific_thematic(thematic): all_indexed_data = [] for thematics_genre in all_genre_of_different_artist[thematic]: indexed_data = es.search( index="services", body={ "from": 0, "size": 50, "query": { "bool": { "filter": { "query_string": { "fields": ["thematics"], "query": thematics_genre } } } } } ) all_indexed_data += [indexed_data] all_indexed_data = indexed_filter(all_indexed_data, []) return custom_response(add_others_information_to_list_of_service(all_indexed_data), 200)
def get_top_beats(listed=False): """ top beat at the three last month """ last_3_month_date = datetime.datetime.now() + relativedelta(months=-3) beats_and_artist = es.search( index="beats", body={ "from": 0, "size": 10, "query": { "range": { "created_at": { "gte": last_3_month_date, "lte": "now" } } }, # "sort": [ # {"listened": "desc"}, # {"admire": "desc"}, # {"share": "desc"} # ] }) beats = [] for k in beats_and_artist['hits']['hits']: beats.append(k['_source']) if listed: return beats return custom_response({'top_beats': beats}, 200)
def add_others_information_to_list_of_service(all_indexed_data): new_list_of_all_indexed_data = [] for service in all_indexed_data: material = es.search( index="materials", body={ "query": { "bool": { "must": [{ "match": { "id": service["materials_id"] } }] } }} ) option = es.search( index="options", body={ "query": { "bool": { "must": [{ "match": { "user_id": service["user_id"] } }], "filter": { "query_string": { "fields": ["services_id_list"], "query": service["id"] } }} } } ) service["materials"] = material['hits']['hits'][0]["_source"] service["artist_name"] = User.get_one_user(service["user_id"]).name service["notes"] = check_service_stars(service["id"]) service["options"] = [d["_source"] for d in option['hits']['hits']] new_list_of_all_indexed_data.append(service) return sorted(new_list_of_all_indexed_data, key=itemgetter('price'))
def indexation(options, query_string=None): args_to_search = dict(must=options) if query_string: args_to_search['filter'] = query_string return es.search( index="services", body={ "from": 0, "size": 50, "query": { "bool": args_to_search } } )
def get_random_beats(listed=False): """ get 10 beats random in DB """ beats_and_artist = es.search(index="beats", body={ "from": 0, "size": 20, "query": { "match_all": {} } }) all_beats = [] for k in beats_and_artist['hits']['hits']: all_beats.append(k['_source']) if listed: return all_beats return custom_response({"random": all_beats}, 200)
def check_key_value_match(_query, index="beats"): """ Function to find into elastic key value match @param _query: dict type with {"match": {"column_name": value_name}} or list type @param index: index for songs. default value is beats """ return es.search(index=index, body={ "from": 0, "size": 20, "query": { "bool": { "must": { "bool": { "should": _query } } } } })
def search_all_services_with_specific_event(event): indexed_data = es.search( index="services", body={ "from": 0, "size": 50, "query": { "bool": { "filter": { "query_string": { "fields": ["events"], "query": event } } } } } ) all_indexed_data = [d["_source"] for d in indexed_data['hits']['hits']] return custom_response(add_others_information_to_list_of_service(all_indexed_data), 200)
def all_user_beats(user_connected_model, user_connected_schema): """ return all user beats """ beats = es.search( index="beats", doc_type="songs", body={ "query": { "bool": { "must": [ {"match": {"user_id": user_connected_model.id}} ] } } } ) _all_beats = [] for beat in beats['hits']['hits']: _all_beats.append(check_dict_keys(beat['_source'], ['wave', 'stems'], True)) return custom_response({"beats": _all_beats}, 200)
def get_beat_by_id(beat_id): """ """ beat = es.search( index="beats", doc_type="songs", body={"query": { "bool": { "must": [{ "match": { "id": beat_id } }] } }}) try: return custom_response( check_dict_keys(beat['hits']['hits'][0]['_source'], ['wave', 'stems'], True), 200) except IndexError: return custom_response("beat not found or deleted", 400)
def document_delete(index, doc_type, first_, second_, ref=False): """ Function to delete a specific document. """ all_option_for_search = [{"match": first_}] if second_[list(second_)[0]]: all_option_for_search.append({"match": second_}) resp_fetched = es.search( index=index, doc_type=doc_type, body={"query": { "bool": { "must": all_option_for_search } }}) if ref: return resp_fetched es.delete(index=index, doc_type=doc_type, id=resp_fetched['hits']['hits'][0]['_id'])
def search_beats_and_artist(string_to_search): """ search matching with text_ in table medias """ beats_and_artist = es.search( index="beats", doc_type="songs", body={ "from": 0, "size": 10, "query": { "query_string": { "fields": ["title", "artist", "genre", "artist_tag", "description"], "query": "*" + string_to_search + "*" } }, # "sort": [{"listened": {"order": "asc", "mode": "min"}}] }) data = {'beats': [], 'artists': []} for r in beats_and_artist['hits']['hits']: data['beats'].append(r['_source']) artist_profile = profile_schema.dump( User.get_one_user(r['_source']['user_id']).profile) keys_to_remove = [ 'address', 'age', 'city', 'region', 'social_id', 'photo', 'email', 'cover_photo', 'photo' ] artist_profile = check_dict_keys(artist_profile, _keys=keys_to_remove, inverse=True) if artist_profile not in data['artists']: data['artists'].append(artist_profile) return custom_response(data, 200)