This repository has been archived by the owner on Jun 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
update_index.py
114 lines (103 loc) · 3.17 KB
/
update_index.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from flask import Flask
from models import db, Notice
import time
import os
app = Flask(__name__)
app.config.from_envvar('SETTINGS')
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(app.instance_path, 'app.db')
db.init_app(app)
INDEX = app.config['INDEX']
import pyes
es = pyes.ES('127.0.0.1:9200')
try:
#es.indices.delete_index(INDEX)
es.indices.create_index(INDEX)
time.sleep(5)
print "Created Index", INDEX
pass
except:
pass
mapping = {
'location_name': {
'type': 'string',
'fields': {
'raw' : {
'type': 'string',
'index': 'not_analyzed'
}
}
},
'location_path': {
'type': 'string',
'index': 'not_analyzed',
'fields': {
'tree': {
'type': 'string',
'analyzer': 'paths'
}
}
},
'buying_org': {
'type': 'string',
'fields': {
'raw' : {
'type': 'string',
'index': 'not_analyzed'
}
}
},
'business_name': {
'type': 'string',
'fields': {
'raw' : {
'type': 'string',
'index': 'not_analyzed'
}
}
},
}
settings = {
'analysis': {
'analyzer': {
'paths': {
'tokenizer': 'path_hierarchy'
}
}
}
}
es.indices.close_index(INDEX)
es.indices.update_settings(INDEX, settings)
es.indices.open_index(INDEX)
es.indices.put_mapping('notices', {'properties': mapping}, [INDEX])
with app.app_context():
for notice in Notice.query.all():
document = {}
document['id'] = notice.id
document['ref_no'] = notice.ref_no
document['title'] = notice.details.title
document['description'] = notice.details.description
document['buying_org'] = notice.details.buying_org
document['contact_address'] = notice.details.contact_address
document['min_value'] = notice.min_value
document['max_value'] = notice.max_value
if notice.location:
document['location_name'] = notice.location.name
location_path = notice.location.location_path()
document['location_path'] = location_path[len('/European Union'):]
else:
document['location_path'] = '/United Kingdom'
if notice.awards:
document['business_name'] = []
document['business_address'] = []
for award in notice.awards:
if not award.details or not award.details.business_name:
continue
document['business_name'].append(award.details.business_name)
document['business_address'].append(award.details.business_address)
if notice.date_awarded:
document['date_awarded'] = notice.date_awarded.strftime('%Y-%m-%d')
if notice.date_created:
document['date_created'] = notice.date_created.strftime('%Y-%m-%d')
if notice.deadline_date:
document['deadline_date'] = notice.deadline_date.strftime('%Y-%m-%d')
es.index(document, INDEX, 'notices', notice.id)