Python Flask ElasticSearch – indexer command

P

Hi, and welcome to the 6th article devoted to the theme:  “How to work with ElasticSearch, Python and Flask”. Previous article (Part 5: Python, Flask, ElasticSearch – model data layer) is located here. At that article we are going to create python console command which would be used for indexing some initial test data to ElasticSearch index. The command is located at src/command folder.

Python indexer command

Here is the code of the command:

import click
from datetime import datetime
from src.elasticsearch.connection import Connection
from src.elasticsearch.documents.hotels import Hotels
from src.elasticsearch.documents.hotel import Hotel
from src.elasticsearch.documents.booking import Booking


class FillElasticsearch:
    @staticmethod
    @click.command("fill-elasticsearch")
    def run():
        click.secho('Saving data to elasticsearch')
        FillElasticsearch.fill_elasticsearch()
        click.secho('Done.')

    @staticmethod
    def fill_elasticsearch():
        # create connection to elasticsearch
        Connection.create_connection()

        # create the mappings in elasticsearch
        Hotels.init()
        Hotel.init()
        Booking.init()

        hotel = Hotel(
            _id=1,
            hotel_id=1,
            name="Golden star hotel",
            city_name_en="Warsaw",
            location={"lat": "52.21", "lon": "21.01"},
            age=7,
            stars=5,
            free_places_at_now=True,
            rating=4.85
        )

        hotel.add_comment(
            hotel_id=1,
            stars=5,
            content="Some content",
            created_at=datetime.now()
        )

        hotel.save()

        hotel.add_booking(200, datetime.now())

        hotel = Hotel(
            _id=2,
            hotel_id=2,
            name="Silver star hotel",
            city_name_en="Warsaw",
            location={"lat": "52.13", "lon": "21.01"},
            age=7,
            stars=4,
            free_places_at_now=True,
            rating=4.6
        )

        hotel.save()


if __name__ == '__main__':
    FillElasticsearch.run()

Of course, that is only simplified example. But here you already have the code that covers all possible scenarios e.g parent child relationship, and nested object. In real life you will have to create some commands for initial indexing and reindexing with much more complicated logic. At real practice you will probably have to connect to some RDB, external microservices to get according data. Moreover, you will have to create some worker that will track all changes provided by customers and administrators at elasticsearch data. That is a little bit out of the horizonts of that article. But the principles I am using here would not be changed. So that command is a good point you would be able to start from. 

At the next lecture we will finish to create our search microservice. Together, we will create search service, query builder and realize filter design pattern. If you can’t wait a long time, then I propose you to view all that material at my on-line course at udemy where you will also find full project skeleton. 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, please wait at next articles. Thank you for you attention. Hope that you liked current part


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