-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-domain-indexes.py
executable file
·86 lines (75 loc) · 3.34 KB
/
create-domain-indexes.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
83
84
85
86
#!/usr/bin/env python
# Create Domain with 'title, and exact 'albums' with 'albums_text' suggester.
from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser
from collections import OrderedDict
import boto3
from lib import get_fields
# We must create 'albums' before 'albums_text' which uses 'albums' as its source
INDEXES = OrderedDict({
'albums': {
'IndexFieldName': 'albums',
'IndexFieldType': 'literal-array',
'LiteralArrayOptions': {'FacetEnabled': True,
'ReturnEnabled': True,
'SearchEnabled': True}},
'albums_text': {
'IndexFieldName': 'albums_text',
'IndexFieldType': 'text-array',
'TextArrayOptions': {'AnalysisScheme': '_en_default_',
'HighlightEnabled': True, # don't need, but OK
'ReturnEnabled': True,
'SourceFields': 'albums'}},
'title': {
'IndexFieldName': 'title',
'IndexFieldType': 'text',
'TextOptions': {'AnalysisScheme': '_en_default_',
'HighlightEnabled': True, # don't need, but OK
'ReturnEnabled': True,
'SortEnabled': True}},
})
def main(args):
csc = boto3.client('cloudsearch')
# Create the domain if it doesn't exist
res = csc.describe_domains(DomainNames=[args.domain])
if res['DomainStatusList'] == []:
print('Creating domain, this can take a while.')
csc.create_domain(DomainName=args.domain)
# Create or update the indexes
if res['DomainStatusList'] and not args.update:
print('Existing domain, use "--update" to update indexes')
exit(1)
print('Updating indices...')
fields_now = get_fields(args.domain)
need_reindex = False
for name, field in INDEXES.items():
if name in fields_now and field == fields_now[name]:
print('Skipping same definition for field=%s' % name)
continue
print('Update field=%s' % name)
if name not in fields_now:
print('Create field=%s' % field)
else:
print('Update field=%s' % field)
print('Current field=%s' % fields_now[name])
res = csc.define_index_field(DomainName=args.domain, IndexField=field)
state = res['IndexField']['Status']['State']
version = res['IndexField']['Status']['UpdateVersion']
print('Index "%s" State=%s Version=%s' % (field['IndexFieldName'], state, version))
if state == 'RequiresIndexDocuments':
need_reindex = True
print('Indexing docs...')
if need_reindex:
res = csc.index_documents(DomainName=args.domain)
print('Indexing fields: %s' % res['FieldNames'])
else:
print('Indexing docs not needed')
if __name__ == '__main__':
parser = ArgumentParser(description=('Create "title", and exact "albums" with humane'
' text "albums_text" suggester'),
formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument('-d', '--domain', required=True,
help='cloudsearch domain name')
parser.add_argument('-u', '--update', default=False, action='store_true',
help='cloudsearch domain name')
args = parser.parse_args()
main(args)