def get_es_feed_query(self, sq, region=mkt.regions.RESTOFWORLD.id, carrier=None, original_region=None): """ Build ES query for feed. Must match region. Orders by FeedItem.order. Boosted operator shelf matching region + carrier. Boosted operator shelf matching original_region + carrier. region -- region ID (integer) carrier -- carrier ID (integer) original_region -- region from before we were falling back, to keep the original shelf atop the RoW feed. """ region_filter = es_filter.Term(region=region) shelf_filter = es_filter.Term(item_type=feed.FEED_TYPE_SHELF) ordering_fn = es_function.FieldValueFactor( field='order', modifier='reciprocal', filter=es_filter.Bool(must=[region_filter], must_not=[shelf_filter])) boost_fn = es_function.BoostFactor(value=10000.0, filter=shelf_filter) if carrier is None: # If no carrier, just match the region and exclude shelves. return sq.query('function_score', functions=[ordering_fn], filter=es_filter.Bool(must=[region_filter], must_not=[shelf_filter])) # Must match region. # But also include the original region if we falling back to RoW. # The only original region feed item that will be included is a shelf # else we wouldn't be falling back in the first place. region_filters = [region_filter] if original_region: region_filters.append(es_filter.Term(region=original_region)) return sq.query( 'function_score', functions=[boost_fn, ordering_fn], filter=es_filter.Bool( should=region_filters, # Filter out shelves that don't match the carrier. must_not=[ es_filter.Bool(must=[shelf_filter], must_not=[es_filter.Term(carrier=carrier)]) ]))
def get_featured_websites(self): """ Get up to 11 featured MOWs for the request's region. If less than 11 are available, make up the difference with globally-featured MOWs. """ REGION_TAG = 'featured-website-%s' % self.request.REGION.slug region_filter = es_filter.Term(tags=REGION_TAG) GLOBAL_TAG = 'featured-website' global_filter = es_filter.Term(tags=GLOBAL_TAG) mow_query = query.Q( 'function_score', filter=es_filter.Bool(should=[region_filter, global_filter]), functions=[ SF('random_score', seed=self._get_daily_seed()), es_function.BoostFactor(value=100.0, filter=region_filter) ], ) es = Search(using=WebsiteIndexer.get_es())[:11] results = es.query(mow_query).execute().hits return ESWebsiteSerializer(results, many=True).data