This repository has been archived by the owner on Apr 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.py
124 lines (105 loc) · 3.36 KB
/
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
115
116
117
118
119
120
121
122
123
124
# coding:utf8
import json
from time import time
from datetime import datetime
from search import Search
from settings import handler, DEBUG
from util import gen_pages, pretty_date
from flask import Flask, request, render_template, jsonify
app = Flask(__name__)
app.debug = DEBUG
app.logger.addHandler(handler)
@app.route("/", methods=['GET'])
@app.route("/api", methods=['GET'])
def index():
try:
_from = int(request.args.get('from', '0'))
limit = int(request.args.get('limit', '10'))
q = unicode(request.args.get('q', ''))
s = request.args.get('s', 'sumup')
raw = bool(request.args.get('raw', ''))
except Exception, e:
result = {
"status_code": 400,
"error": u"(T▽T) params error "
}
return jsonify(**result)
if len(q) == 0 and request.path == '/':
return render_template("index.html")
#: build search
search = Search(index='v2', doc_type='topic')
search['body']['query'] = \
{
"multi_match":
{
"query": "%s" % q,
"fields": ["title", "content", "rcontent"]
}
}
search['size'] = limit
search['from_'] = _from
if request.path == '/api':
#: field exclude for api
search['_source_exclude'] = ['content_rendered', 'rcontent']
#: choose a sort method from ['sumup','replies','created','match']
if s == 'match':
search['sort'] = '_score'
if s in ["replies", "created"]:
search['sort'] = "%s:desc" % s
if s == 'sumup':
if search['sort']:
del search['sort']
search['body']['sort'] = {
"_script": {
"script": "(doc['created'].value-1272124800000) * \
log10(doc['replies'].value+1)* log10(doc.score)",
"type": "number",
"params": {
"factor": 0
},
"order": "desc"
}
}
#: do search in es
try:
time0 = time()
result = search.exe()
time1 = time()
except Exception, e:
msg = str(datetime.now()) + '\n' + \
unicode(request.url) + '\n' + str(e) + '\r\n'
app.logger.error(msg)
result = {
"status_code": 500,
"error": "Sorry,server error T_T"
}
return jsonify(**result)
# [return] raw json
if raw and app.debug:
return jsonify(**result)
# [return] api request
if request.path == '/api':
# remove some keys from result
del_key = ['_shards', 'timed_out', 'took']
for k in del_key:
if k in result:
del result[k]
result['cost_ms'] = int((time1 - time0) * 1000)
return jsonify(**result)
# data for template
total = result['hits']['total']
current = _from / 10
max_page = total / 10
pages = gen_pages(current, max_page)
sort_by = {
"sumup": u"综合",
"match": u"精确匹配",
"created": u"创建时间",
"replies": u"回复数"
}
return render_template(
'result.html', res=result, pages=pages,
current=current, q=q, s=s, cost=time1-time0, pretty_date=pretty_date,
enumerate=enumerate, int=int, sort_by=sort_by, route="")
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)