forked from kanedata/find-that-charity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_elasticsearch.py
82 lines (69 loc) · 2.87 KB
/
create_elasticsearch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import argparse
from elasticsearch import Elasticsearch
import os
INDEXES = [
{
"name": "charitysearch",
"mapping": [
"charity", {
"properties": {
"geo": {
"properties": {
"location": {
"type": "geo_point"
}
}
},
"names": {
"type": "nested",
"properties": {
"type": {"type": "string"},
"source": {"type": "string"},
"name": {"type": "string"}
}
},
"complete_names": {
"type": "completion"
}
}
}
]
}
]
def main():
parser = argparse.ArgumentParser(description='Setup elasticsearch indexes.')
parser.add_argument('--reset', action='store_true',
help='If set, any existing indexes will be deleted and recreated.')
# elasticsearch options
parser.add_argument('--es-host', default="localhost", help='host for the elasticsearch instance')
parser.add_argument('--es-port', default=9200, help='port for the elasticsearch instance')
parser.add_argument('--es-url-prefix', default='', help='Elasticsearch url prefix')
parser.add_argument('--es-use-ssl', action='store_true', help='Use ssl to connect to elasticsearch')
parser.add_argument('--es-index', default='charitysearch', help='index used to store charity data')
parser.add_argument('--es-type', default='charity', help='type used to store charity data')
args = parser.parse_args()
es = Elasticsearch(host=args.es_host, port=args.es_port, url_prefix=args.es_url_prefix, use_ssl=args.es_use_ssl)
potential_env_vars = [
"ELASTICSEARCH_URL",
"ES_URL",
"BONSAI_URL"
]
for e_v in potential_env_vars:
if os.environ.get(e_v):
es = Elasticsearch(os.environ.get(e_v))
break
INDEXES[0]["name"] = args.es_index
INDEXES[0]["mapping"][0] = args.es_type
for i in INDEXES:
if es.indices.exists(i["name"]) and args.reset:
print("[elasticsearch] deleting '%s' index..." % (i["name"]))
res = es.indices.delete(index=i["name"])
print("[elasticsearch] response: '%s'" % (res))
if not es.indices.exists(i["name"]):
print("[elasticsearch] creating '%s' index..." % (i["name"]))
res = es.indices.create(index=i["name"])
if "mapping" in i:
res = es.indices.put_mapping(i["mapping"][0], i["mapping"][1], index=i["name"])
print("[elasticsearch] set mapping on %s index" % (i["name"]))
if __name__ == '__main__':
main()