-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_corpus.py
executable file
·209 lines (174 loc) · 6.08 KB
/
web_corpus.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
#
# Copyright (c) 2022 Jordi Mas i Hernandez <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
from flask import Flask, request, Response
from elasticsearch import Elasticsearch
import logging
import logging.handlers
import os
import time
import json
from datetime import datetime
app = Flask(__name__)
HTTP_OK = 200
HTTP_ERROR = 500
def init_logging():
logfile = 'web_corpus.log'
LOGLEVEL = os.environ.get('LOGLEVEL', 'INFO').upper()
logger = logging.getLogger()
hdlr = logging.handlers.RotatingFileHandler(logfile, maxBytes=1024*1024, backupCount=1)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(LOGLEVEL)
console = logging.StreamHandler()
console.setLevel(LOGLEVEL)
logger.addHandler(console)
# API calls
def json_answer(data):
resp = Response(data, mimetype='application/json')
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
def json_answer_status(data, status):
resp = json_answer(data)
resp.status = str(status)
return resp
@app.route('/hello', methods=['GET'])
def hello_api():
data={"data":"Hello World"}
return data
def get_query(field, word, size):
query_body = {
"query": {
"function_score": {
"query": {
"bool": {
"should": [
{
"match_phrase": {
f"{field}": {
"query": f"{word}",
"boost": 2
}
}
},
{
"match": {
f"{field}": {
"query": f"{word}"
}
}
}
]
}
},
"field_value_factor": {
"field" : "prio",
"modifier": "reciprocal"
}
}
},
"highlight": {
"fields": {
f"{field}": {
"number_of_fragments" : 0
}
}
},
"size": size,
}
return query_body
@app.route('/index_stats/', methods=['GET'])
def index_stats_api():
results = {}
status = HTTP_OK
try:
INDEX_NAME = 'eng-cat'
es = Elasticsearch('es01:9200', timeout=30)
res = es.indices.stats(index=INDEX_NAME)
docs = res['indices'][INDEX_NAME]['total']['docs']['count']
size_in_bytes = res['indices'][INDEX_NAME]['primaries']['store']['size_in_bytes']
size_in_GB = size_in_bytes / 1024/1024/1024
index_info = es.indices.get(index=INDEX_NAME)
creation_date = index_info[INDEX_NAME]["settings"]["index"]["creation_date"]
ts = int(creation_date) / 1000
creation_date = datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
results["indexed_docs"] = docs
results["index_size"] = f"{size_in_GB:.2f}GB"
results["creation_date"] = creation_date
except Exception as e:
err = str(e)
logging.error(f"Error: {err}")
results = {}
results['error'] = err
status = HTTP_ERROR
json_results = json.dumps(results, indent=4)
return json_answer_status(json_results, status)
@app.route('/search/', methods=['GET'])
def search_api():
start_time = time.time()
source = request.args.get('source')
target = request.args.get('target')
results = request.args.get('results')
if source is not None and len(source) > 0:
field = "src"
word = source
elif target is not None and len(target) > 0:
field = "trg"
word = target
else:
return "Invalid parameters", 400
if results is not None and len(results) > 0:
size = int(results)
size = min(size, 500)
else:
size = 10
try:
# es = Elasticsearch('http://localhost:9200', timeout=30)
es = Elasticsearch('es01:9200', timeout=30)
query_body = get_query(field, word, size)
res = es.search(index="eng-cat", body=query_body)
elapsed_time = time.time() - start_time
hits = res['hits']['hits']
results = []
for hit in hits:
item = {}
item['src'] = hit['_source']['src']
item['trg'] = hit['_source']['trg']
item['prio'] = hit['_source']['prio']
item['license'] = hit['_source']['license']
item['project'] = hit['_source']['project']
item['highlight'] = hit['highlight']
results.append(item)
status = HTTP_OK
logging.debug(f"/search for '{word}': {len(results)} results, time: {elapsed_time:.2f}s")
except Exception as e:
err = str(e)
logging.error(f"Error: {err}")
results = {}
results['error'] = err
status = HTTP_ERROR
json_results = json.dumps(results, indent=4)
return json_answer_status(json_results, status)
if __name__ == '__main__':
init_logging()
app.debug = True
app.run()
if __name__ != '__main__':
init_logging()