示例#1
0
def es_search_by_params(search_params, domain, reserved_query_params=None):
    payload = {
        "filter": {
            "and": [{
                "term": {
                    "domain.exact": domain
                }
            }]
        },
    }

    # ?_search=<json> for providing raw ES query, which is nonetheless restricted here
    # NOTE: The fields actually analyzed into ES indices differ somewhat from the raw
    # XML / JSON.
    if '_search' in search_params:
        additions = json.loads(search_params['_search'])

        if 'filter' in additions:
            payload['filter']['and'].append(additions['filter'])

        if 'query' in additions:
            payload['query'] = additions['query']

    # ?q=<lucene>
    if 'q' in search_params:
        payload['query'] = payload.get('query', {})
        payload['query']['query_string'] = {
            'query': search_params['q']
        }  # A bit indirect?

    # filters are actually going to be a more common case
    reserved_query_params = RESERVED_QUERY_PARAMS | set(reserved_query_params
                                                        or [])
    query_params = {
        param: value
        for param, value in search_params.items()
        if param not in reserved_query_params and not param.endswith('__full')
    }
    for consumer in query_param_consumers:
        try:
            payload_filter = consumer.consume_params(query_params)
        except DateTimeError as e:
            raise Http400("Bad query parameter: {}".format(six.text_type(e)))

        if payload_filter:
            payload["filter"]["and"].append(payload_filter)

    # add unconsumed filters
    for param, value in query_params.items():
        # assume these fields are analyzed in ES so convert to lowercase
        # Any fields that are not analyzed in ES should be in the ``query_param_consumers`` above
        value = value.lower()
        payload["filter"]["and"].append(filters.term(param, value))

    return payload
示例#2
0
def _is_api_key_authentication(request):
    authorization_header = request.META.get('HTTP_AUTHORIZATION', '')

    api_key_authentication = HQApiKeyAuthentication()
    try:
        username, api_key = api_key_authentication.extract_credentials(request)
    except ValueError:
        raise Http400(
            "Bad HTTP_AUTHORIZATION header {}".format(authorization_header))
    else:
        return username and api_key
示例#3
0
def es_query_from_get_params(search_params,
                             domain,
                             reserved_query_params=None,
                             doc_type='form'):
    # doc_type can be form or case
    assert doc_type in ['form', 'case']
    es = FormES() if doc_type == 'form' else CaseES()

    query = es.remove_default_filters().domain(domain)

    if doc_type == 'form':
        if 'include_archived' in search_params:
            query = query.filter(
                filters.OR(filters.term('doc_type', 'xforminstance'),
                           filters.term('doc_type', 'xformarchived')))
        else:
            query = query.filter(filters.term('doc_type', 'xforminstance'))

    if '_search' in search_params:
        # This is undocumented usecase by Data export tool and one custom project
        #   Validate that the passed in param is one of these two expected
        _filter = _validate_and_get_es_filter(
            json.loads(search_params['_search']))
        query = query.filter(_filter)

    # filters are actually going to be a more common case
    reserved_query_params = RESERVED_QUERY_PARAMS | set(reserved_query_params
                                                        or [])
    query_params = {
        param: value
        for param, value in search_params.items()
        if param not in reserved_query_params and not param.endswith('__full')
    }
    for consumer in query_param_consumers:
        try:
            payload_filter = consumer.consume_params(query_params)
        except DateTimeError as e:
            raise Http400("Bad query parameter: {}".format(str(e)))

        if payload_filter:
            query = query.filter(payload_filter)

    # add unconsumed filters
    for param, value in query_params.items():
        # assume these fields are analyzed in ES so convert to lowercase
        # Any fields that are not analyzed in ES should be in the ``query_param_consumers`` above
        value = value.lower()
        query = query.filter(filters.term(param, value))

    return query.raw_query
示例#4
0
文件: es.py 项目: zbidi/commcare-hq
def es_search(request, domain, reserved_query_params=None):
    payload = {
        "filter": {
            "and": [{
                "term": {
                    "domain.exact": domain
                }
            }]
        },
    }

    # ?_search=<json> for providing raw ES query, which is nonetheless restricted here
    # NOTE: The fields actually analyzed into ES indices differ somewhat from the raw
    # XML / JSON.
    if '_search' in request.GET:
        additions = json.loads(request.GET['_search'])

        if 'filter' in additions:
            payload['filter']['and'].append(additions['filter'])

        if 'query' in additions:
            payload['query'] = additions['query']

    # ?q=<lucene>
    if 'q' in request.GET:
        payload['query'] = payload.get('query', {})
        payload['query']['query_string'] = {
            'query': request.GET['q']
        }  # A bit indirect?

    # filters are actually going to be a more common case
    reserved_query_params = RESERVED_QUERY_PARAMS | set(reserved_query_params
                                                        or [])
    for key in set(request.GET.keys()) - reserved_query_params:
        if key.endswith('__full'): continue

        value = request.GET[key]

        if key in query_param_transforms:
            try:
                payload["filter"]["and"].append(
                    query_param_transforms[key](value))
            except DateTimeError:
                raise Http400("Bad query parameter")
        else:
            payload["filter"]["and"].append({"term": {key: value}})

    return payload