Exemplo n.º 1
0
    def post(self, request, format=None):
        """Performs search."""
        data = request.data.copy()

        # to support legacy paging parameters that can be in query_string
        for legacy_query_param in ('limit', 'offset'):
            if legacy_query_param in request.query_params \
                    and legacy_query_param not in request.data:
                data[legacy_query_param] = request.query_params[
                    legacy_query_param]

        validated_data = self.validate_data(data)
        query = self.get_base_query(request, validated_data)

        limited_query = limit_search_query(
            query,
            offset=validated_data['offset'],
            limit=validated_data['limit'],
        )

        results = execute_search_query(limited_query)

        response = {
            'count': results.hits.total,
            'results': [x.to_dict() for x in results.hits],
        }

        response = self.enhance_response(results, response)

        return Response(data=response)
Exemplo n.º 2
0
    def get(self, request, format=None):
        """Performs basic search."""
        serializer = BasicSearchQuerySerializer(data=request.query_params)
        serializer.is_valid(raise_exception=True)
        validated_params = serializer.validated_data

        fields_to_exclude = (
            *SHARED_FIELDS_TO_EXCLUDE,
            *(self.fields_to_exclude or ()),
        )

        query = get_basic_search_query(
            entity=validated_params['entity'],
            term=validated_params['term'],
            permission_filters_by_entity=dict(
                _get_global_search_permission_filters(request)),
            offset=validated_params['offset'],
            limit=validated_params['limit'],
            fields_to_exclude=fields_to_exclude,
        )

        results = execute_search_query(query)

        response = {
            'count':
            results.hits.total,
            'results': [result.to_dict() for result in results.hits],
            'aggregations': [{
                'count': x['doc_count'],
                'entity': x['key']
            } for x in results.aggregations['count_by_type']['buckets']],
        }

        return Response(data=response)