Symfony ElasticSearch

S

Hi,

Current article is the introduction to the practice series of articles devoted to theme “How to work with ElasticSearch using Symfony PHP framework”. You may treat it as Symfony 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: Symfony PHP framework and ElasticSearch. We will also use ONGR Symfony bundle to integrate ElasticSearch with Symfony framework
  • 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 Symfony framework, ONGR bundle, PHP programming language and ElasticSearch engine. If you want to pass all material faster, then I propose you to view 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.


About the author

sergii-demianchuk

Software engineer with over 15 year’s experience. Everyday stack: PHP, Python, Java, Javascript, Symfony, Flask, Spring, Vue, Docker, AWS Cloud, Machine Learning, Ansible, Terraform, Jenkins, MariaDB, MySQL, Mongo, Redis, ElasticSeach

architecture cluster docker elasticsearch flask geo high availability java php programming languages python recommendation systems search systems spring boot symfony