def get_permission(self, user_id, file_ids): query_conditions = query.Bool(must=[ query.Terms(file_id=file_ids), query.Bool(should=[ query.Term(owner={ 'value': user_id, 'boost': 100 }), query.Bool(must=[ query.Term(share_mode={ 'value': 1, 'boost': 5 }), query.Term(users_shared={ 'value': user_id, 'boost': 5 }) ]), query.Term(share_mode=2) ]) ]) file_es = Search() \ .query(query_conditions) \ .source(['owner', 'share_mode', 'editable']) file_es = file_es[0:1] print(json.dumps(file_es.to_dict())) responses = file_es.using(self.es).index(self._index).execute() return responses
def build_sku_query(self, args: dict): skus = args.get('skus') or [] query_conditions = query.Bool(filter=[ query.Terms(sku=skus) ]) products_es = self.build_product_es_from_text_query_condition(args, query_conditions) # print(json.dumps(products_es.to_dict())) return products_es
def ranged_log_search(cls, start=None, end=None, hosts=[]): """ Returns a search with time range and hosts list terms""" search = cls.bounded_search(start, end) if len(hosts) != 0: # double underscore is translated to a '.' in the ES field name. search = search.query(query.Terms(host__raw=hosts)) return search
def get_filter_conditions(self, args): conditions = [] conditions.append(query.MatchAll()) seller_id = args.get('seller_id') if seller_id: conditions.append(query.Term(seller__id=seller_id)) category_codes = args.get('category_codes') if category_codes: conditions.append(query.Nested( path='categories', query=query.Terms(categories__code=category_codes) )) brand_codes = args.get('brand_codes') if brand_codes: conditions.append(query.Terms(brand__code=brand_codes)) return conditions
def _query_terms(cls, values: Sequence[object], field: Sequence[str]) -> Query: q = query.Terms(**{".".join(field): values}) for i in range(len(field) - 2, -1, -1): q = query.Nested(path=".".join(field[:i + 1]), query=q) return q
def get_must_conditions(self, args): conditions = [] file_id = args.get('file_id') if file_id: if isinstance(file_id, list): conditions.append(query.Terms(file_id=file_id)) else: conditions.append(query.Term(file_id=file_id)) search_text = args.get('q') if file_id and search_text: raise BadRequestException("Not support both q and file_id param") if search_text: conditions.append( query.DisMax(queries=[ query.MatchPhrasePrefix(file_title={ 'query': search_text, 'boost': 10 }), query.MatchPhrasePrefix(file_title__no_tone={ 'query': search_text, 'boost': 10 }), query.Match( file_title={ 'query': search_text, 'boost': 4, 'operator': 'or', 'minimum_should_match': "1<75%" }), query.Match( file_title__no_tone={ 'query': search_text, 'boost': 4, 'operator': 'or', 'minimum_should_match': "1<75%" }), query.Match( file_tag__text={ 'query': search_text, 'boost': 2, 'operator': 'or', 'minimum_should_match': "1<75%" }), query.MatchPhrasePrefix(file_tag__text={ 'query': search_text, 'boost': 2 }), query.Match( description={ 'query': search_text, 'boost': 1, 'operator': 'or', 'minimum_should_match': "1<75%" }), query.Match( description__no_tone={ 'query': search_text, 'boost': 1, 'operator': 'or', 'minimum_should_match': "1<75%" }) ])) if not conditions: conditions.append(query.MatchAll()) return conditions