forked from AnthonySigogne/web-search-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
311 lines (269 loc) · 10.3 KB
/
index.py
File metadata and controls
311 lines (269 loc) · 10.3 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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
API - a simple web search engine.
The goal is to index an infinite list of URLs (web pages), and then be able to quickly search relevant URLs against a query.
- Indexing :
The indexing operation of a new URL first crawls URL, then extracts the title and main text content from the page.
Then, a new document representing the URL's data is saved in ElasticSearch, and goes for indexing.
- Searching :
When searching for relevant URLs, the search engine will compare the query with the data in each document (web page),
and retrieve a list of URLs matching the query and sorted by relevance.
This API works for a finite list of languages, see here for the complete list : https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-lang-analyzer.html.
"""
__author__ = "Anthony Sigogne"
__copyright__ = "Copyright 2017, Byprog"
__email__ = "anthony@byprog.com"
__license__ = "MIT"
__version__ = "1.0"
import re
import os
import url
import crawler
import requests
import json
import query
from flask import Flask, request, jsonify
from elasticsearch_dsl.connections import connections
from elasticsearch_dsl import Index, Search, Mapping
from language import languages
from redis import Redis
from rq import Queue
from rq.decorators import job
from scrapy.crawler import CrawlerProcess
from urllib.parse import urlparse
from datetime import datetime
# init flask app and import helper
app = Flask(__name__)
with app.app_context():
from helper import *
# initiate the elasticsearch connection
hosts = [os.getenv("HOST")]
http_auth = (os.getenv("USERNAME"), os.getenv("PASSWORD"))
port = os.getenv("PORT")
client = connections.create_connection(hosts=hosts, http_auth=http_auth, port=port)
# initiate Redis connection
redis_conn = Redis(os.getenv("REDIS_HOST", "redis"), os.getenv("REDIS_PORT", 6379))
# create indices and mappings
for lang in ["fr"] : #languages :
# index named "web-<language code>"
index = Index('web-%s'%lang)
if not index.exists() :
index.create()
# mapping of page
m = Mapping('page')
m.field('url', 'keyword')
m.field('domain', 'keyword')
m.field('title', 'text', analyzer=languages[lang])
m.field('description', 'text', analyzer=languages[lang])
m.field('body', 'text', analyzer=languages[lang])
m.field('weight', 'long')
#m.field('thumbnail', 'binary')
#m.field('keywords', 'completion') # -- TEST -- #
m.save('web-%s'%lang)
# index for misc mappings
index = Index('web')
if not index.exists() :
index.create()
# mapping of domain
m = Mapping('domain')
m.field('homepage', 'keyword')
m.field('domain', 'keyword')
m.field('email', 'keyword')
m.field('last_crawl', 'date')
#m.field('keywords', 'text', analyzer=languages[lang])
m.save('web')
@app.route("/index", methods=['POST'])
def index():
"""
URL : /index
Index a new URL in search engine.
Method : POST
Form data :
- url : the url to index [string, required]
Return a success message.
"""
# get POST data
data = dict((key, request.form.get(key)) for key in request.form.keys())
if "url" not in data :
raise InvalidUsage('No url specified in POST data')
# launch exploration job
index_job.delay(data["url"])
return "Indexing started"
@job('default', connection=redis_conn)
def index_job(link) :
"""
Index a single page.
"""
print("index page : %s"%link)
# get final url after possible redictions
try :
link = url.crawl(link).url
except :
return 0
process = CrawlerProcess({
'USER_AGENT': "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36",
'DOWNLOAD_TIMEOUT':100,
'REDIRECT_ENABLED':False,
'SPIDER_MIDDLEWARES' : {
'scrapy.spidermiddlewares.httperror.HttpErrorMiddleware':True
}
})
process.crawl(crawler.SingleSpider, start_urls=[link,], es_client=client, redis_conn=redis_conn)
process.start() # block until finished
@app.route("/explore", methods=['POST'])
def explore():
"""
URL : /explore
Explore a website and index all urls
Method : POST
Form data :
- url : the url to explore [string, required]
Return a success message (means redis-rq process launched).
"""
# get POST data
data = dict((key, request.form.get(key)) for key in request.form.keys())
if "url" not in data :
raise InvalidUsage('No url specified in POST data')
# launch exploration job
explore_job.delay(data["url"])
return "Exploration started"
@job('default', connection=redis_conn)
def explore_job(link) :
"""
Explore a website and index all urls (redis-rq process).
"""
print("explore website at : %s"%link)
# get final url after possible redictions
try :
link = url.crawl(link).url
except :
return 0
# create or update domain data
domain = url.domain(link)
res = client.index(index="web", doc_type='domain', id=domain, body={
"homepage":link,
"domain":domain,
"last_crawl":datetime.now()
})
# start crawler
process = CrawlerProcess({
'USER_AGENT': "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36",
'DOWNLOAD_TIMEOUT':100,
'DOWNLOAD_DELAY':0.25,
'ROBOTSTXT_OBEY':True,
'HTTPCACHE_ENABLED':False,
'REDIRECT_ENABLED':False,
'SPIDER_MIDDLEWARES' : {
'scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware':True,
'scrapy.spidermiddlewares.httperror.HttpErrorMiddleware':True,
'scrapy.downloadermiddlewares.httpcache.HttpCacheMiddleware':True,
'scrapy.extensions.closespider.CloseSpider':True
},
'CLOSESPIDER_PAGECOUNT':500 #only for debug
})
process.crawl(crawler.Crawler, allowed_domains=[urlparse(link).netloc], start_urls = [link,], es_client=client, redis_conn=redis_conn)
process.start()
return 1
@app.route("/reference", methods=['POST'])
def reference():
"""
URL : /reference
Request the referencing of a website.
Method : POST
Form data :
- url : url to website
- email : contact email
Return a success message.
"""
# get POST data
data = dict((key, request.form.get(key)) for key in request.form.keys())
if "url" not in data or "email" not in data :
raise InvalidUsage('No url or email specified in POST data')
# launch exploration job
reference_job.delay(data["url"], data["email"])
return "Referencing started"
@job('default', connection=redis_conn)
def reference_job(link, email) :
"""
Request the referencing of a website.
"""
print("referencing page %s with email %s"%(link,email))
# get final url after possible redictions
try :
link = url.crawl(link).url
except :
return 0
# create or update domain data
domain = url.domain(link)
res = client.index(index="web", doc_type='domain', id=domain, body={
"homepage":link,
"domain":domain,
"email":email
})
return 1
@app.route("/search", methods=['POST'])
def search():
"""
URL : /search
Query engine to find a list of relevant URLs.
Method : POST
Form data :
- query : the search query [string, required]
- hits : the number of hits returned by query [integer, optional, default:10]
- start : the start of hits [integer, optional, default:0]
Return a sublist of matching URLs sorted by relevance, and the total of matching URLs.
"""
def format_result(hit, highlight) :
# highlight title and description
title = hit["title"]
description = hit["description"]
if highlight :
if "description" in highlight :
description = highlight["description"][0]+"..."
elif "body" in highlight :
description = highlight["body"][0]+"..."
"""if "title" in highlight :
title = highlight["title"][0]"""
# create false title and description for better user experience
if not title :
title = hit["domain"]
if not description :
description = url.create_description(hit["body"])+"..."
return {
"title":title,
"description":description,
"url":hit["url"],
"thumbnail":hit.get("thumbnail", None)
}
# get POST data
data = dict((key, request.form.get(key)) for key in request.form.keys())
if "query" not in data :
raise InvalidUsage('No query specified in POST data')
start = int(data.get("start", "0"))
hits = int(data.get("hits", "10"))
if start < 0 or hits < 0 :
raise InvalidUsage('Start or hits cannot be negative numbers')
# analyze user query
groups = re.search("(site:(?P<domain>[^ ]+))?( ?(?P<query>.*))?",data["query"]).groupdict()
if groups.get("query", False) and groups.get("domain", False) :
# expression in domain query
response = client.search(index="web-*", doc_type="page", body=query.domain_expression_query(groups["domain"], groups["query"]), from_=start, size=hits)
results = [format_result(hit["_source"], hit.get("highlight", None)) for hit in response["hits"]["hits"]]
total = response["hits"]["total"]
elif groups.get("domain", False) :
# domain query
response = client.search(index="web-*", doc_type="page", body=query.domain_query(groups["domain"]), from_=start, size=hits)
results = [format_result(hit["_source"], None) for hit in response["hits"]["hits"]]
total = response["hits"]["total"]
elif groups.get("query", False) :
# expression query
response = client.search(index="web-*", doc_type="page", body=query.expression_query(groups["query"]))
results = []
for domain_bucket in response['aggregations']['per_domain']['buckets']:
for hit in domain_bucket["top_results"]["hits"]["hits"] :
results.append((format_result(hit["_source"], hit.get("highlight", None)),hit["_score"]))
results = [result[0] for result in sorted(results, key=lambda result: result[1], reverse=True)]
total = len(results)
results = results[start:start+hits]
return jsonify(total=total, results=results)