def autocomplete(): video_results = [] playlist_results = [] query = request.request_string("q", default="").strip().lower() if query: max_results_per_type = 10 video_results = filter( lambda video_dict: query in video_dict["title"].lower(), video_title_dicts()) playlist_results = filter( lambda playlist_dict: query in playlist_dict["title"].lower(), playlist_title_dicts()) video_results = sorted( video_results, key=lambda v: v["title"].lower().index(query))[:max_results_per_type] playlist_results = sorted( playlist_results, key=lambda p: p["title"].lower().index(query))[:max_results_per_type] return { "query": query, "videos": video_results, "playlists": playlist_results }
def autocomplete(): video_results = [] playlist_results = [] query = request.request_string("q", default="").strip().lower() if query: max_results_per_type = 10 video_results = filter( lambda video_dict: query in video_dict["title"].lower(), video_title_dicts()) playlist_results = filter( lambda playlist_dict: query in playlist_dict["title"].lower(), playlist_title_dicts()) video_results = sorted(video_results, key=lambda dict: dict["title"].lower().index( query))[:max_results_per_type] playlist_results = sorted(playlist_results, key=lambda dict: dict["title"].lower().index( query))[:max_results_per_type] return { "query": query, "videos": video_results, "playlists": playlist_results }
def get_old(self): """ Deprecated old version of search, so we can Gandalf in the new one. If new search is working, this should be taken out by May 31, 2012. """ show_update = False if App.is_dev_server and user_util.is_current_user_admin(): update = self.request_bool("update", False) if update: self.update() version_number = layer_cache.KeyValueCache.get( "last_dev_topic_vesion_indexed") default_version = topic_models.TopicVersion.get_default_version() if version_number != default_version.number: show_update = True query = self.request.get('page_search_query') template_values = {'page_search_query': query} query = query.strip() if len(query) < search.SEARCH_PHRASE_MIN_LENGTH: if len(query) > 0: template_values.update( {'query_too_short': search.SEARCH_PHRASE_MIN_LENGTH}) self.render_jinja2_template("searchresults.html", template_values) return searched_phrases = [] # Do an async query for all ExerciseVideos, since this may be slow exvids_query = ExerciseVideo.all() exvids_future = util.async_queries([exvids_query]) # One full (non-partial) search, then sort by kind all_text_keys = Topic.full_text_search( query, limit=50, kind=None, stemming=Topic.INDEX_STEMMING, multi_word_literal=Topic.INDEX_MULTI_WORD, searched_phrases_out=searched_phrases) # Quick title-only partial search topic_partial_results = filter( lambda topic_dict: query in topic_dict["title"].lower(), autocomplete.topic_title_dicts()) video_partial_results = filter( lambda video_dict: query in video_dict["title"].lower(), autocomplete.video_title_dicts()) url_partial_results = filter( lambda url_dict: query in url_dict["title"].lower(), autocomplete.url_title_dicts()) # Combine results & do one big get! all_keys = [str(key_and_title[0]) for key_and_title in all_text_keys] all_keys.extend([result["key"] for result in topic_partial_results]) all_keys.extend([result["key"] for result in video_partial_results]) all_keys.extend([result["key"] for result in url_partial_results]) all_keys = list(set(all_keys)) # Filter out anything that isn't a Topic, Url or Video all_keys = [ key for key in all_keys if db.Key(key).kind() in ["Topic", "Url", "Video"] ] # Get all the entities all_entities = db.get(all_keys) # Group results by type topics = [] videos = [] for entity in all_entities: if isinstance(entity, Topic): topics.append(entity) elif isinstance(entity, Video): videos.append(entity) elif isinstance(entity, Url): videos.append(entity) elif entity: logging.info("Found unknown object " + repr(entity)) # Get topics for videos not in matching topics filtered_videos = [] filtered_videos_by_key = {} for video in videos: if [(str(topic.key()) in video.topic_string_keys) for topic in topics].count(True) == 0: video_topic = video.first_topic() if video_topic != None: topics.append(video_topic) filtered_videos.append(video) filtered_videos_by_key[str(video.key())] = [] else: filtered_videos.append(video) filtered_videos_by_key[str(video.key())] = [] video_count = len(filtered_videos) # Get the related exercises all_exercise_videos = exvids_future[0].get_result() exercise_keys = [] for exvid in all_exercise_videos: video_key = str(ExerciseVideo.video.get_value_for_datastore(exvid)) if video_key in filtered_videos_by_key: exercise_key = ExerciseVideo.exercise.get_value_for_datastore( exvid) video_exercise_keys = filtered_videos_by_key[video_key] video_exercise_keys.append(exercise_key) exercise_keys.append(exercise_key) exercises = db.get(exercise_keys) # Sort exercises with videos video_exercises = {} for video_key, exercise_keys in filtered_videos_by_key.iteritems(): video_exercises[video_key] = map( lambda exkey: [ exercise for exercise in exercises if exercise.key() == exkey ][0], exercise_keys) # Count number of videos in each topic and sort descending topic_count = 0 matching_topic_count = 0 if topics: if len(filtered_videos) > 0: for topic in topics: topic.match_count = [ (str(topic.key()) in video.topic_string_keys) for video in filtered_videos ].count(True) if topic.match_count > 0: topic_count += 1 topics = sorted(topics, key=lambda topic: topic.match_count, reverse=True) else: for topic in topics: topic.match_count = 0 for topic in topics: if topic.title.lower() == query: topic.matches = True matching_topic_count += 1 child_topics = topic.get_child_topics( include_descendants=True) topic.child_topics = [ t for t in child_topics if t.has_content() ] template_values.update({ 'show_update': show_update, 'topics': topics, 'videos': filtered_videos, 'video_exercises': video_exercises, 'search_string': query, 'video_count': video_count, 'topic_count': topic_count, 'matching_topic_count': matching_topic_count }) self.render_jinja2_template("searchresults.html", template_values)
def get_old(self): """ Deprecated old version of search, so we can Gandalf in the new one. If new search is working, this should be taken out by May 31, 2012. """ show_update = False if App.is_dev_server and user_util.is_current_user_admin(): update = self.request_bool("update", False) if update: self.update() version_number = layer_cache.KeyValueCache.get( "last_dev_topic_vesion_indexed") default_version = topic_models.TopicVersion.get_default_version() if version_number != default_version.number: show_update = True query = self.request.get('page_search_query') template_values = {'page_search_query': query} query = query.strip() if len(query) < search.SEARCH_PHRASE_MIN_LENGTH: if len(query) > 0: template_values.update({ 'query_too_short': search.SEARCH_PHRASE_MIN_LENGTH }) self.render_jinja2_template("searchresults.html", template_values) return searched_phrases = [] # Do an async query for all ExerciseVideos, since this may be slow exvids_query = ExerciseVideo.all() exvids_future = util.async_queries([exvids_query]) # One full (non-partial) search, then sort by kind all_text_keys = Topic.full_text_search( query, limit=50, kind=None, stemming=Topic.INDEX_STEMMING, multi_word_literal=Topic.INDEX_MULTI_WORD, searched_phrases_out=searched_phrases) # Quick title-only partial search topic_partial_results = filter( lambda topic_dict: query in topic_dict["title"].lower(), autocomplete.topic_title_dicts()) video_partial_results = filter( lambda video_dict: query in video_dict["title"].lower(), autocomplete.video_title_dicts()) url_partial_results = filter( lambda url_dict: query in url_dict["title"].lower(), autocomplete.url_title_dicts()) # Combine results & do one big get! all_keys = [str(key_and_title[0]) for key_and_title in all_text_keys] all_keys.extend([result["key"] for result in topic_partial_results]) all_keys.extend([result["key"] for result in video_partial_results]) all_keys.extend([result["key"] for result in url_partial_results]) all_keys = list(set(all_keys)) # Filter out anything that isn't a Topic, Url or Video all_keys = [key for key in all_keys if db.Key(key).kind() in ["Topic", "Url", "Video"]] # Get all the entities all_entities = db.get(all_keys) # Group results by type topics = [] videos = [] for entity in all_entities: if isinstance(entity, Topic): topics.append(entity) elif isinstance(entity, Video): videos.append(entity) elif isinstance(entity, Url): videos.append(entity) elif entity: logging.info("Found unknown object " + repr(entity)) # Get topics for videos not in matching topics filtered_videos = [] filtered_videos_by_key = {} for video in videos: if [(str(topic.key()) in video.topic_string_keys) for topic in topics].count(True) == 0: video_topic = video.first_topic() if video_topic != None: topics.append(video_topic) filtered_videos.append(video) filtered_videos_by_key[str(video.key())] = [] else: filtered_videos.append(video) filtered_videos_by_key[str(video.key())] = [] video_count = len(filtered_videos) # Get the related exercises all_exercise_videos = exvids_future[0].get_result() exercise_keys = [] for exvid in all_exercise_videos: video_key = str(ExerciseVideo.video.get_value_for_datastore(exvid)) if video_key in filtered_videos_by_key: exercise_key = ExerciseVideo.exercise.get_value_for_datastore( exvid) video_exercise_keys = filtered_videos_by_key[video_key] video_exercise_keys.append(exercise_key) exercise_keys.append(exercise_key) exercises = db.get(exercise_keys) # Sort exercises with videos video_exercises = {} for video_key, exercise_keys in filtered_videos_by_key.iteritems(): video_exercises[video_key] = map( lambda exkey: [exercise for exercise in exercises if exercise.key() == exkey][0], exercise_keys) # Count number of videos in each topic and sort descending topic_count = 0 matching_topic_count = 0 if topics: if len(filtered_videos) > 0: for topic in topics: topic.match_count = [ (str(topic.key()) in video.topic_string_keys) for video in filtered_videos].count(True) if topic.match_count > 0: topic_count += 1 topics = sorted(topics, key=lambda topic: topic.match_count, reverse=True) else: for topic in topics: topic.match_count = 0 for topic in topics: if topic.title.lower() == query: topic.matches = True matching_topic_count += 1 child_topics = topic.get_child_topics( include_descendants=True) topic.child_topics = [t for t in child_topics if t.has_content()] template_values.update({ 'show_update': show_update, 'topics': topics, 'videos': filtered_videos, 'video_exercises': video_exercises, 'search_string': query, 'video_count': video_count, 'topic_count': topic_count, 'matching_topic_count': matching_topic_count }) self.render_jinja2_template("searchresults.html", template_values)
def get(self): query = self.request.get('page_search_query') template_values = {'page_search_query': query} query = query.strip() if len(query) < search.SEARCH_PHRASE_MIN_LENGTH: if len(query) > 0: template_values.update({ 'query_too_short': search.SEARCH_PHRASE_MIN_LENGTH }) self.render_jinja2_template("searchresults.html", template_values) return searched_phrases = [] # Do an async query for all ExerciseVideos, since this may be slow exvids_query = ExerciseVideo.all() exvids_future = util.async_queries([exvids_query]) # One full (non-partial) search, then sort by kind all_text_keys = Playlist.full_text_search( query, limit=50, kind=None, stemming=Playlist.INDEX_STEMMING, multi_word_literal=Playlist.INDEX_MULTI_WORD, searched_phrases_out=searched_phrases) # Quick title-only partial search playlist_partial_results = filter( lambda playlist_dict: query in playlist_dict["title"].lower(), autocomplete.playlist_title_dicts()) video_partial_results = filter( lambda video_dict: query in video_dict["title"].lower(), autocomplete.video_title_dicts()) # Combine results & do one big get! all_key_list = [str(key_and_title[0]) for key_and_title in all_text_keys] #all_key_list.extend([result["key"] for result in playlist_partial_results]) all_key_list.extend([result["key"] for result in video_partial_results]) all_key_list = list(set(all_key_list)) all_entities = db.get(all_key_list) # Filter results by type playlists = [] videos = [] for entity in all_entities: if isinstance(entity, Playlist): playlists.append(entity) elif isinstance(entity, Video): videos.append(entity) elif entity is not None: logging.error("Unhandled kind in search results: " + str(type(entity))) playlist_count = len(playlists) # Get playlists for videos not in matching playlists filtered_videos = [] filtered_videos_by_key = {} for video in videos: if [(playlist.title in video.playlists) for playlist in playlists].count(True) == 0: video_playlist = video.first_playlist() if video_playlist != None: playlists.append(video_playlist) filtered_videos.append(video) filtered_videos_by_key[str(video.key())] = [] else: filtered_videos.append(video) filtered_videos_by_key[str(video.key())] = [] video_count = len(filtered_videos) # Get the related exercises all_exercise_videos = exvids_future[0].get_result() exercise_keys = [] for exvid in all_exercise_videos: video_key = str(ExerciseVideo.video.get_value_for_datastore(exvid)) if video_key in filtered_videos_by_key: exercise_key = ExerciseVideo.exercise.get_value_for_datastore(exvid) video_exercise_keys = filtered_videos_by_key[video_key] video_exercise_keys.append(exercise_key) exercise_keys.append(exercise_key) exercises = db.get(exercise_keys) # Sort exercises with videos video_exercises = {} for video_key, exercise_keys in filtered_videos_by_key.iteritems(): video_exercises[video_key] = map(lambda exkey: [exercise for exercise in exercises if exercise.key() == exkey][0], exercise_keys) # Count number of videos in each playlist and sort descending for playlist in playlists: if len(filtered_videos) > 0: playlist.match_count = [(playlist.title in video.playlists) for video in filtered_videos].count(True) else: playlist.match_count = 0 playlists = sorted(playlists, key=lambda playlist: -playlist.match_count) template_values.update({ 'playlists': playlists, 'videos': filtered_videos, 'video_exercises': video_exercises, 'search_string': query, 'video_count': video_count, 'playlist_count': playlist_count, }) self.render_jinja2_template("searchresults.html", template_values)
def get(self): query = self.request.get('page_search_query') template_values = {'page_search_query': query} query = query.strip() if len(query) < search.SEARCH_PHRASE_MIN_LENGTH: if len(query) > 0: template_values.update({ 'query_too_short': search.SEARCH_PHRASE_MIN_LENGTH }) self.render_jinja2_template("searchresults.html", template_values) return # Do an async query for all ExerciseVideos, since this may be slow exvids_query = ExerciseVideo.all() exvids_future = util.async_queries([exvids_query]) # One full (non-partial) search, then sort by kind all_text_keys = Topic.full_text_search( query, limit=50, kind=None, stemming=Topic.INDEX_STEMMING, multi_word_literal=Topic.INDEX_MULTI_WORD) # Quick title-only partial search topic_partial_results = filter( lambda topic_dict: query in topic_dict["title"].lower(), autocomplete.topic_title_dicts()) video_partial_results = filter( lambda video_dict: query in video_dict["title"].lower(), autocomplete.video_title_dicts()) url_partial_results = filter( lambda url_dict: query in url_dict["title"].lower(), autocomplete.url_title_dicts()) # Combine results & do one big get! all_key_list = [str(key_and_title[0]) for key_and_title in all_text_keys] # all_key_list.extend([result["key"] for result in topic_partial_results]) all_key_list.extend([result["key"] for result in video_partial_results]) all_key_list.extend([result["key"] for result in url_partial_results]) all_key_list = list(set(all_key_list)) # Filter out anything that isn't a Topic, Url or Video all_key_list = [key for key in all_key_list if db.Key(key).kind() in ["Topic", "Url", "Video"]] # Get all the entities all_entities = db.get(all_key_list) # Group results by type topics = [] videos = [] for entity in all_entities: if isinstance(entity, Topic): topics.append(entity) elif isinstance(entity, Video): videos.append(entity) elif isinstance(entity, Url): videos.append(entity) elif entity: logging.info("Found unknown object " + repr(entity)) topic_count = len(topics) # Get topics for videos not in matching topics filtered_videos = [] filtered_videos_by_key = {} for video in videos: if [(str(topic.key()) in video.topic_string_keys) for topic in topics].count(True) == 0: video_topic = video.first_topic() if video_topic != None: topics.append(video_topic) filtered_videos.append(video) filtered_videos_by_key[str(video.key())] = [] else: filtered_videos.append(video) filtered_videos_by_key[str(video.key())] = [] video_count = len(filtered_videos) # Get the related exercises all_exercise_videos = exvids_future[0].get_result() exercise_keys = [] for exvid in all_exercise_videos: video_key = str(ExerciseVideo.video.get_value_for_datastore(exvid)) if video_key in filtered_videos_by_key: exercise_key = ExerciseVideo.exercise.get_value_for_datastore(exvid) video_exercise_keys = filtered_videos_by_key[video_key] video_exercise_keys.append(exercise_key) exercise_keys.append(exercise_key) exercises = db.get(exercise_keys) # Sort exercises with videos video_exercises = {} for video_key, exercise_keys in filtered_videos_by_key.iteritems(): video_exercises[video_key] = map(lambda exkey: [exercise for exercise in exercises if exercise.key() == exkey][0], exercise_keys) # Count number of videos in each topic and sort descending if topics: if len(filtered_videos) > 0: for topic in topics: topic.match_count = [(str(topic.key()) in video.topic_string_keys) for video in filtered_videos].count(True) topics = sorted(topics, key=lambda topic: -topic.match_count) else: for topic in topics: topic.match_count = 0 template_values.update({ 'topics': topics, 'videos': filtered_videos, 'video_exercises': video_exercises, 'search_string': query, 'video_count': video_count, 'topic_count': topic_count, }) self.render_jinja2_template("searchresults.html", template_values)
def get(self): query = self.request.get('page_search_query') template_values = {'page_search_query': query} query = query.strip() if len(query) < search.SEARCH_PHRASE_MIN_LENGTH: if len(query) > 0: template_values.update( {'query_too_short': search.SEARCH_PHRASE_MIN_LENGTH}) self.render_jinja2_template("searchresults.html", template_values) return # Do an async query for all ExerciseVideos, since this may be slow exvids_query = ExerciseVideo.all() exvids_future = util.async_queries([exvids_query]) # One full (non-partial) search, then sort by kind all_text_keys = Topic.full_text_search( query, limit=50, kind=None, stemming=Topic.INDEX_STEMMING, multi_word_literal=Topic.INDEX_MULTI_WORD) # Quick title-only partial search topic_partial_results = filter( lambda topic_dict: query in topic_dict["title"].lower(), autocomplete.topic_title_dicts()) video_partial_results = filter( lambda video_dict: query in video_dict["title"].lower(), autocomplete.video_title_dicts()) url_partial_results = filter( lambda url_dict: query in url_dict["title"].lower(), autocomplete.url_title_dicts()) # Combine results & do one big get! all_key_list = [ str(key_and_title[0]) for key_and_title in all_text_keys ] # all_key_list.extend([result["key"] for result in topic_partial_results]) all_key_list.extend( [result["key"] for result in video_partial_results]) all_key_list.extend([result["key"] for result in url_partial_results]) all_key_list = list(set(all_key_list)) # Filter out anything that isn't a Topic, Url or Video all_key_list = [ key for key in all_key_list if db.Key(key).kind() in ["Topic", "Url", "Video"] ] # Get all the entities all_entities = db.get(all_key_list) # Group results by type topics = [] videos = [] for entity in all_entities: if isinstance(entity, Topic): topics.append(entity) elif isinstance(entity, Video): videos.append(entity) elif isinstance(entity, Url): videos.append(entity) elif entity: logging.info("Found unknown object " + repr(entity)) topic_count = len(topics) # Get topics for videos not in matching topics filtered_videos = [] filtered_videos_by_key = {} for video in videos: if [(str(topic.key()) in video.topic_string_keys) for topic in topics].count(True) == 0: video_topic = video.first_topic() if video_topic != None: topics.append(video_topic) filtered_videos.append(video) filtered_videos_by_key[str(video.key())] = [] else: filtered_videos.append(video) filtered_videos_by_key[str(video.key())] = [] video_count = len(filtered_videos) # Get the related exercises all_exercise_videos = exvids_future[0].get_result() exercise_keys = [] for exvid in all_exercise_videos: video_key = str(ExerciseVideo.video.get_value_for_datastore(exvid)) if video_key in filtered_videos_by_key: exercise_key = ExerciseVideo.exercise.get_value_for_datastore( exvid) video_exercise_keys = filtered_videos_by_key[video_key] video_exercise_keys.append(exercise_key) exercise_keys.append(exercise_key) exercises = db.get(exercise_keys) # Sort exercises with videos video_exercises = {} for video_key, exercise_keys in filtered_videos_by_key.iteritems(): video_exercises[video_key] = map( lambda exkey: [ exercise for exercise in exercises if exercise.key() == exkey ][0], exercise_keys) # Count number of videos in each topic and sort descending if topics: if len(filtered_videos) > 0: for topic in topics: topic.match_count = [ (str(topic.key()) in video.topic_string_keys) for video in filtered_videos ].count(True) topics = sorted(topics, key=lambda topic: -topic.match_count) else: for topic in topics: topic.match_count = 0 template_values.update({ 'topics': topics, 'videos': filtered_videos, 'video_exercises': video_exercises, 'search_string': query, 'video_count': video_count, 'topic_count': topic_count, }) self.render_jinja2_template("searchresults.html", template_values)