def _initialize_elasticsearch(es_host): dsl.connections.connections.create_connection(hosts=[es_host]) Country.init() Admin1.init() Admin2.init() Admin3.init() Place.init()
def index(csv_file_name): with open(csv_file_name) as csv_file: reader = csv.DictReader(csv_file, csv_field_names, delimiter=str('\t')) for row in reader: print( '[+] indexing place {} {}'.format( row['postal code'], row['place name'].decode('utf-8') ) ) country, created = Country.get_or_create( code=row['country code'] ) admin1, created = Admin1.get_or_create( code=row['admin code1'], name=row['admin name1'], _parent=country.meta.id, ) admin2, created = Admin2.get_or_create( code=row['admin code2'], name=row['admin name2'], _parent=admin1.meta.id, ) admin3, created = Admin3.get_or_create( code=row['admin code3'], name=row['admin name3'], _parent=admin2.meta.id, ) place, created = Place.get_or_create( postal_code=row['postal code'], place_name=row['place name'], defaults={ 'country': country.meta.id, 'postal_code': row['postal code'], 'place_name': row['place name'], 'admin1': admin1.meta.id, 'admin2': admin2.meta.id, 'admin3': admin3.meta.id, 'geopoint': (row['latitude'] +','+ row['longitude']) } )