Current article is the introduction to the practice series of articles devoted to theme “How to work with ElasticSearch using Python and Flask framework”. You may treat it as Python ElasticSearch tutorial though you will get also additional deep knowledge at how to build search microservice from scratch, how to use programming patterns at real examples. So, lets start from task definition and requirements. What we really want to achieve is reflected at next list:
- microservice as a web application that will expose API point for providing hotels search
- the structure and search principles would be close to the case we already described at that article “ElasticSearch how to build search system” (if you still did not read it I definitely advice to to do it in order to keep logical consistency of mind)
- the main application logic would be realized with using next technological stack: Python, Flask framework and ElasticSearch. We will also use Python ElasticSearch DSL package to integrate ElasticSearch with Flask
- application would be built using best programming practices and well known programming patterns: REST API + swagger, DTO, Builder and Filter programming design patterns
- docker + docker compose to create microservice local environment
- microservice structure should be very flexible for further extending and modifications
- our final goal is to get ready working skeleton that can be extended easily to any further requirements
We will start with creating according microservice environment. Using docker and docker compose, we will create web server, microservice application by itself and ElasticSearch as a separate docker containers
Then we will continue with creating our code using the architecture scheme represented below. Our search request would be processed by a controller where we will first create a criteria DTO object. That will create an abstract layer between the front world and our backend logic. You may look at it also from another side, e.g as our search service will have its own interface to work with. Such an approach will allow us to be flexible in changing url parameters. From another side, using the builder pattern we would be able easily change the source and type of request by itself. For example we can expose search additionally for some external microservice that will be integrated with our system within some queue message broker. Next we will pass our DTO search criteria object to the search service that will be responsible for representing search results and calling the query builder object. Query builder will be using a filter design pattern to construct our final ElasticSearch query using an object oriented approach. It would be also responsible for adding sort, paging and aggregations parameters. In addition our microservice will keep a model data layer that will project ElasticSearch document structure to according classes.
Below is the representation of the builder design pattern that we will use to create a DTO search criteria object. The builder pattern is a design pattern designed to provide a flexible solution to various object creation problems in object-oriented programming. The intent of the Builder design pattern is to separate the construction of a complex object from its representation. In our simplified example the search criteria object would not be very complicated but in real practice we can easily have even 50 search parameters. And such an approach would be very helpful.
We will also get acquainted with filter design patterns. That will help to organize our code for creating complicated ES queries.
In the next articles I will try to recreate all above mentioned architecture using Python, Flask framework, EasticSearch DSL package and ElasticSearch engine. If you can’t wait a long time, then I propose you to view all that material at my on-line course at udemy. Below is the link to the course. As the reader of that blog you are also getting possibility to use coupon for the best possible low price. Otherwise, you may continue to read articles below. Thank you for you attention.
- Part 2 “Python,Flask, ElasticSearch and Docker environment“
- Part 3 “Python Flask ElasticSearch – Front Controller and API documentation”
- Part 4 “Python and Flask ElasticSearch – builder pattern and DTO search criteria object”
- Part 5 “Python, Flask, ElasticSearch – model data layer”
- Part 6 “Python, Flask, ElasticSearch – indexer command”
- Part 7 “Python, Flask, ElasticSearch – search service and query builder“