Exemple #1
0
    def search_dataset(self, request, **kwargs):
        """
        Perform a full-text search on only one dataset.

        TKTK -- implement field searches
        TKTK -- implement wildcard + boolean searches
        """
        self.method_check(request, allowed=['get'])
        self.is_authenticated(request)
        self.throttle_check(request)

        if 'pk' in kwargs:
            dataset_id = kwargs['pk']
        else:
            dataset_id = request.GET.get('id')

        d = Dataset.objects.get(id=dataset_id)

        limit = int(request.GET.get('limit', settings.PANDA_DEFAULT_SEARCH_ROWS))
        offset = int(request.GET.get('offset', 0))

        s = SolrSearch(self._solr())
        s = s.query(full_text=request.GET.get('q'))
        s = s.filter(dataset_id=dataset_id)
        s = s.paginate(offset, limit)
        s = s.execute()

        paginator = Paginator(request.GET, s, resource_uri=request.path_info)
        page = paginator.page()

        dataset_url = reverse('api_dispatch_detail', kwargs={'api_name': kwargs['api_name'], 'resource_name': 'dataset', 'pk': dataset_id })

        # Update with attributes from the dataset
        # (Resulting object matches a group from the search endpoint)
        page.update({
            'id': d.id,
            'name': d.name,
            'resource_uri': dataset_url,
            'row_count': d.row_count,
            'schema': d.schema
        })

        objects = []

        for obj in s.result.docs:
            bundle = self.build_bundle(obj=SolrObject(obj), request=request)
            bundle = self.full_dehydrate(bundle)
            objects.append(bundle)

        page['objects'] = objects

        self.log_throttled_access(request)

        return self.create_response(request, page)
Exemple #2
0
    def search(self, request, **kwargs):
        """
        An endpoint for performing full-text searches.

        TKTK -- implement field searches
        TKTK -- implement wildcard + boolean searches
        """
        self.method_check(request, allowed=['get'])
        self.is_authenticated(request)
        self.throttle_check(request)

        limit = int(request.GET.get('limit', settings.PANDA_DEFAULT_SEARCH_GROUPS))
        offset = int(request.GET.get('offset', 0))

        s = SolrSearch(self._solr())
        s = s.query(full_text=request.GET.get('q'))
        s = s.group_by('dataset_id', limit=settings.PANDA_DEFAULT_SEARCH_ROWS_PER_GROUP, offset=0, sort='+row')
        s = s.paginate(offset, limit)
        s = s.execute()

        paginator = Paginator(request.GET, s, resource_uri=request.path_info)

        page = paginator.page()

        datasets = []

        for dataset_id, group in s.result.groups.items():
            dataset_url = reverse('api_dispatch_detail', kwargs={'api_name': kwargs['api_name'], 'resource_name': 'dataset', 'pk': dataset_id })
            dataset_search_url = reverse('api_search_dataset', kwargs={'api_name': kwargs['api_name'], 'resource_name': 'dataset', 'pk': dataset_id })

            d = Dataset.objects.get(id=dataset_id)

            dataset = {
                'id': d.id,
                'name': d.name,
                'resource_uri': dataset_url,
                'row_count': d.row_count,
                'schema': d.schema,
                'meta': {
                    'limit': settings.PANDA_DEFAULT_SEARCH_ROWS_PER_GROUP,
                    'next': None,
                    'offset': 0,
                    'previous': None,
                    'total_count': group.numFound
                },
                'objects': []
            }

            if group.numFound > settings.PANDA_DEFAULT_SEARCH_ROWS_PER_GROUP:
                dataset['meta']['next'] = '?'.join([dataset_search_url, 'limit=%i&offset=%i' % (settings.PANDA_DEFAULT_SEARCH_ROWS, settings.PANDA_DEFAULT_SEARCH_ROWS)])

            for obj in group.docs:
                bundle = self.build_bundle(obj=SolrObject(obj), request=request)
                bundle = self.full_dehydrate(bundle)
                dataset['objects'].append(bundle)

            datasets.append(dataset)

        page['objects'] = datasets

        self.log_throttled_access(request)

        return self.create_response(request, page)