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.
pyelasticsearchrequiresrequests1.x. Breaking changes were introduced inrequests1.0, so if your project was using a previous version, you may need to update your code. Most likely, you just need to changeresponse.jsontoresponse.json().Instantiating the client should be as simple as changing the invocation…
pyes.ES(host, **kwargs)
…to…
pyelasticsearch.ElasticSearch(host, **kwargs)
pyelasticsearchhas 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
indiceskeyword argument inpyesturns toindexinpyelasticsearch, whether the method takes multiple indices or not.The
doc_typeskeyword argument inpyesturns todoc_typeinpyelasticsearch.get()will raiseElasticHttpNotFoundErrorif the requested documents are not found.pyesexpects arguments toindexto 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_rawfrompyes, you can usesearch()and, if necessary, rename the keyword arguments.