-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
73 lines (55 loc) · 1.72 KB
/
api.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
from flask import Flask, request, json
from pymongo import MongoClient
from bson.json_util import dumps
# from elasticsearch import Elasticsearch
app = Flask(__name__)
client = MongoClient('localhost', 27017)
# es = Elasticsearch({'host': 'localhost', 'port': 9200})
db = client.test.spaceObjects
MONGO_COMPARATORS = ['gt', 'gte', 'in', 'lt', 'lte', 'ne', 'nin']
# converts values into numbers if applicable
def parse(val):
try:
return float(val)
except Exception:
return val
# decorate query for mongodb
def transform_query(q):
for comparator in MONGO_COMPARATORS:
if comparator in q:
key = '${}'.format(comparator)
val = q.split(comparator)[1]
return {key: parse(val)}
return q
@app.route('/', methods=['GET'])
def search():
# Converts (request.args)ImmutableDict to dict
query_dict = json.loads(json.dumps(request.args))
object_types = query_dict.get('types')
if object_types:
object_types = object_types.split()
if len(object_types) > 1:
query_dict['types'] = {'$in': object_types}
for key in query_dict:
query_dict[key] = transform_query(query_dict.get(key))
print 'Query: {}'.format(query_dict)
print 'Results:'
obj_list = []
for item in db.find(query_dict):
obj_list.append(item)
print item
return dumps({'results': obj_list})
# trying out mongodb's full text search
@app.route('/fts/<string:query>', methods=['GET'])
def fts_search(query):
query.replace('galaxies', 'galaxy')
query.replace('stars', 'star')
query.replace('interstellar matter', 'interstellar_matter')
print 'Full Text Search:'
obj_list = []
for item in db.find({'$text': { '$search': query }}):
obj_list.append(item)
print item
return dumps({'results': obj_list})
if __name__ == '__main__':
app.run(debug=True)