forked from datenanfragen/data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-typesense-collection.py
More file actions
54 lines (41 loc) · 1.59 KB
/
Copy pathsetup-typesense-collection.py
File metadata and controls
54 lines (41 loc) · 1.59 KB
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
import json
import os
import typesense
# This only needs to be run once to setup the collection in Typesense (or when the schema is updated).
# IMPORTANT: It will *drop* previously setup collections, *including* their records.
client = typesense.Client({
'master_node': {
'host': 'search.datenanfragen.de',
'port': '443',
'protocol': 'https',
'api_key': os.environ['TYPESENSE_API_KEY']
},
'timeout_seconds': 2
})
def get_fields_for_props(props):
fields = []
for prop, details in props:
prop_type = ''
if details["type"] == 'string': prop_type = 'string'
elif details['type'] == 'array':
if details['items'] and details['items']['type'] != 'string': continue
prop_type = 'string[]'
else: prop_type = 'string'
fields.append({'name': prop, 'type': prop_type})
return fields
def setup_index(index, schema_filename):
try:
client.collections[index].delete()
except typesense.exceptions.ObjectNotFound:
pass
schema = json.load(open(schema_filename, encoding = 'utf-8'))
fields = get_fields_for_props(schema['properties'].items())
fields.append({'name': 'id', 'type': 'string'})
fields.append({'name': 'sort-index', 'type': 'int32'})
client.collections.create({
'name': index,
'fields': fields,
'default_sorting_field': 'sort-index' # TODO: Once we have the appropriate data, we can have an actual weight here.
})
setup_index('companies', 'schema.json')
setup_index('supervisory-authorities', 'schema-supervisory-authorities.json')