Migrating From pyes
¶
Moving your project from pyes
to pyelasticsearch
is easy, especially
for simple use cases. Here are some code changes that will aid your porting.
pyelasticsearch
requiresrequests
1.x. Breaking changes were introduced inrequests
1.0, so if your project was using a previous version, you may need to update your code. Most likely, you just need to changeresponse.json
toresponse.json()
.Instantiating the client should be as simple as changing the invocation…
pyes.ES(host, **kwargs)
…to…
pyelasticsearch.ElasticSearch(host, **kwargs)
pyelasticsearch
has no methodcreate_index_if_missing
. Instead, you’ll need catch the exception manually:try: connection.create_index(index='already_existing_index') except pyelasticsearch.IndexAlreadyExistsError as ex: print 'Index already exists, moving on...'
Instead of using
pyes
’s_send_request
, usesend_request()
. This also requires the path to be passed as an iterable instead of a string. For example…es._send_request('POST', 'my_index/my_doc_type', body)
…becomes…
connection.send_request('POST', ['my_index', 'my_doc_type'], body)
The
indices
keyword argument inpyes
turns toindex
inpyelasticsearch
, whether the method takes multiple indices or not.The
doc_types
keyword argument inpyes
turns todoc_type
inpyelasticsearch
.get()
will raiseElasticHttpNotFoundError
if the requested documents are not found.pyes
expects arguments toindex
to be in a different order than ourindex()
. The document to be indexed needs to be moved from the first positional argument to the third.send_request()
will raise an error if the response can’t be converted to JSON. If you expect that a response will not be JSON, catch the exception and inspect the status code. For example…connection = ElasticSearch(host) try: # Check for the existence of the "pycon" index: connection.send_request('HEAD', ['pycon']) except InvalidJsonResponseError as exc: if exc.response.status_code == 200: print 'The index exists!'
If using
search_raw
frompyes
, you can usesearch()
and, if necessary, rename the keyword arguments.