This repository was archived by the owner on Jan 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
47 lines (34 loc) · 1.33 KB
/
main.py
File metadata and controls
47 lines (34 loc) · 1.33 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
import falcon
import json
from whoosh import index
from whoosh.qparser import QueryParser
class SearchResource(object):
def __init__(self):
self.types = json.load(open('types.json'))
self.ix = index.open_dir('types.idx')
self.query_parser = QueryParser('name', self.ix.schema)
def on_get(self, request, response):
"""
:param request: Request
:param response: Response
:return: Search result in JSON format
"""
# Convert query to lower case unicode string
query = unicode(request.get_param('query', True), "utf-8").lower()
query = self.query_parser.parse(query)
# Find matches and retrieve results by name from dict
with self.ix.searcher() as searcher:
matches = searcher.search(query, limit=25)
results = [self.types[match["name"]] for match in matches]
# Encode JSON and send response
encoded_results = json.dumps(results)
response.body = encoded_results
response.status = falcon.HTTP_200
response.content_type = "application/json"
response.append_header("Access-Control-Allow-Origin", "*")
# Initialize application
application = falcon.API()
# Instantiate search
search = SearchResource()
# Wire up route
application.add_route('/api/search/v1/market-type/', search)