-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.py
executable file
·53 lines (43 loc) · 1.35 KB
/
server.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import tornado.web
from tornado.ioloop import IOLoop
from pyelasticsearch import ElasticSearch
from settings import HOST, INDEX, DOCTYPE
es = ElasticSearch(HOST)
class SearchHandler(tornado.web.RequestHandler):
def post(self):
dsl = {
'query': {
'bool': {
'should': [
{'match': {'content': self.get_argument('q')}},
{'match': {'title': self.get_argument('q')}},
]
}
},
'highlight': {
'pre_tags': ['<em>'],
'post_tags': ['</em>'],
'fields': {
'content': {'no_match_size': 150, 'number_of_fragments': 1},
'title': {'no_match_size': 150, 'number_of_fragments': 0},
}
}
}
results = es.search(dsl, index=INDEX, doc_type=DOCTYPE)
hits = results['hits']['hits']
self.write(json.dumps(hits))
def main():
app = tornado.web.Application(
[
(r'/()$', tornado.web.StaticFileHandler, {'path': 'templates/index.html'}),
(r'/search', SearchHandler),
],
debug=True,
)
app.listen(9528)
IOLoop.instance().start()
if __name__ == '__main__':
main()