def get_es_query(self): query = [Q(self.operator, **{'promoted.group_id': self.get_values()})] if app := self.get_app(): # If a specific application isn't set then application_id is None, # but ES doesn't store None values - it just doesn't store the # field for that record - so we have to use NOT 'exists' to check # for None. query.append( Q('term', **{'promoted.application_id': app}) | ~Q('exists', field='promoted.application_id'))
def get_es_query(self): query = [Q(self.operator, **{'promoted.group_id': self.get_values()})] if app := self.get_app(): query.append(Q('term', **{'promoted.approved_for_apps': app}))
def _build_query(self, filter_kwargs): query = [] for filter_key, filter_value in filter_kwargs.items(): filter_key = filter_key.split('__') if filter_key[-1] in ['ne', 'in', 'iexact', 'contains', 'gte', 'gt', 'lte', 'lt']: filter_type = filter_key[-1] filter_key = '__'.join(filter_key[:-1]) else: filter_type = None filter_key = '__'.join(filter_key) if filter_type is None: if filter_value is None: query.append(~Q('exists', field=filter_key)) else: query.append(Q('term', ** {filter_key: filter_value})) elif filter_type == 'ne': if filter_value is None: query.append(Q('exists', field=filter_key)) else: query.append(~Q('term', ** {filter_key: filter_value})) elif filter_type == 'in': if None in filter_value: filter_value.remove(None) query.append(Q('bool', should=[ ~Q('exists', field=filter_key), Q('terms', ** {filter_key: filter_value}) ])) else: query.append(Q('terms', ** {filter_key: filter_value})) elif filter_type == 'iexact': query.append(Q('match', ** {filter_key: filter_value})) elif filter_type == 'contains': query.append(Q('wildcard', ** {filter_key: '*{}*'.format(filter_value)})) elif filter_type == 'gte': query.append(Q('range', ** {filter_key: {'gte': filter_value}})) elif filter_type == 'gt': query.append(Q('range', ** {filter_key: {'gt': filter_value}})) elif filter_type == 'lte': query.append(Q('range', ** {filter_key: {'lte': filter_value}})) elif filter_type == 'lt': query.append(Q('range', ** {filter_key: {'lt': filter_value}})) else: raise AssertionError('Invalid filter: {}'.format('__'.join(filter_key))) return query