Search code examples
pythonflasksearchflask-sqlalchemyfuzzy-search

Search on Products in Flask and Flask-SQLAlchemy


I am currently building a backend for a mobile application with Flask and Flask-SQLAlchemy.

I am currently stuck in a function.

I have a Products table in Postgres database created with Flask-SQLAlchemy. The products have the following fields :

  • Brand
  • Title
  • Description
  • Collection (Men/Women/Kids)
  • Division (Clothing/Shoes/Bags & Accessories/...)
  • Category (Tee-Shirts/Polos/Vests & Coats/Loafers/Slippers/ ...)
  • Price
  • Size id (Relationship to a table Size with records XS/S/...)

I would like to implement a search. For example, when a user sends a request with men tee-shirts Zara, I would like to get the search over the DB performed with ORM query with Brand = Zara, Category = Tee-Shirts, Collection = Men

However, I am struggling with the approach as putting it in place myself would be difficult and I am afraid of the performance. Specially for the cases where you have such search with 'Zara bags and Massimo Dutti shoes'

Do you have any suggestions ?

Thank you in advance.


Solution

  • What you want is probably a full text search engine which does the job for you. One possibility is to use elasticsearch. This is an additional programm which will run on your production server and you will need to feed it with the data from your original database. The implementation is far from trivial however there is this great tutorial which provides a step by step solution for integrating elasticsearch (or any other fulltext search engine) with an flask application.

    Using an existing search engine will cover all the complex search algorithms for you so that you do not have to implement it yourself