Skip to content

Instantly share code, notes, and snippets.

@korakot
Last active February 1, 2022 05:06
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save korakot/15fe4f18d0e0f53d7b834ef797880500 to your computer and use it in GitHub Desktop.
Save korakot/15fe4f18d0e0f53d7b834ef797880500 to your computer and use it in GitHub Desktop.
Test elasticsearch server & client on Colab
# install es server
!apt install default-jdk > /dev/null
!wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.4.tar.gz -q --show-progress
!tar -xzf elasticsearch-6.5.4.tar.gz
!chown -R daemon:daemon elasticsearch-6.5.4
# start server
import os
from subprocess import Popen, PIPE, STDOUT
es_server = Popen(['elasticsearch-6.5.4/bin/elasticsearch'],
stdout=PIPE, stderr=STDOUT,
preexec_fn=lambda: os.setuid(1) # as daemon
)
# client-side
!pip install elasticsearch -q
from elasticsearch import Elasticsearch
es = Elasticsearch()
es.ping() # got True
!wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.0.0-linux-x86_64.tar.gz -q
!tar -xzf elasticsearch-7.0.0-linux-x86_64.tar.gz
!chown -R daemon:daemon elasticsearch-7.0.0
# start server
import os
from subprocess import Popen, PIPE, STDOUT
es_server = Popen(['elasticsearch-7.0.0/bin/elasticsearch'],
stdout=PIPE, stderr=STDOUT,
preexec_fn=lambda: os.setuid(1) # as daemon
)
# wait a bit then test
!curl -X GET "localhost:9200/"
from requests import models, Session, Request
from urllib.parse import urljoin
from IPython.core.magic import register_cell_magic
# to display json response
models.Response._repr_html_ = lambda rsp: """
<script src="https://rawgit.com/caldwell/renderjson/master/renderjson.js"></script>
<script>
renderjson.set_show_to_level(1)
document.body.appendChild(renderjson(%s))
</script>
""" % rsp.text
# %%es magic
@register_cell_magic
def es(line=None, cell=None):
line1 = (cell + '\n').find('\n')
method, path = cell[:line1].split(None, 1)
body = cell[line1:].strip()
args = {}
if body:
args['data'] = body + '\n' # in case _bulk
args['headers'] = {'Content-Type': 'application/json'}
rsp = Session().send(
Request(method, urljoin('http://localhost:9200', path), **args)
.prepare())
return rsp
# add more features than magic.py
from requests import models, Session, Request
from urllib.parse import urljoin
from IPython.core.magic import register_cell_magic
import re
# to display json response
def render(r):
text = r.text
if text[0] in "[{": # really json
return """
<script src="https://rawgit.com/caldwell/renderjson/master/renderjson.js"></script>
<script>
renderjson.set_show_to_level(1)
document.body.appendChild(renderjson(%s))
new ResizeObserver(google.colab.output.resizeIframeToContent).observe(document.body)
</script>
""" % text
else: # other status text
return "<pre>%s</pre>" % text
models.Response._repr_html_ = render
# %%es magic
@register_cell_magic
def es(line=None, cell=""):
cell = re.sub(r'(?m)^\s*#.*\n?','', cell) # remove comment
line1 = (cell + '\n').find('\n')
method, path = cell[:line1].split(None, 1)
body = cell[line1:].strip()
args = {}
if body:
args['data'] = (body + '\n').encode() # in case _bulk
args['headers'] = {'Content-Type': 'application/json'}
rsp = Session().send(
Request(method, urljoin('http://localhost:9200', path), **args)
.prepare())
return rsp
@Abhi001vj
Copy link

Thank you for these code snippets but I am getting permission denied error on collab, can we do something about it? Thank you.

@pranavpawar3
Copy link

@Abhi001vj I am facing the same issue, have you found any solution to this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment