DEV Community

lukaszkuczynski
lukaszkuczynski

Posted on

Elasticsearch viewable by Flask

API for ES - why

So what is the goal? I want to have small app, that will allow greedy HR workers to explore Projects I took part in. I believe, one's experience is the most important feature of future (IT) worker; school was long time ago, courses are OK but who knows who helped to accomplish them?

Python for APIs

Working with some small projects using Django, I believe it's nice framework for web apps. But do we need always some full-featured machine like that? No. Sometimes microservice is ready to be written in few lines. How so?

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"
Enter fullscreen mode Exit fullscreen mode

Yeah, that is everything to write the service that is listening on port 5000 if you will run it with

FLASK_APP=hello.py flask run
Enter fullscreen mode Exit fullscreen mode

That's all folks.

Elasticsearch managed by py

It was fun implementing searching in py official library for ES. It is easy as following:

from elasticsearch import Elasticsearch
es = Elasticsearch()
response = self.es.search(index=INDEX_PATTERN, doc_type=PROJECT_DOCTYPE, q=lucene_query)
Enter fullscreen mode Exit fullscreen mode

Response is regular json so using pythonic dicts it accessing it's body will be possible.

Python + ES = fast API work

Even having TDD approach I was able to expose search functionality for my future webapp in 2 or 3 hours. Integration testing is also easy as this flask-testing and elastic-testing examples show. All results visible so far on my github project page.

Top comments (0)