Created
December 22, 2009 10:13
-
-
Save tomdyson/261655 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # export DJANGO_SETTINGS_MODULE=api.settings | |
| from pymongo import Connection, DESCENDING | |
| import re | |
| from datetime import datetime | |
| from songdata.models import Song | |
| connection = Connection() | |
| db = connection['test'] | |
| songs = db.songs | |
| db.songs.ensure_index('terms') | |
| for song in Song.objects.all(): | |
| song_text = '%s %s' % (song.title, song.denormed_artist_name) | |
| if song.search_keywords: | |
| song_text += ' %s' % song.search_keywords | |
| song_terms = song_text.strip().lower().split(' ') | |
| songs.insert({ | |
| 'id': song.id, | |
| 'pop': song.popularity_out_of_ten, | |
| 'title': song.title, | |
| 'terms': song_terms | |
| }) | |
| def mongo_query(query_string, max=0): | |
| print 'searching for: %s' % query_string | |
| then = datetime.now() | |
| query_terms = query_string.strip().split(' ') | |
| if len(query_terms) == 1: | |
| pat = re.compile(r'^%s' % query_string) | |
| q = {'terms': pat} | |
| else: | |
| # regexps currently fail across list members | |
| #q_list = ['/^'+query_term+'/' for query_term in query_terms] | |
| q = SON({'terms': {'$all': query_terms} }) | |
| pop_songs = songs.find(q).sort('pop', DESCENDING).limit(max) | |
| print 'execution time: %s' % (datetime.now() - then) | |
| print 'results: %s' % pop_songs.count() | |
| mongo_query('sunshine') | |
| mongo_query('sunshine of') | |
| mongo_query('david hero') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment