def test_index_excludes_users(self): """Users are both 1) a kind that should never be indexed and 2) a placeholder for all kinds that aren't in config.indexed_models.""" self.admin_api.create('User', email='', auth_id='', last_name='Doe', first_name='John') self.cron.index() result_dicts = [ util.search_document_to_dict(doc) for doc in self.search_index.get_range() ] result_kinds = [Model.get_kind(d['uid']) for d in result_dicts] self.assertNotIn('User', result_kinds)
def search_users(self, params): index = search.Index(config.user_index) page_size = 50 offset = 0 search_results = index.search( search.Query(query_string=self._stringify_search_params(params), )) result_dicts = [ util.search_document_to_dict(doc) for doc in search_results.results ] result_dicts = self._annotate_search_content(result_dicts) return result_dicts
def test_index_includes_assessment(self): """Assessments should be searchable after creation via put hook.""" self.admin_api.create( 'Assessment', name=u'Indexed Assessment', url_name='indexed-assessment', description=u"R\xf8ckin'", num_phases=2, listed=True, ) result_dicts = [ util.search_document_to_dict(doc) for doc in self.search_index.get_range() ] result_kinds = [Model.get_kind(d['uid']) for d in result_dicts] self.assertIn('Assessment', result_kinds)
def search_content(self, params): index = search.Index(config.content_index) search_text = '' # This search_results objects has properties `number_found`, `results`, # and `cursor`. See # https://cloud.google.com/appengine/docs/python/search/searchresultsclass # search.Query docs: # https://cloud.google.com/appengine/docs/python/search/queryclass # search.QueryOptions docs: # https://cloud.google.com/appengine/docs/python/search/queryoptionsclass # Pagination using a 'page' argument pulls n arguments offset by n*page # pull first set of results with page=0 page_size = 20 offset = 0 if 'page' in params: offset = int(params.pop('page')) * page_size # Build the SortOptions with 2 sort keys sort1 = search.SortExpression( expression='promoted', direction=search.SortExpression.DESCENDING, default_value='A') sort2 = search.SortExpression( expression='created', direction=search.SortExpression.DESCENDING, default_value=0) sort_opts = search.SortOptions(expressions=[sort1, sort2]) search_results = index.search( search.Query(query_string=self._stringify_search_params(params), options=search.QueryOptions( limit=page_size, offset=offset, snippeted_fields=['summary', 'body'], sort_options=sort_opts, ))) result_dicts = [ util.search_document_to_dict(doc) for doc in search_results.results ] result_dicts = self._annotate_search_content(result_dicts) return result_dicts
def test_index_includes_practice(self): """Practices should be searchable after creation via put hook.""" self.normal_api.create( 'Practice', name=u'Indexed Practice', summary=u"R\xf8ckin'", tags=['super', u'c\xf8\xf8l', 'tagone'], subjects=['math', 'history', 'reading'], min_grade=0, max_grade=13, type='text', body=u"R\xf8ckin'", youtube_id='https://www.youtube.com/watch?v=6sJqTDaOrTg', has_files=True, pending=False, listed=True, ) result_dicts = [ util.search_document_to_dict(doc) for doc in self.search_index.get_range() ] result_kinds = [Model.get_kind(d['uid']) for d in result_dicts] self.assertIn('Practice', result_kinds)
def test_index_includes_lesson(self): """Lessons should be searchable after creation via put hook.""" self.admin_api.create( 'Lesson', id='indexed-lesson', name=u'Indexed Lesson', summary=u"R\xf8ckin'", tags=['tagone', 'tagtwo'], min_grade=5, max_grade=8, subjects=['reading', 'writing'], json_properties={ 'a': u'\xeb', 'b': [1, 2, 3] }, listed=True, ) result_dicts = [ util.search_document_to_dict(doc) for doc in self.search_index.get_range() ] result_kinds = [Model.get_kind(d['uid']) for d in result_dicts] self.assertIn('Lesson', result_kinds)
def test_index_excludes_unlisted(self): """Content which is normally searchable is not if unlisted.""" self.admin_api.create( 'Lesson', id='unindexed-lesson', name=u'Unindexed Lesson', summary=u"R\xf8ckin'", tags=['tagone', 'tagtwo'], min_grade=5, max_grade=8, subjects=['reading', 'writing'], json_properties={ 'a': u'\xeb', 'b': [1, 2, 3] }, listed=False, ) result_dicts = [ util.search_document_to_dict(doc) for doc in self.search_index.get_range() ] result_kinds = [Model.get_kind(d['uid']) for d in result_dicts] self.assertNotIn('Lesson', result_kinds)
def search_content(self, params): index = search.Index(config.content_index) search_text = '' # This search_results objects has properties `number_found`, `results`, # and `cursor`. See # https://cloud.google.com/appengine/docs/python/search/searchresultsclass # search.Query docs: # https://cloud.google.com/appengine/docs/python/search/queryclass # search.QueryOptions docs: # https://cloud.google.com/appengine/docs/python/search/queryoptionsclass # Pagination using a 'page' argument pulls n arguments offset by n*page # pull first set of results with page=0 page_size = 20 offset = 0 if 'page' in params: offset = int(params.pop('page')) * page_size # Build the SortOptions with 2 sort keys sort1 = search.SortExpression( expression='display_order', direction=search.SortExpression.DESCENDING, default_value=0) sort2 = search.SortExpression( expression='votes_for', direction=search.SortExpression.DESCENDING, default_value=0) sort3 = search.SortExpression( expression='created', direction=search.SortExpression.DESCENDING, default_value=0) sort_opts = search.SortOptions(expressions=[sort1, sort2, sort3]) search_results = index.search( search.Query(query_string=self._stringify_search_params(params), options=search.QueryOptions( limit=page_size, offset=offset, snippeted_fields=['summary', 'body'], sort_options=sort_opts, ))) for result in search_results.results: if 'Chapter_' in result.doc_id: book_id = None for field in result.fields: if field.name == 'books': book_id = field.value if book_id: book = Book.get_by_id(book_id) result.fields.append(search.TextField('bookUID', book.uid)) result.fields.append( search.TextField('bookTitle', book.title)) result.fields.append( search.TextField( 'bookIcon', util.extract_value_from_json(book.icon, 'link'))) result.fields.append( search.TextField( 'chapterNumber', str(book.chapters.index(result.doc_id) + 1))) result_dicts = [ util.search_document_to_dict(doc) for doc in search_results.results ] result_dicts = self._annotate_search_content(result_dicts) return result_dicts