def test_apply_custom_analyzer(self): document_cls = Mock() document_cls.get_es_mapping = Mock(return_value=({'Assignment': { 'properties': { 'assignee_id': {'type': 'string', 'analyzer': 'email'}, 'simple': {'type': 'string'}}}}, [])) get_document_cls = Mock(return_value=document_cls) params = {'assignee_id': 'some_user', 'simple': 'new_value'} result = apply_analyzer(params, 'Assignment', get_document_cls) assert result == {'bool': {'must': [{'term': {'assignee_id': 'some_user'}}]}}
def test_apply_custom_analyzer_doesnt_duplicated(self): def get_document_cls(model_name): document = Mock() if model_name == 'Assignment': document.get_es_mapping = Mock(return_value=({'Assignment': { 'properties': { 'assignee_id': {'type': 'string', 'analyzer': 'email'}, 'simple': {'type': 'string'}}}}, [])) return document if model_name == 'Task': document.get_es_mapping = Mock(return_value=({'Task': { 'properties': { 'assignee_id': {'type': 'string', 'analyzer': 'email'}, 'simple': {'type': 'string'}}}}, [])) return document params = {'assignee_id': 'some_user', 'simple': 'new_value'} result = apply_analyzer(params, 'Assignment,Task', get_document_cls) assert result == {'bool': {'must': [{'term': {'assignee_id': 'some_user'}}]}}
def build_search_params(self, params): params = dictset(params) _params = dict( index=self.index_name, doc_type=self.doc_type ) _raw_terms = params.pop('q', '') if 'body' not in params and 'es_q' not in params: analyzed_terms = apply_analyzer(params, self.doc_type, engine.get_document_cls) query_string = self.build_qs(params.remove(RESERVED_PARAMS), _raw_terms) query = {'must': []} if query_string: query['must'].append({'query_string': {'query': query_string}}) if analyzed_terms: query['must'].append(analyzed_terms) if query['must']: _params['body'] = {'query': {'bool': query}} else: _params['body'] = {'query': {'match_all': {}}} if 'body' in params: raise JHTTPUnprocessableEntity('Illegal parameter "body"') if '_limit' not in params: params['_limit'] = self.api.count(index=self.index_name)['count'] _params['from_'], _params['size'] = process_limit( params.get('_start', None), params.get('_page', None), params['_limit']) if 'es_q' in params: _params['body'] = {} try: _params['body']['query'] = compile_es_query(params) except Exception as exc: log.exception('es_q parsing error: {exc}'.format(exc=exc)) raise JHTTPBadRequest('Bad query string for {params}' .format( params=_params['body']['query']['query_string']['query'])) log.debug('Parsed ES request body {body}'.format(body=_params['body']['query'])) if '_sort' in params and self.proxy: params['_sort'] = substitute_nested_terms(params['_sort'], self.proxy.substitutions) if '_sort' in params: _params['sort'] = apply_sort(params['_sort']) if '_fields' in params: params['_fields'] = self.add_nested_fields(params['_fields'], ',') _params['fields'] = params['_fields'] if '_search_fields' in params: search_fields = params['_search_fields'].split(',') search_fields.reverse() # Substitute search fields and add ^index for index, search_field in enumerate(search_fields): sf_terms = search_field.split('.') if self.proxy is not None: if len(sf_terms) > 0 and sf_terms[0] in self.proxy.substitutions: sf_terms[0] += "_nested" search_field = '.'.join(sf_terms) search_fields[index] = search_field + '^' + str(index + 1) must_query = _params['body']['query']['bool']['must'] query_string = {} for query_item in must_query: if 'query_string' in query_item: query_string = query_item break current_qs = query_string.get('query_string', None) if current_qs: query_string['query_string']['fields'] = search_fields return _params