Exemplo n.º 1
0
def configure_index(index, mapping, settings):
    """Create or update a search index with the given mapping and
    settings. This will try to make a new index, or update an
    existing mapping with new properties.
    """
    if es.indices.exists(index=index):
        log.info("Configuring index: %s...", index)
        options = {
            'index': index,
            'timeout': MAX_TIMEOUT,
            'master_timeout': MAX_TIMEOUT
        }
        config = es.indices.get(index=index).get(index, {})
        mapping = rewrite_mapping_safe(mapping, config.get('mappings'))
        res = es.indices.put_mapping(body=mapping, ignore=[400], **options)
        if not _check_response(index, res):
            return False
        settings.get('index').pop('number_of_shards')
        if check_settings_changed(settings, config.get('settings')):
            res = es.indices.close(ignore_unavailable=True, **options)
            res = es.indices.put_settings(body=settings, **options)
            if not _check_response(index, res):
                return False
        res = es.indices.open(**options)
        return True
    else:
        log.info("Creating index: %s...", index)
        body = {
            'settings': settings,
            'mappings': mapping
        }
        res = es.indices.create(index, body=body, ignore=[400])
        return True
Exemplo n.º 2
0
Arquivo: util.py Projeto: we1l1n/aleph
def configure_index(index, mapping, settings):
    """Create or update a search index with the given mapping and
    settings. This will try to make a new index, or update an
    existing mapping with new properties.
    """
    if es.indices.exists(index=index):
        log.info("Configuring index: %s...", index)
        options = {
            'index': index,
            'timeout': MAX_TIMEOUT,
            'master_timeout': MAX_TIMEOUT
        }
        res = es.indices.close(ignore_unavailable=True,
                               **options)
        res = es.indices.put_mapping(body=mapping,
                                     ignore=[400],
                                     **options)
        _check_response(index, res)
        settings.get('index').pop('number_of_shards')
        res = es.indices.put_settings(body=settings,
                                      ignore=[400],
                                      **options)
        _check_response(index, res)
        res = es.indices.open(**options)
        return True
    log.info("Creating index: %s...", index)
    res = es.indices.create(index, body={
        'settings': settings,
        'mappings': mapping
    }, ignore=[400])
    return True