示例#1
0
def sunburnt_query(entity_type=None,
                   excluded_entity_types=set(),
                   instance=None,
                   connection=None):
    '''
    return a pre configured sunburnt query object. If *entity_type*
    is given, return a query object preconfigured to only fetch
    documents from solr with a matching doc_type. if instance is
    given, only documents are returned that contain the index
    key.

    *entity_type*
        An indexed model class. Indexed classes are listed
        in :data:`adhocracy.lib.search.INDEXED_CLASSES`.
    *instance*
        A :class:`adhocracy.model.Instance` object
    *connection*
        An existing sunburnt connection. Mostly useful
        in tests.
    '''
    if connection is None:
        connection = get_sunburnt_connection()
    q = connection.query()
    if entity_type:
        q = q.filter(doc_type=refs.cls_type(entity_type))
    for t in excluded_entity_types:
        q = q.filter_exclude(doc_type=refs.cls_type(t))
    if instance and c.instance:
        q = q.filter(instance=instance.key)
    return q
示例#2
0
文件: query.py 项目: dwt/adhocracy
def sunburnt_query(entity_type=None, excluded_entity_types=set(),
                   instance=None, connection=None):
    '''
    return a pre configured sunburnt query object. If *entity_type*
    is given, return a query object preconfigured to only fetch
    documents from solr with a matching doc_type. if instance is
    given, only documents are returned that contain the index
    key.

    *entity_type*
        An indexed model class. Indexed classes are listed
        in :data:`adhocracy.lib.search.INDEXED_CLASSES`.
    *instance*
        A :class:`adhocracy.model.Instance` object
    *connection*
        An existing sunburnt connection. Mostly useful
        in tests.
    '''
    if connection is None:
        connection = get_sunburnt_connection()
    q = connection.query()
    if entity_type:
        q = q.filter(doc_type=refs.cls_type(entity_type))
    for t in excluded_entity_types:
        q = q.filter_exclude(doc_type=refs.cls_type(t))
    if instance and c.instance:
        q = q.filter(instance=instance.key)
    return q
示例#3
0
def run(terms, instance=None, entity_type=None, **kwargs):
    conn = get_connection()
    try:
        if terms is None or not len(terms):
            terms = u'*:*'

        filter_query = u''

        if entity_type:
            filter_query += u'+doc_type:%s' % refs.cls_type(entity_type)

        if instance:
            filter_query += u' +instance:%s' % instance.key

        log.debug("Query: %s (fq: %s)" % (terms, filter_query))
        data = conn.query(terms, fq=filter_query, rows=1000)

        if (entity_type is not None and hasattr(entity_type, 'find_all') and
            len(data.results)):
            ids = [refs.to_id(r.get('ref')) for r in data.results]
            return entity_type.find_all(ids, **kwargs)

        entities = []
        for fields in data.results:
            ref = fields.get('ref')
            entity = refs.to_entity(ref, **kwargs)
            entities.append(entity)
        return entities
    except Exception, e:
        log.exception(e)
        return []
示例#4
0
def run(terms, instance=None, entity_type=None, **kwargs):
    conn = get_connection()
    try:
        if terms is None or not len(terms):
            terms = u'*:*'

        filter_query = u''

        if entity_type:
            filter_query += u'+doc_type:%s' % refs.cls_type(entity_type)

        if instance:
            filter_query += u' +instance:%s' % instance.key

        log.debug("Query: %s (fq: %s)" % (terms, filter_query))
        data = conn.query(terms, fq=filter_query, rows=1000)

        if (entity_type is not None and hasattr(entity_type, 'find_all')
                and len(data.results)):
            ids = [refs.to_id(r.get('ref')) for r in data.results]
            return entity_type.find_all(ids, **kwargs)

        entities = []
        for fields in data.results:
            ref = fields.get('ref')
            entity = refs.to_entity(ref, **kwargs)
            entities.append(entity)
        return entities
    except Exception, e:
        log.exception(e)
        return []
示例#5
0
def sunburnt_query(entity_type=None):
    '''
    return a sunburnt query object. If *entity_type* is given,
    return a query object preconfigured to only fetch documents
    from solr with a matching doc_type
    '''
    si = get_sunburnt_connection()
    q = si.query()
    if entity_type:
        q = q.filter(doc_type=refs.cls_type(entity_type))
    return q
示例#6
0
def sunburnt_query(entity_type=None):
    '''
    return a sunburnt query object. If *entity_type* is given,
    return a query object preconfigured to only fetch documents
    from solr with a matching doc_type
    '''
    si = get_sunburnt_connection()
    q = si.query()
    if entity_type:
        q = q.filter(doc_type=refs.cls_type(entity_type))
    return q