Skip to content
Snippets Groups Projects
Commit c1e5287d authored by ale's avatar ale
Browse files

add a "more like these" method

parent 689943c7
No related branches found
No related tags found
No related merge requests found
import solr
from collections import defaultdict
from djrandom.model.mp3 import MP3
......@@ -12,3 +13,21 @@ class Searcher(object):
fields='score,id', start=0, rows=n)
for doc in results.results:
yield doc['score'], doc['id']
def more_like_these(self, hashes):
"""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',
mlt='true', mlt_fl='text', mlt_mindf=1, mlt_mintf=1)
# Parse and uniq-ify the results.
hashes_set = set(hashes)
more = defaultdict(lambda: 0)
for doclist in results.moreLikeThis.itervalues():
for doc in doclist:
if doc['id'] not in hashes_set:
more[doc['id']] += doc['score']
results = more.keys()
results.sort(reverse=True, key=lambda x: more[x])
return results
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment