def search_type(text): q = BoolQuery(minimum_number_should_match=0) #TODO add synonyms instead of relying on 'french' q.add_must(MatchQuery('french', normalize_query_string(text))) types = conn.search(query=q, indices="types") if len(types) < 1: log("KO", text) return [t.get_id() for t in types[:1]]
def search_city(text): q = BoolQuery(minimum_number_should_match=0) q.add_must(MatchQuery('name', normalize_query_string(text))) q.add_should(MatchQuery('zipcode', normalize_query_string(text))) cities = conn.search(query=q, indices="cities") if len(cities) < 1: log("KO", text) return [city.get_id() for city in cities[:1]] return result
def _get_user_filter(self, user=None): '''Get a filter query with given user context, which will be used for filtering hits down to those are only accessible to the user. ''' #filter = TermQuery(field='permission', value='gnfusers') user = user or self.user filter = BoolQuery() #always includes "_type:gene", so gene type will bypass the permission # filters below. filter.add_should(TermQuery('_type', 'gene')) #allow 'dataset' type bypass the permission below for now, #until permission for dataset is set. filter.add_should(TermQuery('_type', 'dataset')) #filter by roles if not user or user.is_anonymous(): user_roles = [] else: user_roles = get_role_shortnames(user.roles) if 'biogpsusers' not in user_roles: #always add the role for public access user_roles.append('biogpsusers') role_filter = TermsQuery(field='role_permission', value=user_roles) filter.add_should(role_filter) #filter by username to get user's own objects if user and user.username: username_filter = TermQuery(field='username', value=user.username) filter.add_should(username_filter) return filter
def _build_filter(self, only_in=[], filter_by=None): '''Return list of hits based on given value on a specific field. @param filter_by: A dictionary of {<field>: <value>} or a list of (<field>, <value>) tuple, e.g., {'tag': 'chart', 'species': 'human'} [('tag', 'chart'), ('species', ['human', 'mouse'])] Note that <value> can be a list for multiple values. @param kwargs: refer to self.query method. ''' if only_in == ['gene']: filter = BoolQuery() else: filter = self._get_user_filter() if filter_by: if isinstance(filter_by, dict): _filter_by = filter_by.items() else: _filter_by = filter_by for (field, value) in _filter_by: if type(value) is types.ListType: sub_filter = BoolQuery() for v in value: sub_filter.add_should(TermQuery(field, v)) filter.add_must(sub_filter) else: filter.add_must(TermQuery(field, value)) return None if filter.is_empty() else filter
def _build_es_query(cls, search_phrase): """ Return an instance of `~pyes.query.Query` for the given phrase. If downstream modules wish to alter the behavior of search, for example by adding more fields to the query or changing the ranking in a different way, this would be the method to change. """ return BoolQuery( should=[ MatchQuery( 'code', search_phrase, boost=1.5 ), MatchQuery( 'name', search_phrase, boost=2 ), MatchQuery( 'name.partial', search_phrase ), MatchQuery( 'name.metaphone', search_phrase ), ], must=[ MatchQuery( 'active', "true" ), ] )
def query_gene_by_interval(self, chr, gstart, gend, taxid=None, species=None, assembly=None, **kwargs): '''query genes by genome interval.''' if (taxid, species, assembly) == (None, None, None): raise ValueError('At least one of "taxid", "species", "assembly" need to be specified.') qrange = ESRange(field='pos', from_value=safe_genome_pos(gstart), to_value=safe_genome_pos(gend), include_lower=True, include_upper=True) q = RangeQuery(qrange) filter = BoolQuery() filter.add_must(TermQuery('chr', chr)) if taxid: filter.add_must(TermQuery('taxid', taxid)) if species: filter.add_must(TermQuery('species', species)) if assembly: filter.add_must(TermQuery('assembly', assembly)) q = FilteredQuery(q, filter) return self.query(q, only_in=['gene'], **kwargs)
def search(searchkey=u"电影"): conn = ES('127.0.0.1:9200') # TextQuery会对searchkey进行分词 qtitle = TextQuery("title", searchkey) h = HighLighter(['<b>'], ['</b>'], fragment_size=500) # 多字段搜索(must=>and,should=>or),高亮,结果截取(分页),排序 q = Search(BoolQuery(should=[qtitle]), highlight=h, start=0, size=3, sort={'id': { 'order': 'asc' }}) q.add_highlight("title") results = conn.search(q, "zhihu", "answer") list = [] for r in results: if ("title" in r._meta.highlight): r['title'] = r._meta.highlight[u"title"][0] list.append(r) return template('results.html', list=list, count=results.total)
def _create_term_query(self, must_list): # TODO: add remaining conditional list functionality. query = BoolQuery() for term in must_list: query.add_must(term)
from pyes import ES from pyes import TermQuery from pyes import RangeQuery from pyes import QueryStringQuery from pyes import BoolQuery from pyes import ESRange from pyes import ANDFilter from pyes import TermFilter from pyes import FilteredQuery from pyes import query conn = ES('localhost:9200') a_range = RangeQuery(qrange=ESRange('a', 0.179, 0.180)) b_filter = TermFilter("b", "0.2") period_filter = TermFilter("period", "2") total_filter = ANDFilter([b_filter, period_filter]) c_range = RangeQuery(qrange=ESRange('c', 8, 12)) que = FilteredQuery(BoolQuery(must=[a_range, c_range]), total_filter) search = query.Search(query=que) get = conn.search(search, indices='shrimp') census = get.total for i in get: print i