forked from pyelasticsearch/pyelasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpyelasticsearch.py
480 lines (379 loc) · 16.5 KB
/
pyelasticsearch.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# -*- coding: utf-8 -*-
"""
NOTE: You should use the unit tests, not these doctests, which are harder to get running consistently.
I've left them here as documentation only, they are accurate as usage examples.
Create ElasticSearch connection
>>> conn = ElasticSearch('http://localhost:9200/')
Or a more verbose log level.
>>> import logging
>>> class VerboseElasticSearch(ElasticSearch):
... def setup_logging(self):
... log = super(VerboseElasticSearch, self).setup_logging()
... log.addHandler(logging.StreamHandler())
... log.setLevel(logging.DEBUG)
... return log
>>> conn = VerboseElasticSearch('http://localhost:9200/')
Add a few documents
>>> conn.index({"name":"Joe Tester"}, "test-index", "test-type", 1)
{'_type': 'test-type', '_id': '1', 'ok': True, '_index': 'test-index'}
>>> conn.index({"name":"Bill Baloney"}, "test-index", "test-type", 2)
{'_type': 'test-type', '_id': '2', 'ok': True, '_index': 'test-index'}
Get one
>>> conn.refresh(["test-index"]) # doctest: +ELLIPSIS
{'ok': True, '_shards': {...}}
>>> conn.get("test-index", "test-type", 1)
{'_type': 'test-type', '_id': '1', '_source': {'name': 'Joe Tester'}, '_index': 'test-index'}
Get a count
>>> conn.count("name:joe")
{'count': 1, '_shards': {'successful': 5, 'failed': 0, 'total': 5}}
Search
>>> conn.search("name:joe")
{'hits': {'hits': [{'_type': 'test-type', '_id': '1', '_source': {'name': 'Joe Tester'}, '_index': 'test-index'}], 'total': 1}, '_shards': {'successful': 5, 'failed': 0, 'total': 5}}
Terms
>>> conn.terms(['name'])
{'docs': {'max_doc': 2, 'num_docs': 2, 'deleted_docs': 0}, 'fields': {'name': {'terms': [{'term': 'baloney', 'doc_freq': 1}, {'term': 'bill', 'doc_freq': 1}, {'term': 'joe', 'doc_freq': 1}, {'term': 'tester', 'doc_freq': 1}]}}, '_shards': {'successful': 5, 'failed': 0, 'total': 5}}
>>> conn.terms(['name'], indexes=['test-index'])
{'docs': {'max_doc': 2, 'num_docs': 2, 'deleted_docs': 0}, 'fields': {'name': {'terms': [{'term': 'baloney', 'doc_freq': 1}, {'term': 'bill', 'doc_freq': 1}, {'term': 'joe', 'doc_freq': 1}, {'term': 'tester', 'doc_freq': 1}]}}, '_shards': {'successful': 5, 'failed': 0, 'total': 5}}
>>> conn.terms(['name'], min_freq=2)
{'docs': {'max_doc': 2, 'num_docs': 2, 'deleted_docs': 0}, 'fields': {'name': {'terms': []}}, '_shards': {'successful': 5, 'failed': 0, 'total': 5}}
More Like This
>>> conn.index({"name":"Joe Test"}, "test-index", "test-type", 3)
{'_type': 'test-type', '_id': '3', 'ok': True, '_index': 'test-index'}
>>> conn.refresh(["test-index"]) # doctest: +ELLIPSIS
{'ok': True, '_shards': {...}}
>>> conn.morelikethis("test-index", "test-type", 1, ['name'], min_term_freq=1, min_doc_freq=1)
{'hits': {'hits': [{'_type': 'test-type', '_id': '3', '_source': {'name': 'Joe Test'}, '_index': 'test-index'}], 'total': 1}, '_shards': {'successful': 5, 'failed': 0, 'total': 5}}
>>> conn.delete("test-index", "test-type", 3)
{'_type': 'test-type', '_id': '3', 'ok': True, '_index': 'test-index'}
Delete Bill
>>> conn.delete("test-index", "test-type", 2)
{'_type': 'test-type', '_id': '2', 'ok': True, '_index': 'test-index'}
>>> conn.delete_by_query("test-index, "test-type", {"query_string": {"query": "name:joe OR name:bill"}})
{'ok': True, '_indices': {'test-index': {'_shards': {'successful': 5, 'failed': 0, 'total': 5}}}}
Delete the index
>>> conn.delete_index("test-index")
{'acknowledged': True, 'ok': True}
Create the index anew
>>> conn.create_index("test-index")
{'acknowledged': True, 'ok': True}
Try (and fail) to create an existing index
>>> conn.create_index("test-index")
{'error': '[test-index] Already exists'}
Put mapping
>>> conn.put_mapping("test-type", {"test-type" : {"properties" : {"name" : {"type" : "string", "store" : "yes"}}}})
{'acknowledged': True, 'ok': True}
Get status
>>> conn.status(["test-index"]) # doctest: +ELLIPSIS
{'indices': {'test-index': ...}}
>>> conn.flush(["test-index"]) # doctest: +ELLIPSIS
{'ok': True, '_shards': {...}}
>>> conn.refresh(["test-index"]) # doctest: +ELLIPSIS
{'ok': True, '_shards': {...}}
>>> conn.optimize(["test-index"]) # doctest: +ELLIPSIS
{'ok': True, '_shards': {...}}
Test adding with automatic id generation
>>> conn.index({"name":"Joe Tester"}, "test-index", "test-type") # doctest: +ELLIPSIS
{'_type': 'test-type', '_id': '...', 'ok': True, '_index': 'test-index'}
"""
import datetime
import logging
import re
import requests
from urllib import urlencode
try:
# For Python < 2.6 or people using a newer version of simplejson
import simplejson as json
except ImportError:
# For Python >= 2.6
import json
__author__ = 'Robert Eanes'
__all__ = ['ElasticSearch']
__version__ = (0, 0, 3)
def get_version():
return "%s.%s.%s" % __version__
DATETIME_REGEX = re.compile('^(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})T(?P<hour>\d{2}):(?P<minute>\d{2}):(?P<second>\d{2})(\.\d+)?$')
class ElasticSearchError(Exception):
pass
class NullHandler(logging.Handler):
def emit(self, record):
pass
class ElasticSearch(object):
"""
ElasticSearch connection object.
"""
def __init__(self, url, timeout=60):
self.url = url
self.timeout = timeout
if self.url.endswith('/'):
self.url = self.url[:-1]
self.log = self.setup_logging()
def setup_logging(self):
"""
Sets up the logging.
Done as a method so others can override as needed without complex
setup.
"""
log = logging.getLogger('pyelasticsearch')
null = NullHandler()
log.addHandler(null)
log.setLevel(logging.ERROR)
return log
def _make_path(self, path_components):
"""
Smush together the path components. Empty components will be ignored.
"""
path_components = [str(component) for component in path_components if component]
path = '/'.join(path_components)
if not path.startswith('/'):
path = '/'+path
return path
def _build_url(self, path):
return self.url + path
def _send_request(self, method, path, body="", querystring_args=None, prepare_body=True):
if querystring_args:
path = "?".join([path, urlencode(querystring_args)])
kwargs = {
'timeout': self.timeout,
}
url = self._build_url(path)
if body:
if prepare_body:
body = self._prep_request(body)
kwargs['data'] = body
if not hasattr(requests, method.lower()):
raise ElasticSearchError("No such HTTP Method '%s'!" % method.lower())
self.log.debug("making %s request to path: %s %s with body: %s" % (method, url, path, kwargs.get('data', {})))
req_method = getattr(requests, method.lower())
try:
resp = req_method(url, **kwargs)
except requests.ConnectionError, e:
raise ElasticSearchError("Connecting to %s failed: %s." % (url, e))
self.log.debug("response status: %s" % resp.status_code)
prepped_response = self._prep_response(resp.content)
if resp.status_code >= 400:
raise ElasticSearchError("Non-OK status code returned (%d) containing %r." % (resp.status_code, prepped_response.get('error', prepped_response)))
self.log.debug("got response %s" % prepped_response)
return prepped_response
def _prep_request(self, body):
"""
Encodes body as json.
"""
try:
return json.dumps(body)
except (TypeError, json.JSONDecodeError), e:
raise ElasticSearchError('Invalid JSON %r' % body, e)
def _prep_response(self, response):
"""
Parses json to a native python object.
"""
try:
return json.loads(response)
except (TypeError, json.JSONDecodeError), e:
raise ElasticSearchError('Invalid JSON %r' % response, e)
def _query_call(self, query_type, query, body=None, indexes=['_all'], doc_types=[], **query_params):
"""
This can be used for search and count calls.
These are identical api calls, except for the type of query.
"""
querystring_args = query_params
if query:
querystring_args['q'] = query
path = self._make_path([','.join(indexes), ','.join(doc_types),query_type])
response = self._send_request('GET', path, body, querystring_args)
return response
## REST API
def index(self, doc, index, doc_type, id=None, force_insert=False):
"""
Index a typed JSON document into a specific index and make it searchable.
"""
if force_insert:
querystring_args = {'op_type':'create'}
else:
querystring_args = {}
if id is None:
request_method = 'POST'
else:
request_method = 'PUT'
path = self._make_path([index, doc_type, id])
response = self._send_request(request_method, path, doc, querystring_args)
return response
def bulk_index(self, index, doc_type, docs, id_field="id"):
"""
Indexes a list of documents as efficiently as possible.
"""
body_bits = []
if not len(docs):
raise ElasticSearchError("No documents provided for bulk indexing!")
for doc in docs:
action = {"index": {"_index": index, "_type": doc_type}}
if doc.get(id_field):
action['index']['_id'] = doc[id_field]
body_bits.append(self._prep_request(action))
body_bits.append(self._prep_request(doc))
path = self._make_path(['_bulk'])
# Need the trailing newline.
body = '\n'.join(body_bits) + '\n'
response = self._send_request('POST', path, body, {'op_type':'create'}, prepare_body=False)
return response
def delete(self, index, doc_type, id=None):
"""
Delete a typed JSON document from a specific index based on its id.
If id is omitted, all documents of a given doctype will be deleted.
"""
path_parts = [index, doc_type]
if id:
path_parts.append(id)
path = self._make_path(path_parts)
response = self._send_request('DELETE', path)
return response
def delete_by_query(self, index, doc_type, query):
"""
Delete a typed JSON documents from a specific index based on query
"""
path = self._make_path([index, doc_type, '_query'])
response = self._send_request('DELETE', path, query)
return response
def get(self, index, doc_type, id):
"""
Get a typed JSON document from an index based on its id.
"""
path = self._make_path([index, doc_type, id])
response = self._send_request('GET', path)
return response
def search(self, query, body=None, indexes=['_all'], doc_types=[], **query_params):
"""
Execute a search query against one or more indices and get back search hits.
query must be a dictionary that will convert to Query DSL
TODO: better api to reflect that the query can be either 'query' or 'body' argument.
"""
return self._query_call("_search", query, body, indexes, doc_types, **query_params)
def count(self, query, body=None, indexes=['_all'], doc_types=[], **query_params):
"""
Execute a query against one or more indices and get hits count.
"""
return self._query_call("_count", query, body, indexes, doc_types, **query_params)
def get_mapping(self, indexes=['_all'], doc_types=[]):
"""
Fetches the existing mapping definition for a specific index & type.
"""
path = self._make_path([','.join(indexes), ','.join(doc_types), "_mapping"])
response = self._send_request('GET', path)
return response
def put_mapping(self, doc_type, mapping, indexes=['_all'], **query_params):
"""
Register specific mapping definition for a specific type against one or more indices.
"""
path = self._make_path([','.join(indexes), doc_type,"_mapping"])
response = self._send_request('PUT', path, mapping, **query_params)
return response
def morelikethis(self, index, doc_type, id, fields, **query_params):
"""
Execute a "more like this" search query against one or more fields and get back search hits.
"""
path = self._make_path([index, doc_type, id, '_mlt'])
query_params['fields'] = ','.join(fields)
response = self._send_request('GET', path, querystring_args=query_params)
return response
## Index Admin API
def status(self, indexes=['_all']):
"""
Retrieve the status of one or more indices
"""
path = self._make_path([','.join(indexes), '_status'])
response = self._send_request('GET', path)
return response
def create_index(self, index, settings=None, quiet=True):
"""
Creates an index with optional settings.
Settings must be a dictionary which will be converted to JSON.
Elasticsearch also accepts yaml, but we are only passing JSON.
"""
try:
response = self._send_request('PUT', self._make_path([index]), settings)
except ElasticSearchError, e:
if not quiet:
raise
response = {"message": "Create index '%s' errored: %s" % (index, e)}
return response
def delete_index(self, index, quiet=True):
"""
Deletes an index.
"""
try:
response = self._send_request('DELETE', self._make_path([index]))
except ElasticSearchError, e:
if not quiet:
raise
response = {"message": "Delete index '%s' errored: %s" % (index, e)}
return response
def flush(self, indexes=['_all'], refresh=None):
"""
Flushes one or more indices (clear memory)
"""
path = self._make_path([','.join(indexes), '_flush'])
args = {}
if refresh is not None:
args['refresh'] = refresh
response = self._send_request('POST', path, querystring_args=args)
return response
def refresh(self, indexes=['_all']):
"""
Refresh one or more indices
"""
path = self._make_path([','.join(indexes), '_refresh'])
response = self._send_request('POST', path)
return response
def gateway_snapshot(self, indexes=['_all']):
"""
Gateway snapshot one or more indices
"""
path = self._make_path([','.join(indexes), '_gateway', 'snapshot'])
response = self._send_request('POST', path)
return response
def optimize(self, indexes=['_all'], **args):
"""
Optimize one ore more indices
"""
path = self._make_path([','.join(indexes), '_optimize'])
response = self._send_request('POST', path, querystring_args=args)
return response
def from_python(self, value):
"""
Converts Python values to a form suitable for ElasticSearch's JSON.
"""
if hasattr(value, 'strftime'):
if hasattr(value, 'hour'):
value = value.isoformat()
else:
value = "%sT00:00:00" % value.isoformat()
elif isinstance(value, str):
value = unicode(value, errors='replace')
return value
def to_python(self, value):
"""
Converts values from ElasticSearch to native Python values.
"""
if isinstance(value, (int, float, long, complex, list, tuple, bool)):
return value
if isinstance(value, basestring):
possible_datetime = DATETIME_REGEX.search(value)
if possible_datetime:
date_values = possible_datetime.groupdict()
for dk, dv in date_values.items():
date_values[dk] = int(dv)
return datetime.datetime(date_values['year'], date_values['month'], date_values['day'], date_values['hour'], date_values['minute'], date_values['second'])
try:
# This is slightly gross but it's hard to tell otherwise what the
# string's original type might have been. Be careful who you trust.
converted_value = eval(value)
# Try to handle most built-in types.
if isinstance(converted_value, (list, tuple, set, dict, int, float, long, complex)):
return converted_value
except:
# If it fails (SyntaxError or its ilk) or we don't trust it,
# continue on.
pass
return value
if __name__ == "__main__":
import doctest
doctest.testmod()