def get_es_feed_element_query(self, sq, feed_items): """ From a list of FeedItems with normalized feed element IDs, return an ES query that fetches the feed elements for each feed item. """ filters = [] for feed_item in feed_items: item_type = feed_item['item_type'] filters.append( es_filter.Bool(must=[ es_filter.Term(id=feed_item[item_type]), es_filter.Term(item_type=item_type) ])) return sq.filter(es_filter.Bool(should=filters))[0:len(feed_items)]
def contributions(self, request, **kwargs): """gets or adds contributions :param request: a WSGI request object :param kwargs: keyword arguments (optional) :return: `rest_framework.response.Response` """ # Check if the contribution app is installed if Contribution not in get_models(): return Response([]) if request.method == "POST": serializer = ContributionSerializer(data=get_request_data(request), many=True) if not serializer.is_valid(): return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) serializer.save() return Response(serializer.data) else: content_pk = kwargs.get('pk', None) if content_pk is None: return Response([], status=status.HTTP_404_NOT_FOUND) queryset = Contribution.search_objects.search().filter( es_filter.Term(**{'content.id': content_pk})) serializer = ContributionSerializer( queryset[:queryset.count()].sort('id'), many=True) return Response(serializer.data)
def get_queryset(self): qs = Contribution.search_objects.search() feature_types = self.request.QUERY_PARAMS.getlist('feature_types') if feature_types: qs = qs.filter( es_filter.Terms( **{'content.feature_type.slug': feature_types})) contributors = self.request.QUERY_PARAMS.getlist('contributors') if contributors: qs = qs.filter( es_filter.Terms(**{'contributor.username': contributors})) tags = self.request.QUERY_PARAMS.getlist('tags') if tags: pass staff = self.request.QUERY_PARAMS.get('staff', None) if staff: is_freelance = True if staff == 'freelance' else False qs = qs.filter( es_filter.Term(**{'contributor.is_freelance': is_freelance})) qs = qs.sort('id') return ESPublishedFilterBackend().filter_queryset( self.request, qs, None)
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: return sq.query('function_score', functions=[ordering_fn], filter=region_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, must_not=[es_filter.Bool( must=[shelf_filter], must_not=[es_filter.Term(carrier=carrier)])]) )
def get_pending_queue(self): if self.use_es: must = [ es_filter.Term(status=amo.STATUS_PENDING), es_filter.Term( **{'latest_version.status': amo.STATUS_PENDING}), es_filter.Term(is_escalated=False), es_filter.Term(is_disabled=False), ] return WebappIndexer.search().filter('bool', must=must) return (Version.objects.no_cache().filter( files__status=amo.STATUS_PENDING, addon__disabled_by_user=False, addon__status=amo.STATUS_PENDING).exclude( addon__id__in=self.excluded_ids).order_by( 'nomination', 'created').select_related('addon', 'files').no_transforms())
def get_homescreen_queue(self): # Both unreviewed homescreens and published homescreens with new # unreviewed versions go in this queue. if self.use_es: must = [ es_filter.Term(**{'latest_version.status': mkt.STATUS_PENDING}), es_filter.Term(is_escalated=False), es_filter.Term(is_disabled=False), ] return HomescreenIndexer.search().filter('bool', must=must) return (Version.objects.filter( files__status=mkt.STATUS_PENDING, addon__disabled_by_user=False, addon__tags__tag_text='homescreen') .exclude(addon__id__in=self.excluded_ids) .order_by('nomination', 'created') .select_related('addon', 'files').no_transforms())
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
def filter_queryset(self, request, queryset, view): daily_seed = int(datetime.datetime.now().strftime('%Y%m%d')) # Map over the game categories to create a function score query for one # and dump it into a Bool should. game_query = query.Q( 'function_score', filter=es_filter.Bool( should=[es_filter.Term(tags=cat) for cat in GAME_CATEGORIES]), # Consistently random based on the day. functions=[SF('random_score', seed=daily_seed)], ) # Buckets by tag. Run a size=1 TopHits aggregation to only select one # game from each tag. Results will have to be pulled out of # S.execute().aggregations rather than S.execute().hits. top_hits = aggs.TopHits(size=1) a = aggs.A('terms', field='tags', aggs={'first_game': top_hits}) queryset = queryset.query(game_query)[0:4] queryset.aggs.bucket('top_hits', a) # Not chainable. return queryset
def _get_colombia_filter(self): if self.allow_colombia and self.request.REGION == mkt.regions.COL: return None co_filter = es_filter.Term(tags=COLOMBIA_WEBSITE) return es_filter.F(es_filter.Bool(must_not=[co_filter]), )