forked from deezer/cover_song_detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathes_search.py
303 lines (251 loc) · 12.1 KB
/
es_search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# -*- coding: utf-8 -*-
"""
Set of functions and methods for various search requests to dzr_elastic search augmented msd db
Albin Andrew Correya
R&D Intern
@Deezer,2017
"""
from requests import get
from copy import deepcopy
import templates as presets
class SearchModule(object):
"""
Class containing custom methods to search the elasticsearch index containing the augmented MSD dataset
"""
from elasticsearch import Elasticsearch
import pandas as pd
import json
init_json = deepcopy(presets.simple_query_string) # save the preset as attribute
def __init__(self, uri_config, query_json=None, timeout=30):
"""
Init params:
uri_config : uri_config dictionary specifying the host and port of es db.
(check 'uri_config' in the templates.py file)
query_json : {default : None}
"""
self.config = uri_config
self.handler = self.Elasticsearch(hosts=[{'host': self.config['host'],
'port': self.config['port'],
'scheme': self.config['scheme']}], timeout=timeout)
if query_json:
self.post_json = query_json
else:
self.post_json = presets.simple_query_string
return
def _load_json(self, jsonfile):
"""Load a json file as python dict"""
with open(jsonfile) as f:
json_data = self.json.load(f)
return json_data
def _make_request(self, target_url, query, verbose=False):
"""
[DEPRECIATED] make the request and fetch results
"""
if verbose:
print "GET %s -d '%s'" % (target_url, self.json.dumps(query))
r = get(target_url, data=self.json.dumps(query))
return self.json.loads(r.text)
def _format_url(self, msd_id):
return "%s://%s:%s/%s/%s/%s" % (
self.config['scheme'],
self.config['host'],
self.config['port'],
self.config['index'],
self.config['type'],
msd_id
)
def _format_query(self, query_str, msd_id, mode='simple_query', field='msd_title', size=100):
"""
Format POST json dict object with query_str and msd_id
"""
if mode == 'simple_query':
self.post_json['query']['bool']['must'][0]['simple_query_string']['query'] = query_str
self.post_json['query']['bool']['must'][0]['simple_query_string']['fields'][0] = field
# we exclude the query id from the result
self.post_json['query']['bool']['must_not'][0]['query_string']['query'] = msd_id
self.post_json['size'] = size
if mode == 'query_string':
self.post_json['query']['bool']['must'][0]['query_string']['query'] = query_str
# we exclude the query id from the result
self.post_json['query']['bool']['must_not'][0]['query_string']['query'] = msd_id
self.post_json['size'] = size
return self.post_json
@staticmethod
def _format_init_json(init_json, query_str, msd_id, field='msd_title', size=100):
"""
"""
init_json['query']['bool']['must'][0]['simple_query_string']['query'] = query_str
init_json['query']['bool']['must'][0]['simple_query_string']['fields'][0] = field
# we exclude the query id from the result
init_json['query']['bool']['must_not'][0]['query_string']['query'] = msd_id
init_json['size'] = size
return init_json
@staticmethod
def _parse_response_for_eval(response):
"""
Parse list of msd_track_ids and their respective scores from a search response json
Input :
response : json response from the elasticsearch
"""
msd_ids = [d['_id'] for d in response]
scores = [d['_score'] for d in response]
return msd_ids, scores
def _view_response(self, response):
"""
Aggregrate response as pandas dataframe to view response as tables in the ipython console
Input :
response : json response from the elasticsearch
Output : A pandas dataframe with aggregrated results
"""
row_list = [(track['_id'], track['_score'], track['_source']['msd_title']) for track in response]
results = self.pd.DataFrame({
'msd_id': [r[0] for r in row_list],
'score': [r[1] for r in row_list],
'msd_title': [r[2] for r in row_list]
})
return results
def format_lyrics_post_json(self, body, lyrics, track_id, size, field='dzr_lyrics.content'):
"""
format post_json template for lyrics search with lyrics and msd_track-id
"""
self.post_json = body
self.post_json['query']['bool']['must'][0]['more_like_this']['like'] = lyrics
self.post_json['query']['bool']['must'][0]['more_like_this']['fields'][0] = field
# we exclude the query id from the results
self.post_json['query']['bool']['must_not'][0]['query_string']['query'] = track_id
self.post_json['size'] = size
def limit_post_json_to_shs(self):
"""
Limits search only on the songs present in the second hand songs train set as provided by labrosa
ie. limit search to 1 x 12960 from 1 x 1M
"""
if len(self.post_json['query']['bool']['must']) <= 1: # mar: why this condition?
self.post_json['query']['bool']['must'].append({'exists': {'field': 'shs_id'}})
def limit_to_dzr_mapped_msd(self):
"""
Limits search only on the songs have a respective mapping to the deezer_song_ids
ie. limit search to 1 x ~83k
"""
self.post_json['query']['bool']['must'].append({'exists': {'field': 'dzr_song_title'}})
def add_remove_duplicates_filter(self):
"""
Filter songs with field 'msd_is_duplicate_of' from the search
and response using must_not exist method in the post-request
"""
if len(self.post_json['query']['bool']['must_not']) <= 1: # mar: why this condition?
self.post_json['query']['bool']['must_not'].append({'exists': {'field': 'msd_is_duplicate_of'}})
@staticmethod
def add_must_field_to_query_dsl(post_json, role_type='Composer', field='dzr_artists.role_name',
query_type='simple_query_string'):
post_json['query']['bool']['must'].append({query_type: {'fields': [field], 'query': role_type}})
return post_json # mar: why here we return something and not the previous add_ function
@staticmethod
def add_role_artists_to_query_dsl(post_json, artist_names, field='dzr_artists.artist_name',
query_type='simple_query_string'):
if len(artist_names) > 1:
query_str = ' OR '.join(artist_names)
else:
query_str = artist_names[0]
post_json['query']['bool']['must'].append({query_type: {'fields': [field], 'query': query_str}})
return post_json # mar: why here we return something and not the previous add_ function
@staticmethod
def parse_field_from_response(response, field='msd_artist_id'):
"""
Parse a particular field value from the es response
:param response: es response json
:param field: field_name
"""
if field not in response['_source'].keys():
return None
elif not response['_source'][field]:
return None
elif field == 'dzr_lyrics':
return response['_source'][field]['content']
else:
return response['_source'][field]
def get_field_info_from_id(self, msd_id, field):
"""
Retrieve info for a particular field associated to a msd_id in the es db
eg. get_field_info_from_id(msd_id='TRWFERO128F425FE0D', field='dzr_lyrics.content')
"""
response = get(self._format_url(msd_id))
field_info = self.parse_field_from_response(response.json(), field=field)
return field_info
def get_mxm_lyrics_by_id(self, track_id):
"""
Get Musixmatch lyrics associated with a msd track id from es index if there is any.
:param track_id: msd track id
:return:
"""
return self.get_field_info_from_id(msd_id=track_id, field='mxm_lyrics')
def get_cleaned_title_from_id(self, msd_id, field="dzr_msd_title_clean"):
"""
Get preprocessed MSD title by MSD track id
"""
# mar: the field "dzr_msd_title_clean" should not be a parameter (like in get_mxm_lyrics)
response = get(self._format_url(msd_id))
return self.parse_field_from_response(response.json(), field=field)
def search_es(self, body):
"""
Make a search request to elasticsearch provided by json POST dictionary
[This is a general method you can use for querying the es db with respective query_dsl as inputs]
Input :
body : JSON post dict for elastic search
(you can use the template jsons in the templates.py script)
eg : body = templates.simple_query_string
"""
res = self.handler.search(index=self.config["index"], body=body)
return res['hits']['hits']
def search_by_exact_title(self, track_title, track_id, mode='simple_query', out_mode='view', size=100):
"""
Search by track_title using simple_query_string method in the elasticsearch
"""
res = self.search_es(self._format_query(query_str=track_title, msd_id=track_id, mode=mode, size=size))
# mar: because the following code is copy/pasted several times, it should be a function
# like return_results(res, out_mode)
if out_mode == 'eval':
msd_ids, scores = self._parse_response_for_eval(res)
return msd_ids, scores
if out_mode == 'view':
return self._view_response(res)
return None
def search_with_cleaned_title(self, track_id, out_mode='view', field="dzr_msd_title_clean", size=100):
"""
Search by cleaned msd_track-title
"""
# mar: field="dzr_msd_title_clean" should not ne a parameter but included in get_cleaned_title_from_id
track_title = self.get_cleaned_title_from_id(msd_id=track_id)
res = self.search_es(self._format_query(query_str=track_title, msd_id=track_id, mode='simple_query',
field=field, size=size))
if out_mode == 'eval':
msd_ids, scores = self._parse_response_for_eval(res)
return msd_ids, scores
if out_mode == 'view':
return self._view_response(res)
return None
def search_by_mxm_lyrics(self, post_json, msd_track_id, out_mode='eval', size=100):
"""
Search the es_db by musixmatch_lyrics which are mapped to certain msd_track_ids
These mappings are obtained from the musixmatch dataset (https://labrosa.ee.columbia.edu/millionsong/musixmatch)
[NOTE]: It returns a tuple of list of response msd_track_ids and
response_scores from the elastic search response if the track has corresponding "mxm_lyrics"
otherwise return a tuple of (None, None)
Inputs:
post_json : (dict) Query_DSL json template for the es_query (eg. presets.more_like_lyrics)
msd_track_id : (string) MSD track identifier of the query file
Params :
out_mode : (string) Available modes (['eval', 'view'])
size : (int) size of the required response from es_db
"""
lyrics = self.get_mxm_lyrics_by_id(msd_track_id)
if not lyrics:
return None, None
self.format_lyrics_post_json(body=post_json, track_id=msd_track_id, lyrics=lyrics,
size=size, field='mxm_lyrics')
res = self.search_es(body=self.post_json)
if out_mode == 'eval':
msd_ids, scores = self._parse_response_for_eval(res)
return msd_ids, scores
if out_mode == 'view':
return self._view_response(res)
return None