Пример #1
0
def initialize(name=None, delete=False):
    index_name = name or '-'.join([es.index_name, date.today().isoformat()])
    log.info('Initiliazing index "{0}"'.format(index_name))
    if es.indices.exists(index_name):
        if prompt_bool('Index {0} will be deleted, are you sure ?'.format(index_name)):
            es.indices.delete(index_name)
        else:
            exit(-1)
    mappings = [
        (adapter.doc_type(), adapter.mapping)
        for adapter in adapter_catalog.values()
        if adapter.mapping
    ]
    with open(ANALYSIS_JSON) as analysis:
        es.indices.create(index_name, {
            'mappings': dict(mappings),
            'settings': {'analysis': json.load(analysis)},
        })

    for model, adapter in adapter_catalog.items():
        log.info('Indexing {0} objects'.format(model.__name__))
        qs = model.objects.visible() if hasattr(model.objects, 'visible') else model.objects
        for obj in qs.timeout(False):
            try:
                es.index(index=index_name, doc_type=adapter.doc_type(), id=obj.id, body=adapter.serialize(obj))
            except:
                log.exception('Unable to index %s "%s"', model.__name__, str(obj.id))

    log.info('Creating alias "{0}" index "{1}"'.format(es.index_name, index_name))
    indices = es.indices.get_alias(name=es.index_name).keys()
    es.indices.put_alias(index=index_name, name=es.index_name)
    for index in indices:
        es.indices.delete_alias(index=index, name=es.index_name)
        if delete:
            es.indices.delete(index=index)
Пример #2
0
def init(name=None, delete=False, force=False):
    '''Initialize or rebuild the search index'''
    index_name = name or '-'.join([es.index_name, date.today().isoformat()])
    log.info('Initiliazing index "{0}"'.format(index_name))
    if es.indices.exists(index_name):
        if force or prompt_bool(
            ('Index {0} will be deleted, are you sure ?'.format(index_name))):
            es.indices.delete(index_name)
        else:
            exit(-1)
    mappings = [(adapter.doc_type(), adapter.mapping)
                for adapter in adapter_catalog.values() if adapter.mapping]
    with open(ANALYSIS_JSON) as analysis:
        es.indices.create(
            index_name, {
                'mappings': dict(mappings),
                'settings': {
                    'analysis': json.load(analysis)
                },
            })

    for model, adapter in adapter_catalog.items():
        doctype = adapter.doc_type()
        log.info('Indexing {0} objects'.format(model.__name__))
        qs = model.objects
        if hasattr(model.objects, 'visible'):
            qs = qs.visible()
        for obj in qs.timeout(False):
            if adapter.is_indexable(obj):
                try:
                    es.index(index=index_name,
                             doc_type=doctype,
                             id=obj.id,
                             body=adapter.serialize(obj))
                except:
                    log.exception('Unable to index %s "%s"', model.__name__,
                                  str(obj.id))

    log.info('Creating alias "{0}" on index "{1}"'.format(
        es.index_name, index_name))
    if es.indices.exists_alias(name=es.index_name):
        alias = es.indices.get_alias(name=es.index_name)
        previous_indices = alias.keys()
        if index_name not in previous_indices:
            es.indices.put_alias(index=index_name, name=es.index_name)
        for index in previous_indices:
            if index != index_name:
                es.indices.delete_alias(index=index, name=es.index_name)
                if delete:
                    es.indices.delete(index=index)
    else:
        es.indices.put_alias(index=index_name, name=es.index_name)
Пример #3
0
def iter_adapters():
    '''Iter over adapter in predictable way'''
    adapters = adapter_catalog.values()
    return sorted(adapters, key=lambda a: a.model.__name__)
Пример #4
0
def iter_adapters():
    '''Iter over adapter in predictable way'''
    adapters = adapter_catalog.values()
    return sorted(adapters, key=lambda a: a.model.__name__)