From 868a5eda2532c78aa4beedbcdaab3887e2b7a090 Mon Sep 17 00:00:00 2001 From: ale <ale@incal.net> Date: Thu, 29 Sep 2011 23:04:32 +0100 Subject: [PATCH] support a rough control of the number of more_like_these results --- server/djrandom/frontend/api_views.py | 3 ++- server/djrandom/frontend/search.py | 4 ++-- server/djrandom/frontend/static/js/djr/backend.js | 4 ++-- server/djrandom/frontend/static/js/djr/player.js | 4 ++-- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/server/djrandom/frontend/api_views.py b/server/djrandom/frontend/api_views.py index 80eb5f0..7b19685 100644 --- a/server/djrandom/frontend/api_views.py +++ b/server/djrandom/frontend/api_views.py @@ -160,9 +160,10 @@ def search_json(): @require_auth def more_like_these_json(): hashes = request.form.get('h', '').split(',') + n = int(request.form.get('n', 5)) results = [] if hashes: - results = svcs['searcher'].more_like_these(hashes) + results = svcs['searcher'].more_like_these(hashes, n) return jsonify(results=results) diff --git a/server/djrandom/frontend/search.py b/server/djrandom/frontend/search.py index 720b631..9f96651 100644 --- a/server/djrandom/frontend/search.py +++ b/server/djrandom/frontend/search.py @@ -14,11 +14,11 @@ class Searcher(object): for doc in results.results: yield doc['score'], doc['id'] - def more_like_these(self, hashes): + def more_like_these(self, hashes, n=5): """Return a set of hashes that are related to the ones provided.""" query_str = ' OR '.join('(id:%s)' % x for x in hashes) results = self.solr.select( - q=query_str, fields='score,id', + q=query_str, fields='score,id', rows=n, mlt='true', mlt_fl='text', mlt_mindf=1, mlt_mintf=1) # Parse and uniq-ify the results. diff --git a/server/djrandom/frontend/static/js/djr/backend.js b/server/djrandom/frontend/static/js/djr/backend.js index 490f28b..a499b25 100644 --- a/server/djrandom/frontend/static/js/djr/backend.js +++ b/server/djrandom/frontend/static/js/djr/backend.js @@ -65,10 +65,10 @@ djr.Backend.prototype.search = function(query, callback, ctx) { * @param {function} callback Callback function. * @param {Object} ctx Callback context. */ -djr.Backend.prototype.moreLikeThese = function(uuids, callback, ctx) { +djr.Backend.prototype.moreLikeThese = function(n, uuids, callback, ctx) { wrap_with_loader( {url: '/json/morelikethese', - data: {'h': uuids.join(',')}, + data: {'h': uuids.join(','), 'n': n}, dataType: 'json', type: 'POST', context: ctx || this, diff --git a/server/djrandom/frontend/static/js/djr/player.js b/server/djrandom/frontend/static/js/djr/player.js index 534b445..b463d74 100644 --- a/server/djrandom/frontend/static/js/djr/player.js +++ b/server/djrandom/frontend/static/js/djr/player.js @@ -175,7 +175,7 @@ djr.Player.prototype.extendCurrentPlaylist = function() { var player = this; var cur_songs = this.playlist.allSongs(); - this.backend.moreLikeThese(cur_songs, function(results) { + this.backend.moreLikeThese(5, cur_songs, function(results) { player.createChunk(results, 'suggestions'); }); }; @@ -198,7 +198,7 @@ djr.Player.prototype.moreLikeThis = function(sha1) { var player = this; var songs = [sha1]; - this.backend.moreLikeThese(songs, function(results) { + this.backend.moreLikeThese(10, songs, function(results) { player.createChunk(results, 'like that'); }); }; -- GitLab