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 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 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 _create_term_query(self, must_list): # TODO: add remaining conditional list functionality. query = BoolQuery() for term in must_list: query.add_must(term)