def _get_query_string_for_arguments(self, request, arg_name, field_name=None): """Returns query string for request args :param request: object representing the HTTP request :param str arg_name: request arguments :param str field_name: elasticsearch field name """ if not request or not getattr(request, 'args') or not getattr( request, 'args').get(arg_name): return None search_string = sanitize_query_text( getattr(request, 'args').get(arg_name)) query_text = '{}{}({})'.format(field_name if field_name else '', ':' if field_name else '', search_string) return { 'query_string': { 'query': query_text, 'lenient': True, 'default_operator': 'AND' } }
def query_string(text: str, lenient: bool = False, default_operator: str = 'OR', field: str = None): sanitized_text = sanitize_query_text(text) query = f'{field}:({sanitized_text})' if field is not None else sanitized_text return { 'query_string': { 'query': query, 'lenient': lenient, 'default_operator': default_operator } }
def _get_query_string_for_slugline(self, request): """Get elasticsearch query string for slugline :param request: object representing the HTTP request """ if not request or not getattr(request, 'args') or not getattr( request, 'args').get('slugline'): return [] query_text = sanitize_query_text( getattr(request, 'args').get('slugline')) if not query_text: return [] return [{ 'bool': { 'should': [{ 'query_string': { 'query': 'slugline:({})'.format(query_text), 'lenient': False, 'default_operator': 'AND' } }, { 'nested': { 'path': 'coverages', 'query': { 'bool': { 'must': [{ 'query_string': { 'query': 'coverages.planning.slugline:({})'. format(query_text), 'lenient': False, 'default_operator': 'AND' } }] } } } }] } }]