def test_pinned_and_tagged(self): # Verify that pin bubbling logic still works with addtional query rules. content = self.make_content(4) tag = Tag.objects.create(name='Joe Biden') # Only 2 contents tagged, one is pinned for i in [1, 3]: content[i].tags = [tag] content[i].save() Content.search_objects.refresh() # A tag query query = dict( groups=[ {'conditions': [ {'values': [{'value': u'joe-biden', 'label': u'joe-biden'}], 'type': 'all', 'field': 'tag'} ]} ], pinned_ids=[content[3].id], ) q = custom_search_model(Content, query, field_map=self.field_map).full() sorted_ids = [content[3].id, content[1].id] self.assertEqual(sorted_ids, [c.id for c in q])
def get_recirc_content(self, published=True, count=3): """gets the first 3 content objects in the `included_ids` """ query = self.get_query() # check if query has included_ids & if there are any ids in it, # in case the ids have been removed from the array if not query.get('included_ids'): qs = Content.search_objects.search() qs = qs.query( TagBoost(slugs=self.tags.values_list("slug", flat=True)) ).filter( ~Ids(values=[self.id]) ).sort( "_score" ) return qs[:count] # NOTE: set included_ids to just be the first 3 ids, # otherwise search will return last 3 items query['included_ids'] = query['included_ids'][:count] search = custom_search_model(Content, query, published=published, field_map={ "feature_type": "feature_type.slug", "tag": "tags.slug", "content-type": "_type" }) return search
def get_recirc_content(self, published=True, count=3): """gets the first 3 content objects in the `included_ids` """ query = self.get_query() # check if query has included_ids & if there are any ids in it, # in case the ids have been removed from the array if not query.get('included_ids'): qs = Content.search_objects.search() qs = qs.query( TagBoost(slugs=self.tags.values_list("slug", flat=True)) ).filter(~Ids(values=[self.id])).sort("_score") return qs[:count] # NOTE: set included_ids to just be the first 3 ids, # otherwise search will return last 3 items query['included_ids'] = query['included_ids'][:count] search = custom_search_model(Content, query, published=published, field_map={ "feature_type": "feature_type.slug", "tag": "tags.slug", "content-type": "_type" }) return search
def get_custom_search_queryset(self, query): qs = custom_search_model(self.model, query, preview=self.is_preview, published=self.is_published, field_map=self.field_map) return qs
def get_filtered_queryset(self, params, sort_pinned=True): query = params is_preview = params.get("preview", True) qs = custom_search_model( self.model, query, preview=is_preview, sort_pinned=sort_pinned, field_map=self.field_map ) return qs.full()
def test_two_pinned(self): content = self.make_content(4) pinned_ids = [content[i].id for i in [1, 3]] query = dict( pinned_ids=pinned_ids, ) q = custom_search_model(Content, query, field_map=self.field_map).full() # First two pinned bubble to top (sorted by published), then other two sorted by published self.assertSequenceEqual(set([content[i].id for i in [3, 1]]), set([c.id for c in q[:2]]))
def get_filtered_queryset(self, params, sort_pinned=True): query = params is_preview = params.get("preview", True) qs = custom_search_model(self.model, query, preview=is_preview, sort_pinned=sort_pinned, field_map=self.field_map) return qs.full()
def get_full_recirc_content(self, published=True): """performs es search and gets all content objects """ q = self.get_query() search = custom_search_model(Content, q, published=published, field_map={ "feature_type": "feature_type.slug", "tag": "tags.slug", "content-type": "_type" }) return search
def test_chain_additional_query(self): # Ensure that custom_search_model() queries can chain additional queries. # This was originally broken when custom_search_model() used a query_raw() to bubble pinned # IDs to the top. content = self.make_content(4) query = dict( pinned_ids=[content[3].id] ) q = custom_search_model(Content, query, field_map=self.field_map) # Simulate SpecialCoverage behavior to get all content except first pinned result self.assertEqual(content[3].id, q.query('ids', values=[content[3].id])[0].id)
def test_one_pinned(self): content = self.make_content(4) pinned = content[-1] query = dict( pinned_ids=[content[-1].id], ) q = custom_search_model(Content, query, field_map=self.field_map).full() self.assertEqual(len(content), q.count()) # Sorted by pinned, then published sorted_ids = [c.id for c in [pinned] + content[:-1]] self.assertEqual(sorted_ids, [c.id for c in q])
def get_content(self): """performs es search and gets content objects """ if "query" in self.query: q = self.query["query"] else: q = self.query search = custom_search_model(Content, q, field_map={ "feature-type": "feature_type.slug", "tag": "tags.slug", "content-type": "_type", }) return search
def get_custom_search_queryset(self, query): qs = custom_search_model( self.model, query, preview=self.is_preview, published=self.is_published, field_map=self.field_map ) return qs
def check_filtered_count(self, query, expected_count): qs = custom_search_model(Content, query, field_map=self.field_map) self.assertEqual(qs.count(), expected_count) results = qs.execute() self.assertEqual(len(results), expected_count)
def check_preview_filter_count(self, query, expected_count): qs = custom_search_model(Content, query, preview=True, field_map=self.field_map) self.assertEqual(qs.count(), expected_count)
def check_ordering(self, query, ids): qs = custom_search_model(Content, query, field_map=self.field_map) self.assertSequenceEqual([c.id for c in qs[:len(ids)]], ids)