DEV Community

lukaszkuczynski
lukaszkuczynski

Posted on

2

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.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay