-
Notifications
You must be signed in to change notification settings - Fork 0
/
elasticsearch.py
90 lines (68 loc) · 2.27 KB
/
elasticsearch.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
import httplib
import hashlib
import simplejson
from flask import request
from app import app
#from django.core.cache import cache
caching = False
es_host = app.config['ES_HOST']
def get_dimensions(index):
""" Get all dimensions """
# Build query for all types in index
params = {
'size': 0,
'facets': {
'types': {
'terms': {
'field': '_type'
}
}
}
}
# Make request
response = request('search', index, None, params)
# Map dimensions to list
dimensions = map(lambda x: x['term'], response['facets']['types']['terms'])
# Remove "observation" as this is the data itself (not a dimension)
dimensions = filter(lambda x: x != 'observation', dimensions)
return dimensions
def request(action, index, type, params):
""" Make request to elastic search REST API"""
# Build path for REST API
path = '/' + index + '/'
if type is not None:
path += type + '/'
path += '_' + action
# Generate JSON representation of params
params = simplejson.dumps(params)
# Generate caching key by hashing the request's path and parameters
# TODO: Find a better way to do this
cache_key = hashlib.md5(path + ':' + params).hexdigest()
# Attempt to get response from cache
if caching:
obj = cache.get(cache_key)
if obj is not None:
return obj
try:
# Connect to API
conn = httplib.HTTPConnection(es_host)
conn.set_debuglevel(100)
# Make the request:
# Note that according to the elastic search specs this should be a GET request
# but HTTPConnection doesn't seem to support GET requests that have request bodies
conn.request('POST', path, params)
# Read response
response = conn.getresponse()
except httplib.HTTPException:
raise ElasticSearchException('Unable to contact elastic search API')
# Load in reponse JSON
try:
obj = simplejson.loads(response.read())
except simplejson.JSONDecodeError:
raise ElasticSearchException('Unable to parse response as JSON')
# Cache response
if caching:
cache.add(cache_key, obj)
return obj
class ElasticSearchException(Exception):
pass