elasticsearch bulk api

Snippets for creating an elasticsearch index using its bulk api (and python wrapper).

def bulk_index(infile_csv, index_name):
    from elasticsearch.helpers import bulk


    print 'Bulk indexing %s...' % infile_csv,
    bulk(client=_es, actions=_actions(infile_csv, index_name))
    print 'Done'

def _create_index(name):
    global _es

    body = {
        'mappings': {
            'teaser': {
                'properties': {
                    'foo': { 'type': 'integer' },
                    'bar': { 'type': 'float' },
                    # defaults to string if not specified
                    'baz': { 'type': 'string' },

    if not _es.indices.exists(name):
        print "Creating index: %s" % name
        _es.indices.create(name, body=body)
        print "Index: %s already exists" % name

def _actions(infile_csv, index_name):
    Return generator of actions for elasticsearch bulk indexing
    with open(infile_csv) as csv_file:
        reader = csv.DictReader(csv_file)
        for row in reader:
            mapping = {
                'foo': int,
                'bar': float,
            yield {k: mapping[k](v) if k in mapping else v for k,v in row.items()}