Created
September 16, 2015 20:21
-
-
Save luizgpsantos/922592989a6b53c842eb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
echo "Delete document" | |
curl -XDELETE 'http://localhost:8888/' | |
echo "" | |
echo "\nInitial search" | |
curl -XGET 'http://localhost:8888/' | |
echo "" | |
echo "\nCreate document" | |
curl -XPUT 'http://localhost:8888/' | |
echo "" | |
curl -XGET 'http://localhost:8888/' | |
echo "" | |
echo "\nUpdate document" | |
curl -XPOST 'http://localhost:8888/' | |
echo "" | |
curl -XGET 'http://localhost:8888/' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import json | |
import tornado.ioloop | |
import tornado.web | |
from tornadoes import ESConnection | |
class SearchHandler(tornado.web.RequestHandler): | |
index = "index_test" | |
doc_type = "type_test" | |
es_connection = ESConnection("localhost", 9200) | |
@tornado.web.asynchronous | |
def get(self): | |
self.es_connection.search(callback=self.callback_search, source={ | |
"query": { | |
"match_all": {} | |
} | |
}) | |
@tornado.web.asynchronous | |
def put(self): | |
self.es_connection.post_by_path( | |
callback=self.callback, | |
path="/{}/{}/1?refresh=true".format(self.index, self.doc_type), | |
source=json.dumps({ | |
"field": "initial_value" | |
}) | |
) | |
@tornado.web.asynchronous | |
def post(self): | |
self.es_connection.post_by_path( | |
callback=self.callback, | |
path="/{}/{}/1/_update?refresh=true".format(self.index, self.doc_type), | |
source=json.dumps({ | |
"script": "ctx._source.field = \"updated_value\"" | |
}) | |
) | |
@tornado.web.asynchronous | |
def delete(self): | |
self.es_connection.delete( | |
index=self.index, | |
type=self.doc_type, | |
uid=1, | |
callback=self.callback, | |
parameters={ | |
"refresh": True | |
}, | |
) | |
def callback(self, response): | |
self.write(json.loads(json.dumps(response.body, indent=4, sort_keys=True))) | |
self.finish() | |
def callback_search(self, response): | |
self.content_type = 'application/json' | |
response = json.loads(response.body)["hits"]["hits"] | |
self.write(json.dumps(response, indent=4, sort_keys=True)) | |
self.finish() | |
application = tornado.web.Application([ | |
(r"/", SearchHandler), | |
]) | |
if __name__ == "__main__": | |
application.listen(8888) | |
tornado.ioloop.IOLoop.instance().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment