Skip to content

Instantly share code, notes, and snippets.

@sjudeng
Last active November 27, 2018 12:33
Show Gist options
  • Save sjudeng/b26b10b2b0a87738c8b8a8f3e18494c4 to your computer and use it in GitHub Desktop.
Save sjudeng/b26b10b2b0a87738c8b8a8f3e18494c4 to your computer and use it in GitHub Desktop.
Script to create Elasticsearch index from ElasticGeo test data (gt-elasticsearch/src/test/resources)
#!/usr/bin/python
"""
Script re-creates status_s index for testing.
"""
import os,json,sys,tempfile
os.chdir(os.path.dirname(os.path.realpath(__file__)))
index_name = 'status_s3'
f_active = 'active_mappings.json'
f_inactive = 'inactive_mappings.json'
f_docs = 'wifiAccessPoint.json'
host = 'localhost'
port = '9200'
attributes = sys.argv[1].split(',') if len(sys.argv) > 1 else None
os.popen("curl -s -XDELETE 'http://%s:%s/%s/'" % (host,port,index_name)).read()
os.popen("curl -s -XPUT 'http://%s:%s/%s/'" % (host, port, index_name)).read()
if attributes:
with open(f_active) as f:
active_mappings = json.loads(f.read())
properties = dict(active_mappings['active']['properties'])
for key in active_mappings['active']['properties']:
if key not in attributes:
del properties[key]
active_mappings['active']['properties'] = properties
fd,name = tempfile.mkstemp()
os.write(fd, bytes(json.dumps(active_mappings), 'utf-8'))
os.close(fd)
os.popen("curl -s -H \"Content-Type: application/json\" -XPUT 'http://%s:%s/%s/_mapping/active' -d@%s" % (host, port, index_name, name)).read()
else:
os.popen("curl -s -H \"Content-Type: application/json\" -XPUT 'http://%s:%s/%s/_mapping/active' -d@%s" % (host, port, index_name, f_active)).read()
#os.popen("curl -s -H \"Content-Type: application/json\" -XPUT 'http://%s:%s/%s/_mapping/not-active' -d@%s" % (host, port, index_name, f_inactive)).read()
lines = [i for i in open(f_docs).read().strip().split('\n') if i[0] != '#']
features = json.loads(''.join(lines))['features']
for item in features:
id = str(item['id'])
type_name = str(item['status_s'])
if type_name != 'active':
continue
item = dict(item)
if attributes:
keys = list(item.keys())
for key in keys:
if key not in attributes:
del item[key]
cmd = "curl -s -H \"Content-Type: application/json\" -XPUT 'http://%s:%s/%s/%s/%s' -d '\n%s\n'"
line = json.dumps(item)
os.popen(cmd % (host, port, index_name, type_name, id, line)).read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment