コード例 #1
0
ファイル: search.py プロジェクト: bloomonkey/archiveshub
    def facets(self, rs):
        session = self.session
        db = self.database
        pm = db.get_path(session, 'protocolMap')
        if not pm:
            db._cacheProtocolMaps(session)
            pm = db.protocolMaps.get('http://www.loc.gov/zing/srw/')
        try:
            nTerms = self.config.getint('facets', 'truncate')
        except ValueError:
            if self.config.getboolean('facets', 'truncate'):
                # Want truncation but value not specified - default 1000
                nTerms = 1000
            else:
                nTerms = 0

        facets = {}
        for cqlIdx, humanName in self.config.items('facets'):
            if not '.' in cqlIdx:
                # Setting, not index
                continue
            query = self.queryFactory.get_query(session,
                                                '{0}="*"'.format(cqlIdx),
                                                format='cql')
            idxObj = pm.resolveIndex(session, query)
            try:
                facets[(cqlIdx, humanName)] = idxObj.facets(session,
                                                            rs,
                                                            nTerms
                                                            )
            except:
                self._log(40, "Couldn't get facets from {0}".format(cqlIdx))
                facets[(cqlIdx, humanName)] = {}
        return facets
コード例 #2
0
ファイル: search.py プロジェクト: bloomonkey/archiveshub
    def _searchAndSort(self, form):
        # Process form and returns a ResultSet
        session = self.session
        db = self.database
        rsid = form.getvalue('rsid', None)
        sortBy = form.getlist('sortBy')
        maximumRecords = int(form.getvalue('maximumRecords', 20))
        startRecord = int(form.getvalue('startRecord', 1))
        if rsid and not 'filter' in form:
            try:
                rs = self._fetch_resultSet(session, rsid)
            except ObjectDoesNotExistException:
                self._log(40, '*** Invalid ResultSet identifier: %s' % rsid)
                return self._render_template(
                    'fail/invalidResultSet.html',
                    rsid=rsid
                )
        else:
            query = self._get_query(form)
            if not isinstance(query, (CQLClause, CQLTriple)):
                # Error message
                return query
            msg = (u'Searching CQL query: {0}'
                   u''.format(query.toCQL())
                   )
            self._log(20, msg.encode('utf-8'))
            rs = db.search(session, query)
            self._log(20, '{0} Hits'.format(len(rs)))
            if len(rs):
                # Store the Query
                query = self._store_query(session, query)
                # Store the Resultset
                rs.id = query.id
                self._store_resultSet(session, rs)
            else:
                # Log 0 result?
                pass
        if sortBy:
            for spec in reversed(sortBy):
                # Check for ascending/descending sort
                # N.B. sort ascending=None is not the same as ascending=False
                # False => descending, None => do what's best for this spec
                ascending = None
                if spec.endswith('/ascending'):
                    spec = spec[:-len('/ascending')]
                    ascending = True
                elif spec.endswith('/descending'):
                    spec = spec[:-len('/descending')]
                    ascending = False

                # Check if the sort spec is an index
                protocolMap = db.get_path(session, 'protocolMap')
                try:
                    sortClause = self.queryFactory.get_query(
                        session,
                        '{0} exact ""'.format(spec)
                    )
                    # Fetch the index object from the database
                    index = protocolMap.resolveIndex(session, sortClause)
                except CQLDiagnostic:
                    index = None

                if index:
                    rs.order(session,
                             index,
                             ascending=ascending,
                             missing=0
                             )
                elif spec:
                    # Not an index, maybe a ResultSetItem attribute, or XPath?
                    # Pass the string to ResultSet to see what it makes of it
                    rs.order(session, spec)

        # Set resultSet cookie
        self._set_cookie('resultSet_id', rs.id)
        self._set_cookie('resultSet_startRecord', startRecord)
        self._set_cookie('resultSet_maximumRecords', maximumRecords)
        self._set_cookie('resultSet_sortBy', ','.join(sortBy))
        return rs