示例#1
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 []
示例#2
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 []
示例#3
0
def rebuild(classes):
    '''
    (Re)Index all entities of the given *classes*.
    '''
    start = time.time()
    done = 0
    connection = index.get_connection()
    for cls in classes:
        if cls not in INDEXED_CLASSES:
            log.warn('Class "%s" is not an indexable class! skipping.' % cls)
            continue
        log.info("Re-indexing %ss..." % cls.__name__)
        for entity in model.meta.Session.query(cls):
            index.update(entity, connection=connection, commit=False)
            done = done + 1
            if done % 100 == 0:
                connection.commit()
                log.info('indexed %d in %ss' % (done, time.time() - start))
    connection.commit()
    connection.close()
示例#4
0
def rebuild(classes):
    '''
    (Re)Index all entities of the given *classes*.
    '''
    start = time.time()
    done = 0
    connection = index.get_connection()
    for cls in classes:
        if cls not in INDEXED_CLASSES:
            log.warn('Class "%s" is not an indexable class! skipping.' %
                     cls)
            continue
        log.info("Re-indexing %ss..." % cls.__name__)
        for entity in model.meta.Session.query(cls):
            index.update(entity, connection=connection, commit=False)
            done = done + 1
            if done % 100 == 0:
                connection.commit()
                log.info('indexed %d in %ss' % (done, time.time() - start))
    connection.commit()
    connection.close()