Exemplo n.º 1
0
    def get_results(self, parameters, cleaned_data):
        page = cleaned_data.get('page') or 1
        page_size = cleaned_data.get('page_size') or self.page_size
        start = (page - 1) * page_size

        query = self.get_query(parameters, cleaned_data, start, page_size)

        # If there aren't any filters defined, we don't want a filter part of
        # our query.
        if 'filter' in query:
            if 'and' in query['filter'] and not query['filter']['and']:
                del query['filter']['and']
            if not query['filter']:
                del query['filter']

        results = self.search_endpoint.query(query)

        results.update(self.get_pagination(page_size, page, start, results))
        results['q'] = cleaned_data['q']

        facet_labels = set()
        for key in query['facets']:
            meta = results['facets'][key]['meta'] = query['facets'][key]
            filter_value = parameters.get(
                'filter.%s' % query['facets'][key]['terms']['field'])
            results['facets'][key]['filter'] = {
                'present': filter_value is not None,
                'value': filter_value
            }
            if meta['terms']['field'].endswith('.uri'):
                for term in results['facets'][key]['terms']:
                    facet_labels.add(term['term'])
                    term['value'] = contract(term['term'])
            else:
                for term in results['facets'][key]['terms']:
                    term['value'] = term['term']

        labels = get_labels(map(rdflib.URIRef, facet_labels),
                            endpoint=self.endpoint)
        for key in query['facets']:
            if results['facets'][key]['meta']['terms']['field'].endswith(
                    '.uri'):
                for term in results['facets'][key]['terms']:
                    uri = rdflib.URIRef(term['term'])
                    if uri in labels:
                        term['label'] = unicode(labels[uri])

        for hit in results['hits']['hits']:
            try:
                hit['_url'] = doc_forwards(hit['_source']['uri'])[None]
            except KeyError:
                raise

        return results
Exemplo n.º 2
0
    def get_results(self, query):
        results = self.search_endpoint.query(query)

        results['q'] = query

        for hit in results['hits']['hits']:
            try:
                hit['_url'] = doc_forwards(hit['_source']['uri'])[None]
            except KeyError:
                raise

        return results
Exemplo n.º 3
0
    def get_results(self, query):
        results = self.search_endpoint.query(query)

        results['q'] = query

        for hit in results['hits']['hits']:
            try:
                hit['_url'] = doc_forwards(hit['_source']['uri'])[None]
            except KeyError:
                raise

        return results
Exemplo n.º 4
0
    def get_results(self, parameters, cleaned_data):
        page = cleaned_data.get('page') or 1
        page_size = cleaned_data.get('page_size') or self.page_size
        start = (page - 1) * page_size

        query = self.get_query(parameters, cleaned_data, start, page_size)

        # If there aren't any filters defined, we don't want a filter part of
        # our query.
        if 'filter' in query:
            if 'and' in query['filter'] and not query['filter']['and']:
                del query['filter']['and']
            if not query['filter']:
                del query['filter']

        results = self.search_endpoint.query(query)

        results.update(self.get_pagination(page_size, page, start, results))
        results['q'] = cleaned_data['q']

        facet_labels = set()
        for key in query['facets']:
            meta = results['facets'][key]['meta'] = query['facets'][key]
            filter_value = parameters.get('filter.%s' % query['facets'][key]['terms']['field'])
            results['facets'][key]['filter'] = {'present': filter_value is not None,
                                                'value': filter_value}
            if meta['terms']['field'].endswith('.uri'):
                for term in results['facets'][key]['terms']:
                    facet_labels.add(term['term'])
                    term['value'] = contract(term['term'])
            else:
                for term in results['facets'][key]['terms']:
                    term['value'] = term['term']

        labels = get_labels(map(rdflib.URIRef, facet_labels), endpoint=self.endpoint)
        for key in query['facets']:
            if results['facets'][key]['meta']['terms']['field'].endswith('.uri'):
                for term in results['facets'][key]['terms']:
                    uri = rdflib.URIRef(term['term'])
                    if uri in labels:
                        term['label'] = unicode(labels[uri])

        for hit in results['hits']['hits']:
            try:
                hit['_url'] = doc_forwards(hit['_source']['uri'])[None]
            except KeyError:
                raise

        return results
Exemplo n.º 5
0
def formats(request, graph, doc_uri, subject_uri, subject, endpoint, renderers):
    format_urls = doc_forwards(subject_uri, renderers, described=True)
    formats_for_context = []
    for renderer in renderers:
        url = rdflib.URIRef(format_urls[renderer.format])
        formats_for_context.append({
            'url': url,
            'name': renderer.name,
            'mimetypes': renderer.mimetypes,
            'format': renderer.format,
        })
        map(graph.add, [
            (doc_uri, NS['dcterms'].hasFormat, url),
            (url, NS['dcterms']['title'], rdflib.Literal('%s description of %s' % (renderer.name, subject.label))),
        ] + [(url, NS['dc']['format'], rdflib.Literal(mimetype)) for mimetype in renderer.mimetypes]
        )
    formats_for_context.sort(key=lambda f:f['name'])
    return {
        'formats': formats_for_context,
    }
Exemplo n.º 6
0
    def get_results(self, parameters, cleaned_data):
        page = cleaned_data.get('page') or 1
        page_size = cleaned_data.get('page_size') or self.page_size
        start = (page - 1) * page_size

        query = {
            'query': {'query_string': {'query': cleaned_data['q'],
                                       'default_operator': 'AND'}},
            'from': start,
            'size': page_size,
            # A blank conjunctive filter. We'll remove this later if necessary.
            'filter': {'and': []},
        }

        # Parse query parameters of the form 'filter.FIELDNAME'.
        for key in list(parameters):
            parameter = parameters[key]
            if key.startswith('filter.'):
                if not parameter:
                    del parameters[key]
                    continue
                elif parameter == '-':
                    filter = {'missing': {'field': key[7:]}}
                else:
                    if key.endswith('.uri') and ':' in parameter:
                        parameter = expand(parameter)
                    filter = {'term': {key[7:]: parameter}}
                query['filter']['and'].append(filter)

        # If there aren't any filters defined, we don't want a filter part of
        # our query.
        if not query['filter']['and']:
            del query['filter']['and']
        if not query['filter']:
            del query['filter']

        if self.facets:
            # Copy the facet definitions as we'll be playing with them shortly.
            facets = copy.deepcopy(self.facets)

            # Add facet filters for all active filters except any acting on this
            # particular facet.
            if 'filter' in query:
                for facet in facets.itervalues():
                    for filter in query['filter']['and']:
                        if facet['terms']['field'] not in (filter.get('term') or filter['missing']):
                            if 'facet_filter' not in facet:
                                facet['facet_filter'] = {'and': []}
                            facet['facet_filter']['and'].append(filter)
            query['facets'] = facets

        response = urllib2.urlopen(self.search_url, json.dumps(query))
        results = self.Deunderscorer(json.load(response))

        results.update(self.get_pagination(page_size, page, start, results))
        results['q'] = cleaned_data['q']

        facet_labels = set()
        for key in query['facets']:
            meta = results['facets'][key]['meta'] = query['facets'][key]
            filter_value = parameters.get('filter.%s' % query['facets'][key]['terms']['field'])
            results['facets'][key]['filter'] = {'present': filter_value is not None,
                                                'value': filter_value}
            if meta['terms']['field'].endswith('.uri'):
                for term in results['facets'][key]['terms']:
                    facet_labels.add(term['term'])
                    term['value'] = contract(term['term'])
            else:
                for term in results['facets'][key]['terms']:
                    term['value'] = term['term']
        
        labels = get_labels(facet_labels, endpoint=self.endpoint)
        for key in query['facets']:
            if results['facets'][key]['meta']['terms']['field'].endswith('.uri'):
                for term in results['facets'][key]['terms']:
                    uri = URIRef(term['term'])
                    if uri in labels:
                        term['label'] = unicode(labels[uri])

        for hit in results['hits']['hits']:
            try:
                hit['_url'] = doc_forwards(hit['_source']['uri'])[None]
            except KeyError:
                raise

        return results