-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
248 lines (222 loc) · 8.19 KB
/
main.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import time
from flask import Flask, request, jsonify
import pymysql
import os
from dotenv import load_dotenv
import math
import json
from elasticsearch import Elasticsearch, helpers
load_dotenv(".env.local")
app = Flask(__name__)
es_auth = ("elastic", os.environ.get("ELASTIC_PASSWORD"))
es_base_url = f"http://elasticsearch:{os.environ.get('ES_PORT')}"
es_client = Elasticsearch(es_base_url, http_auth=es_auth)
@app.route("/", methods=["GET"])
def home():
return "<h1>Welcome to sunnah.com search api.</h1>"
def create_and_update_index(index_name, documents, fields_to_not_index):
settings = {
"index": {
"number_of_shards": 1,
"analysis": {
"analyzer": {
"trigram": {
"type": "custom",
"tokenizer": "standard",
"char_filter": ["html_strip"],
"filter": ["lowercase", "stop", "shingle"],
},
"synonym": {
"type": "custom",
"tokenizer": "standard",
"char_filter": ["html_strip"],
"filter": [
"lowercase",
"stop",
"synonyms_filter",
"stemmer",
],
},
"custom_arabic": {
"tokenizer": "standard",
"char_filter": ["html_strip"],
"filter": [
"lowercase",
"decimal_digit",
"arabic_normalization",
"arabic_stemmer",
"shingle"
]
}
},
"filter": {
# 2-3 word shingles for better suggestions
"shingle": {
"type": "shingle",
"min_shingle_size": 2,
"max_shingle_size": 3,
"output_unigrams": True
},
"synonyms_filter": {
"type": "synonym",
"lenient": True,
"synonyms_path": "synonyms.txt",
},
"arabic_stemmer": {
"type": "stemmer",
"language": "arabic"
},
"arabic_stop": {
"type": "stop",
"stopwords": "_arabic_"
},
},
},
}
}
mappings = {
"properties": {
field: {"type": "text", "index": False} for field in fields_to_not_index
}
|
# Configurating field for suggestions
{
"hadithText": {
"type": "text",
"analyzer": "synonym",
"fields": {
"trigram": {"type": "text", "analyzer": "trigram"},
},
}
}
| {"arabicText": {"type": "text", "analyzer": "custom_arabic"}}
}
if es_client.indices.exists(index=index_name):
es_client.indices.delete(index=index_name)
es_client.indices.create(index=index_name, mappings=mappings, settings=settings)
successCount, errors = helpers.bulk(es_client, documents, index=index_name)
return successCount, errors
def get_suggest_query(suggest_field):
return {
"field": suggest_field,
"size": 3,
"gram_size": 3,
"direct_generator": [
{"field": suggest_field, "suggest_mode": "missing"}
],
"highlight": {"pre_tag": "<em>", "post_tag": "</em>"},
"collate": {
"query": {
"source": {
"match": {suggest_field: "{{suggestion}}"}
}
},
# Only return suggestions with a query match
"prune": False,
},
}
@app.route("/index", methods=["GET"])
def index():
start = time.time()
if request.args.get("password") != os.environ.get("INDEXING_PASSWORD"):
return "Must provide valid password to index", 401
connection = pymysql.connect(
host=os.environ.get("MYSQL_HOST"),
user=os.environ.get("MYSQL_USER"),
password=os.environ.get("MYSQL_PASSWORD"),
database=os.environ.get("MYSQL_DATABASE"),
)
cursor = connection.cursor(pymysql.cursors.DictCursor)
# Arabic Hadiths
cursor.execute(
"""SELECT arabicURN as urn, collection, hadithNumber, hadithText as arabicText,
matchingEnglishURN, "ar" as lang, grade1 as grade FROM ArabicHadithTable"""
)
arabicHadiths = cursor.fetchall()
arabicOnlyHadiths = []
matchingArabicHadiths = {}
for arabicHadith in arabicHadiths:
if arabicHadith["matchingEnglishURN"] == 0:
arabicOnlyHadiths.append(arabicHadith)
else:
matchingArabicHadiths[arabicHadith["matchingEnglishURN"]] = arabicHadith
# English Hadiths
cursor.execute(
"""SELECT englishURN as urn, collection, hadithText,
matchingArabicURN, "en" as lang, grade1 as grade FROM EnglishHadithTable"""
)
englishHadiths = cursor.fetchall()
# Add arabic text and hadithNumber to english hadith
for englishHadith in englishHadiths:
if englishHadith["urn"] not in matchingArabicHadiths:
continue
matchingArabic = matchingArabicHadiths[englishHadith["urn"]]
englishHadith["arabicText"] = matchingArabic["arabicText"]
englishHadith["arabicGrade"] = matchingArabic["grade"]
englishHadith["hadithNumber"] = matchingArabic["hadithNumber"]
indexingSuccessCount, indexingErrors = create_and_update_index(
"english", englishHadiths + arabicOnlyHadiths, ["urn", "matchingArabicURN", "lang"]
)
connection.close()
return {
"all_hadith_index_results": {
"success_count": indexingSuccessCount,
"failed": json.dumps(indexingErrors),
},
"arabic_only": {
"count": len(arabicOnlyHadiths),
},
"timeInSeconds": time.time() - start
}
def get_filter_from_args(args):
filters = []
collection = args.getlist("collection")
if collection:
filters.append({"terms": {"collection": collection}})
grade = args.getlist("grade")
if grade:
filters.append({"terms": {"grade": grade}})
return filters
@app.route("/<language>/search", methods=["GET"])
def search(language):
query = request.args.get("q")
filter = get_filter_from_args(request.args)
# TODO: Query string has a strict syntax and can cause failures when character like ":" appear in a search query.
# It's not recomended for search. But it's what allows us to do "AND collection:bukhari" or "AND hadithNumber:123" in the search bar
# Could be better to expose all those fields as filters instead and move away from query_string
query_string = {
"query_string": {
"query": query,
"type": "cross_fields",
"fields": ["hadithNumber^2", "hadithText", "arabicText", "collection^2"],
}
}
# Complete query with must (for search) and filter (for exact matches, collection, grade, etc)
bool_query = {
"bool": {
"filter": filter,
"must": [
query_string
],
}
}
return jsonify(
es_client.search(
index=language,
query=bool_query,
from_=request.args.get("from", 0),
size=request.args.get("size", 10),
highlight={"number_of_fragments": 0, "fields": {"*": {}}},
suggest= {
"text": query,
"english": {
"phrase": get_suggest_query("hadithText.trigram"),
},
"arabic": {
"phrase": get_suggest_query("arabicText"),
},
},
).body
)
if __name__ == "__main__":
app.run(host="0.0.0.0")